Reputation: 161
I made an adapter to show a number of cities where we have to select them. However, when i scroll down and go back up again the selected items are de-selected again.
I tried setting sethasStableIds to true but that did not work either
public class CitiesAdapter extends RecyclerView.Adapter<CitiesAdapter.ViewHolder> {
private static final String TAG = "CitiesViewAdapter";
private List<Cities> citiesModelList;
private Context mContext;
private OnClickListner mOnNoteListner;
public CitiesAdapter(List<Cities> citiesModelList, Context mContext , OnClickListner mOnNoteListner) {
this.citiesModelList = citiesModelList;
this.mContext = mContext;
this.mOnNoteListner = mOnNoteListner;
setHasStableIds(true);
}
@NonNull
@Override
public CitiesAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cities_listitems, viewGroup , false);
CitiesAdapter.ViewHolder holder = new CitiesAdapter.ViewHolder(view , mOnNoteListner , this);
return holder;
}
@Override
public void onBindViewHolder(@NonNull CitiesAdapter.ViewHolder viewHolder, int i) {
Log.d(TAG , "onBindViewHolder: called.");
Cities citiesmodels = citiesModelList.get(i);
viewHolder.tvCitiesName.setText(citiesmodels.getName());
}
@Override
public int getItemCount() {
return citiesModelList.size();
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public int getItemViewType(int position)
{
return position;
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
TextView tvCitiesName;
OnClickListner onClickListner;
private CitiesAdapter citiesAdapter;
public ViewHolder(@NonNull View itemView , OnClickListner onClickListner , CitiesAdapter citiesAdapter) {
super(itemView);
tvCitiesName = itemView.findViewById(R.id.tvCitiesName);
this.onClickListner = onClickListner;
itemView.setOnClickListener(this);
this.citiesAdapter = citiesAdapter;
}
@Override
public void onClick(View v) {
onClickListner.onItemClick(getAdapterPosition(),citiesAdapter.getItem(getAdapterPosition()));
}
}
public Cities getItem(int i){
return citiesModelList.get(i);
}
public interface OnClickListner{
void onItemClick(int position , Cities cities);
}
}
I have a pasted my adapter below. Any help would be greatly appreciated. Thanks in adavnce
Upvotes: 1
Views: 1979
Reputation: 1574
This happen because of when we scroll onBindViewHolder method call.
//create integer list for selected city or if there is only one selected city then create variable selected city
private List<integer> SelectedcitiesModelList= new ArrayList<integer>() //in adapter class
// now assign tag to tvCitiesName in onBindViewHolder method
viewHolder.tvCitiesName.setTag(i); //to identify the view and i is position
//now in onclick method get textview tag and put in list or variable
SelectedcitiesModelList.add(viewHolder.tvCitiesName.getTag())
//now go to onBindViewHolder method
for(int p;i<SelectedcitiesModelList.size();p++)
{
if(p==i)
{
//this is your selected item
//do what you want to
}
}
Upvotes: 0
Reputation: 3167
On the Cities
object, define a boolean property called selected
and use a method to determine if it is selected by using a getter such as boolean isSelected()
. Then, on your Adapter's onBindViewHolder
method add something like this:
{
// Code to add inside onBindViewHolder.
if (cityModel.isSelected())
{
// Select the view you want to select.
}
else
{
// Deselect the view.
}
}
Always use an if-else condition to avoid incorrect results.
Upvotes: 1