How can I show an array using RecyclerView? - java

class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private final int listSize;
private String[] list;
public MyAdapter(int listSize, String[] list) {
this.listSize = listSize;
this.list = list;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.rv_item,parent,false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
holder.bind(position);
}
#Override
public int getItemCount() {
return listSize;
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView item;
Button button;
public ViewHolder(#NonNull View itemView) {
super(itemView);
item = itemView.findViewById(R.id.item_view);
button = itemView.findViewById(R.id.button_item_view);
}
void bind(int index) {
button.setText(String.valueOf(index));
item.setText(list[index]);
}
}
}
In this adapter class I want to show a String array. The only problem is that the first element (with zero index) is missed on the screen, despite the fact that element exist beyond the screen (Sorry for such a big picture).
The buttons must be numbered with zero, so should the array be printed starting with zero element. How can I resolve this problem?
UPD: Here's the layout:
rv_item
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/item_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|start"
android:text="TextView"
android:textSize="40sp" />
<Button
android:id="#+id/button_item_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|end"
android:layout_marginEnd="48dp"
android:padding="10dp"
android:text="INFO" />
</FrameLayout>
activity_top, in which recyclerView widget is used
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".TopLevelActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listOfGoods"
android:layout_width="409dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/rv_item" />
</androidx.constraintlayout.widget.ConstraintLayout>

Do not give fixed height of recyclerView
Correct Way:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listOfGoods"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/rv_items" />
P.S: I have taken your example and run the app on api 29 work fine cheers!
Also Do not make it listSize final, compiler will give error as you are not initializing

I know that it could be the same, but I would like to corroborate, try to change this code:
#Override
public int getItemCount() {
return listSize;
}
For this:
#Override
public int getItemCount() {
return list.length;
}
One more thing: try to debug this object this.list = list; and check if the array contains zero index element. If the array contains that element, probably it should something about layout as mentioned #Rui Alves.

Related

How to add single item between two grid row in android

Hello guys I want to display one single item between two grid row items.
I want this type of out.
I'm try this way but it didn't work.
My Activity layout XML activity_grid_with_single_rv.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activitry.GridWithSingleRVActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvGridWithSingle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"/>
</LinearLayout>
My Activity class GridWithSingleRVActivity.java
public class GridWithSingleRVActivity extends AppCompatActivity {
private RecyclerView rvGridWithSingle;
private GridWIthSingleRVAdapter gridWIthSingleRVAdapter;
private ArrayList<GridWithSingle> gridWithSingles=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_grid_with_single_rv);
rvGridWithSingle=findViewById(R.id.rvGridWithSingle);
getGridWithSingles();
rvGridWithSingle.setLayoutManager(new GridLayoutManager(this,2));
gridWIthSingleRVAdapter=new GridWIthSingleRVAdapter(this,gridWithSingles);
rvGridWithSingle.setAdapter(gridWIthSingleRVAdapter);
}
private ArrayList<GridWithSingle> getGridWithSingles(){
gridWithSingles.clear();
gridWithSingles=new ArrayList<>();
int addSingleCnt=0;
for (int i=0;i<20;i++){
gridWithSingles.add(new GridWithSingle(i+1,"GRID : "+(i+1)));
if (addSingleCnt==1){
addSingleCnt=0;
gridWithSingles.add(new GridWithSingle(-1,"Single : "));
}else {
addSingleCnt++;
}
}
return gridWithSingles;
}
}
My Adapter class GridWIthSingleRVAdapter.java
public class GridWIthSingleRVAdapter extends RecyclerView.Adapter<GridWIthSingleRVAdapter.GridWIthSingleRVVH> {
private Context context;
private ArrayList<GridWithSingle> gridWithSingles=new ArrayList<>();
private final int GRID_LAYOUT=1;
private final int SINGLE_LAYOUT=0;
public GridWIthSingleRVAdapter(Context context, ArrayList<GridWithSingle> gridWithSingles) {
this.context = context;
this.gridWithSingles = gridWithSingles;
}
#NonNull
#Override
public GridWIthSingleRVVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
if (viewType==GRID_LAYOUT)
return new GridWIthSingleRVVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_grid_item,parent,false));
else
return new GridWIthSingleRVVH(LayoutInflater.from(parent.getContext()).inflate(R.layout.row_single_item,parent,false));
}
#Override
public void onBindViewHolder(#NonNull GridWIthSingleRVVH holder, int position) {
if (holder.getItemViewType()==GRID_LAYOUT){
holder.txtGrid.setText(gridWithSingles.get(position).getName());
}else {
holder.txtSingle.setText(gridWithSingles.get(position).getName());
}
}
#Override
public int getItemCount() {
return gridWithSingles.size();
}
#Override
public int getItemViewType(int position) {
if (gridWithSingles.get(position).getId()==-1){
return SINGLE_LAYOUT;
}else {
return GRID_LAYOUT;
}
}
public class GridWIthSingleRVVH extends RecyclerView.ViewHolder{
private TextView txtGrid,txtSingle;
public GridWIthSingleRVVH(#NonNull View itemView) {
super(itemView);
txtGrid=itemView.findViewById(R.id.txtGrid);
txtSingle=itemView.findViewById(R.id.txtSingle);
}
}
}
Two layout item xml
grid_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/cvGrid"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardBackgroundColor="#EF9A9A">
<TextView
android:id="#+id/txtGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GRID"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/black"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
single_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="#+id/cvSingle"
android:layout_margin="10dp"
app:cardCornerRadius="5dp"
app:cardElevation="5dp"
app:cardBackgroundColor="#CE93D8">
<TextView
android:id="#+id/txtSingle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SINGLE"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/black"/>
</androidx.cardview.widget.CardView>
</LinearLayout>
Output
You need to specify a spanSizeLookup for the affected columns to the layoutManager.
A couple of years ago I wrote a similar proof of concepts where the idea was to insert "ads" every 4 rows.
The project is located on GitHub.
The sample does this the trick directly in the activity by using recyclerView types and modifying the span size.
layoutManager = GridLayoutManager(this, 2)
mainRecyclerView.layoutManager = layoutManager
mainRecyclerView.adapter = adapter
layoutManager.spanSizeLookup = object : GridLayoutManager.SpanSizeLookup() {
override fun getSpanSize(position: Int): Int {
return when (adapter.getItemViewType(position)) {
adapter.viewTypeGreen -> 1
adapter.viewTypePurple -> 1
adapter.viewTypeAd -> 2
else -> -1
}
}
}
The repository that provides this data simulates inserting a different type every 4 items

Android Recycler returns zero output despite reaching onBindViewHolder() for each row

I've put a RecyclerView in the 'Tutorial' activity in my application, to list tutorial versions, which come from the Room database my app has. It is hosted within a fragment, but that's of little consequence since I had the same problem before putting it in there. The RecyclerView adapter reaches onBindViewHolder() without any issues and I get logs from the Log.w I used for debugging there with correct data. The problem seems to be that Layout does not get inflated at all, and the buttons that each row is supposed to output never appear.
THE ADAPTER
public class VersionListAdapter extends RecyclerView.Adapter<VersionListAdapter.VersionViewHolder> {
private List<Version> mVersionList;
private final LayoutInflater mInflater;
private final OnViewClickListener mListener;
public VersionListAdapter(Context context)
{
this.mInflater = LayoutInflater.from(context);
this.mVersionList = null;
this.mListener = (OnViewClickListener) context;
}
#NonNull
#Override
public VersionViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int viewType) {
View row = mInflater.inflate(R.layout.row_versions_rv, viewGroup, false);
return new VersionViewHolder(row, mListener);
}
#Override
public void onBindViewHolder(#NonNull VersionViewHolder versionHolder, int rowNumber) {
versionHolder.thisVersion = mVersionList.get(rowNumber);
versionHolder.versionButton.setText((mVersionList.get(rowNumber).getText()));
Log.w("THIS IS A TAG", mVersionList.get(rowNumber).getText()); //correct output
}
#Override
public int getItemCount() {
if(mVersionList!=null) return mVersionList.size();
else return 0;
}
public void setElementList(List<Version> versions){
this.mVersionList = versions;
notifyDataSetChanged();
}
public static class VersionViewHolder extends RecyclerView.ViewHolder
implements View.OnClickListener
{
OnViewClickListener listenerForThisRow;
Version thisVersion;
Button versionButton;
public VersionViewHolder(
#NonNull View viewForThisRow, OnViewClickListener listenerFromActivity)
{
super(viewForThisRow);
this.listenerForThisRow = listenerFromActivity;
versionButton = viewForThisRow.findViewById(R.id.version_button);
viewForThisRow.setOnClickListener(this);
}
#Override
public void onClick(View v){
listenerForThisRow.onViewClick(thisVersion);
}
}
public interface OnViewClickListener{
void onViewClick(Version version);
}
}
THE FRAGMENT CLASS WHICH HOSTS THE RECYCLER VIEW
public class VersionRecycler extends Fragment {
protected RecyclerView mRecycler;
protected VersionListAdapter mAdapter;
public VersionRecycler(){
super(R.layout.fragment_recycler_versions);
}
#Override
public View onCreateView(
#NotNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState
) {
VersionViewModel mVersionViewModel = new VersionViewModel(requireActivity().getApplication());
long tutorialId = requireArguments().getLong("tutorialId");
View view = inflater.inflate(R.layout.fragment_recycler_versions, container, false);
mAdapter = new VersionListAdapter(requireActivity());
mRecycler = view.findViewById(R.id.versions_rv);
mRecycler.setLayoutManager(new LinearLayoutManager(view.getContext(), LinearLayoutManager.VERTICAL, false));
mVersionViewModel.getByTutorialId(tutorialId).observe(requireActivity(), versions->mAdapter.setElementList(versions));
mRecycler.setAdapter(mAdapter);
return view;
}
}
HOW THE FRAGMENT IS EMBEDDED IN THE ACTIVITY
<LinearLayout
android:id="#+id/layout_versions_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
FRAGMENT LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
LAYOUT FOR RECYCLERVIEW ROW
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false">
<Button
android:id="#+id/version_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="40sp"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You should also add Start and Bottom constraints:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".versionrecycler.VersionRecycler">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/versions_rv"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:listitem="#layout/row_versions_rv"/>
</androidx.constraintlayout.widget.ConstraintLayout>
You could also change RecyclerView layout_height to wrap_content and add Start constraint only

Spinner's style is changed once i open it for the second

I am developing an app where I show a dropdownlist using a spinner.
I used a custom adapter, here is the code:
public class DemandeCongeAdapter extends BaseAdapter {
public static final String TAG = "DemandeCongeAdapter";
private final Context mContext;
private List<DemandeCongeType> mData;
protected LayoutInflater mInflater;
public DemandeCongeAdapter(Context mContext, List<DemandeCongeType> mData) {
this.mContext = mContext;
this.mData = mData;
this.mInflater = LayoutInflater.from(mContext);
}
static class ViewHolder {
public ViewHolder(View v) {
typedeDemandeName = (TextView) v.findViewById(R.id.type_demande_name);
}
protected final TextView typedeDemandeName;
}
#Override
public int getCount() {
return mData.size();
}
#Override
public DemandeCongeType getItem(int position) {
return mData.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.layout_create_demande_conge_custom_spinner, parent, false);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
if (position != 0) {
DemandeCongeType item = getItem(position);
String name = item.getTypdeDemandeName() == null ? "" : item.getTypdeDemandeName();
viewHolder.typedeDemandeName.setText(name);
}
return convertView;
}
}
And for the activity code, here is it:
private List<DemandeCongeType> congeTypes;
private DemandeCongeAdapter spinnerCongeTypeArrayAdapter;
congeTypes.addAll(new ArrayList<>((List<DemandeCongeType>) responseBody));
congeTypes.add(0, new DemandeCongeType());
spinnerCongeTypeArrayAdapter = new
DemandeCongeAdapter(DemandeCongeNewActivity.this, congeTypes);
congeTypeSpinner.setAdapter(spinnerCongeTypeArrayAdapter);
congeTypeSpinner.setSelection(Adapter.NO_SELECTION, false);
Everything works fine, but when I select the spinner for the first time, it shows the list correctly like this:
Once I click outside the spinner and I click for the second time on the spinner, I got it like this:
So I can't the find the problem, please if anyone can help me with that.
Here is the XML code:
<Spinner
android:id="#+id/spinner_type_conge"
fontPath="fonts/light.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:background="#null"
android:dropDownWidth="match_parent"
android:ellipsize="end"
android:lines="1"
android:paddingRight="2dp"
android:spinnerMode="dropdown"
android:textSize="13sp" />
And for the spinner item xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/type_demande_name"
fontPath="fonts/light.otf"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:lines="1"
android:padding="10dp"
android:textColor="#color/annuaire_hint_color"
android:textSize="13sp" />
Try This way i have use this in my Application.
XML Design Code.
Spinner set in your Activity
<Spinner
android:id="#+id/sp_pen_Category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:padding="#dimen/margin_10" />
This file XML File sp_list.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:padding="15dp"
android:singleLine="true"
android:textColor="#color/black"
android:textSize="#dimen/sub_title">
</CheckedTextView>
This file XML File sp_items.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:ellipsize="marquee"
android:singleLine="true"
android:textColor="#color/black">
</CheckedTextView>
This is Java code when you fill spinner.
ArrayAdapter<String> adp_sp = new ArrayAdapter<String>(Activity.this, R.layout.sp_items, arr_spinner);
adp_sp.setDropDownViewResource(R.layout.sp_list);
sp_pen_Category.setAdapter(adp_sp);

Android - Custom ListView For Only One Single Row

I Want Something Like Shown In Image Below... As Item 3, Item 4 And Item 7 Has A Toggle Switch But Item 1, Item 2, Item 5, Item 6 Doesn't Have. Can Anyone Help Me To Make This Layout And Make Toggle Switch Work Too
I Want This (Made In Photoshop)
My Java File
import android.content.*;
import android.view.*;
import android.widget.*;
class CustomSettingsAdapter extends ArrayAdapter<String> {
String[] settingItems = {
"Themes",
"Entry Tune",
"Remember Last Location",
"About Us",
"Exit"
};
public CustomSettingsAdapter(Context context, String[] Items) {
super(context, R.layout.main_settings_listview, Items);
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
View customView = layoutInflater.inflate(R.layout.main_settings_listview, parent, false);
String itemName = getItem(position);
TextView textView =(TextView) customView.findViewById(R.id.itemName);
Switch mButton = (Switch) customView.findViewById(R.id.Switch);
if (position == 1 || position == 2) {
mButton.setVisibility(View.VISIBLE);
}
textView.setText(settingItems[position]);
return customView;
}
}
** XML **
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="6dp"
android:minHeight="48dp"
android:id="#+id/mainActivityListBackground"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item Number"
android:id="#+id/itemName"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:layout_centerVertical="true"
/>
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Switch"
android:visibility="invisible"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
</RelativeLayout>
Use this code it help you.
item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/code"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:layout_weight="1"
android:textSize="16dp" />
<Switch
android:id="#+id/toggleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:gravity="center"
android:text="Off"
android:visibility="invisible" />
</LinearLayout>
and use this adapter
public class PhoneAdapter extends BaseAdapter {
private Context context;
public PhoneAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 7;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(final int i, View convertView, ViewGroup viewGroup) {
convertView = View.inflate(context, R.layout.item, null);
TextView mCode = (TextView) convertView.findViewById(R.id.code);
Switch mButton = (Switch) convertView.findViewById(R.id.toggleButton);
mCode.setText("item"+i+1);
if (i == 2 || i == 3 || i == 6)
mButton.setVisibility(View.VISIBLE);
return convertView;
}}
and this is output:-
feel free to ask if you stuck anywhere in between.
EDIT:- getView() is use for identify which button is clicked so you don't want to care about it .In the getView() the i variable is used for identify which item is clicked.
Just set your OnchangeListner inside getView() and your problem solve.
You can use recycler view.You can do this by creating two xml for two different designs,and on basis of condition you can set view in layout inflater.Use these methods for extra views.
#Override
public int getViewTypeCount() {
return VIEW_TYPE_COUNT;
}
#Override
public int getItemViewType(int position) {
return position;
}
You can also refer to this link.

android dynamic images put tick mark and select optionally one image

I want select optionally one image like radio button and put tick mark on that image. How can i do it.Here I add my adapter class and xml file
This is my adapter class.
public class StarCountAdapter extends RecyclerView.Adapter<StarCountAdapter.StarCountHolder> {
Context context;
LayoutInflater inflater;
List<StarCount> starCounts = new ArrayList<>();
public StarCountAdapter(Context context, List<StarCount> starCounts) {
this.context = context;
this.inflater = LayoutInflater.from(context);
this.starCounts = starCounts;
}
#Override
public StarCountHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.star_count_row,parent,false);
return new StarCountHolder(view);
}
#Override
public void onBindViewHolder(StarCountHolder holder, int position) {
StarCount model = starCounts.get(position);
Picasso.with(context)
.load("http://"+model.getImagePath())
.into(holder.starImage);
holder.actorName.setText(model.getName());
holder.counts.setText(""+model.getCount());
}
#Override
public int getItemCount() {
return starCounts.size();
}
public class StarCountHolder extends RecyclerView.ViewHolder {
ImageView starImage;
TextView actorName,counts;
public StarCountHolder(View itemView) {
super(itemView);
starImage = (ImageView) itemView.findViewById(R.id.starCountIv);
actorName = (TextView) itemView.findViewById(R.id.acterName);
counts = (TextView) itemView.findViewById(R.id.counts);
}
}
}
This is my star_count_row.xml file.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp">
<ImageView
android:id="#+id/starCountIv"
android:layout_width="match_parent"
android:layout_height="175dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="120dp"
android:orientation="vertical">
<TextView
android:id="#+id/acterName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000" />
<TextView
android:id="#+id/counts"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#000" />
</LinearLayout>
</RelativeLayout>
Thank You.
create custom radio button followin link here you will get your answer
Adding custom radio buttons in android

Categories

Resources