Images got changed in recyclerview adapter click - android

I have used RecyclerView to display data which contains an Image and a TextView as rawitem.
My Adapter class is as below :
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.RecyclerViewHolders> {
private List<MyModel> itemList;
private Context context;
private DisplayImageOptions displayImageOptions;
public MyAdapter(Activity context, List<MyModel> itemList) {
this.itemList = itemList;
this.context = context;
}
#Override
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.singleview_grid_location, null);
RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView);
return rcv;
}
#Override
public void onBindViewHolder(final RecyclerViewHolders holder, final int position) {
holder.rl_main.setTag(position);
final MyModel mainModel = itemList.get((int) holder.rl_main.getTag());
if (mainModel.is_selected()) {
holder.rl_main.setBackgroundResource(R.drawable.border_theme);
holder.iv_selection.setVisibility(View.VISIBLE);
} else {
holder.rl_main.setBackgroundColor(Color.parseColor("#FFFFFF"));
holder.iv_selection.setVisibility(View.GONE);
}
holder.txt_location_name.setText(mainModel.getLocationName());
if (mainModel.getImagePath() != null && !mainModel.getImagePath().equals("")) {
Constant.getImageLoader().displayImage(mainModel.getImagePath(),
holder.iv_location_photo, Constant.getInstanceImageOptions(R.mipmap.popup_placeholder), new SimpleImageLoadingListener() {
#Override
public void onLoadingStarted(String imageUri, View view2) {
}
#Override
public void onLoadingFailed(String imageUri, View view2, FailReason failReason) {
holder.iv_location_photo.setImageResource(R.mipmap.popup_placeholder);
}
#Override
public void onLoadingComplete(String imageUri, View view2, Bitmap loadedImage) {
}
});
}
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (mainModel.is_selected()) {
mainModel.setIs_selected(false);
} else {
mainModel.setIs_selected(true);
}
itemList.set(position, mainModel);
notifyDataSetChanged();
}
});
}
#Override
public int getItemCount() {
return this.itemList.size();
}
public class RecyclerViewHolders extends RecyclerView.ViewHolder {
public TextView txt_location_name;
public ImageView iv_location_photo;
public CardView card_view;
public ImageView iv_selection;
public RelativeLayout rl_main;
public RecyclerViewHolders(View itemView) {
super(itemView);
txt_location_name = (TextView) itemView.findViewById(R.id.txt_location_name);
iv_location_photo = (ImageView) itemView.findViewById(R.id.iv_location_photo);
card_view = (CardView) itemView.findViewById(R.id.card_view);
iv_selection = (ImageView) itemView.findViewById(R.id.iv_selection);
rl_main = (RelativeLayout) itemView.findViewById(R.id.rl_main);
}
}
}
The issue is that, When I am clicking on view of Recyclerview, The images got changed. Ya, The textview and my selection remains same and works accurate. The issue is image got changed for views.
What might be the issue ? Please, checkout. Thanks.

What is the point of doing this ??
holder.rl_main.setTag(position);
final MyModel mainModel = itemList.get((int) holder.rl_main.getTag());
You need to get the List position from the position parameter itself that onBindView provides you.
Next You should write onClickListener on Class itself and not implement inside the onBindView.Bad Practice and can create some problems.
Override Method. " getItemViewType(int position) ".
After Modifying the model.
You should do notify item added/removed/changed/inserted based on the need and the provide position which you can get from getAdapterPosition.

Related

itemView.getTag() returns -1

I am using RecyclerView and I want to view the the data of list item that I click on. I'm using itemView.getTag() to get the value of index and typecasting it to my CustomAdapter.
I tried various ways but not to help. please help me.
Here's my code:
public class DonorAdapter extends RecyclerView.Adapter<DonorAdapter.ViewHolder> {
ArrayList<Donor> donorsList;
ItemSelected activity;
public interface ItemSelected{
void onItemClicked(int index);
}
public DonorAdapter(Context context, ArrayList<Donor> list){
activity = (ItemSelected) context;
donorsList = list;
}
public class ViewHolder extends RecyclerView.ViewHolder
{
ImageView ivDonorGender;
TextView tvListName;
TextView tvBloodGroup;
public ViewHolder(#NonNull final View itemView) {
super(itemView);
ivDonorGender = itemView.findViewById(R.id.ivDonorGender);
tvListName = itemView.findViewById(R.id.tvListName);
tvBloodGroup = itemView.findViewById(R.id.tvListBloodGroup);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
activity.onItemClicked(donorsList.indexOf((Donor) itemView.getTag()));
}
});
}
}
#NonNull
#Override
public DonorAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.donors_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(#NonNull DonorAdapter.ViewHolder holder, int position) {
holder.tvListName.setText(donorsList.get(position).getName());
holder.tvBloodGroup.setText(donorsList.get(position).getSurname());
}
}
#Override
public int getItemCount() {
return donorsList.size();
}
}
I am new to android so pardon me if I can't explain properly
Here's the problematic part
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
activity.onItemClicked(donorsList.indexOf((Donor)
itemView.getTag()));
}
I think you need to set the tag for each recycled item in onBindViewHolder
#Override
public void onBindViewHolder(#NonNull DonorAdapter.ViewHolder holder, int position) {
holder.tvListName.setText(donorsList.get(position).getName());
holder.tvBloodGroup.setText(donorsList.get(position).getSurname());
holder.itemView.setTag(donorsList.get(position)); // change here
}
}

fragment recyclerview is giving this error E/RecyclerView: No adapter attached; skipping layout

I,ve tried everything and nothing is working... Any solution please. if i use listview it works perfectly but on recycler view i'm getting an error.
the code is below
home fragment
recyclerViewOne = view.findViewById(R.id.recyclerViewOne);
collectionReference = firebaseFirestore.collection("Trending songs");
recyclerViewOne.setHasFixedSize(true);
recyclerViewOne.setLayoutManager(new
LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
mUpload = new ArrayList<>();
gridViewHolder = new GridViewHolder(getActivity(), mUpload, new
GridViewHolder.ItemClickListeners() {
#Override
public void onClicke(GridModel model, int post) {
((DashboardActivity) getActivity()).method();
((DashboardActivity) getActivity()).playO(post);
}
});
recyclerViewOne.setAdapter(gridViewHolder);
i need some help please
According to the naming, you are trying the set a ViewHolder as an adapter to the recycler. If that is the case, then you should implement it the correct way: a custom Adapter that extends RecyclerView.Adapter and that add that adapter to the recycler view
this is the GridViewHolder
public class GridViewHolder extends RecyclerView.Adapter<GridViewHolder.SongsAdapterViewHolder> {
Context context;
List<GridModel> gridModels;
private ItemClickListeners listeners;
private int selected;
#NonNull
#Override
public SongsAdapterViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.grid_item, parent,false);
return new SongsAdapterViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull SongsAdapterViewHolder holder, int position) {
GridModel gridModel = gridModels.get(position);
holder.mDesc.setText(gridModel.getmDesc());
holder.mTitle.setText(gridModel.getmTitle());
String duration = Utility.convertion(Long.parseLong(gridModel.getDuration()));
Glide.with(context).load(gridModel.imageurl).into(holder.imageurl);
holder.bind(gridModel, listeners);
}
public GridViewHolder(Context context, List<GridModel> gridModels, ItemClickListeners listeners) {
this.context = context;
this.gridModels = gridModels;
this.listeners = listeners;
}
#Override
public int getItemCount() {
return gridModels.size();
}
public class SongsAdapterViewHolder extends RecyclerView.ViewHolder{
TextView mTitle, mDesc;
ImageView imageurl;
public SongsAdapterViewHolder(#NonNull View itemView) {
super(itemView);
mTitle = itemView.findViewById(R.id.gridTitle);
mDesc= itemView.findViewById(R.id.gridMusician);
imageurl = itemView.findViewById(R.id.gridImageView);
}
public void bind(final GridModel gridModel, final ItemClickListeners listeners) {
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listeners.onClicke(gridModel, getAdapterPosition());
}
});
}
}
public interface ItemClickListeners {
void onClicke(GridModel model, int post);
}
public int getSelected() {
return selected;
}
public void setSelected(int selected) {
this.selected = selected;
}
}

Android on click set imageview

so I have an android project that makes use of recyclerview and cardview, where I set a background imageview on click of a card. This is my code:
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.Viewholder> {
private ArrayList <Integer> mImages = new ArrayList<>();
public RecyclerViewAdapter( Context mContext, ArrayList<Integer> mImages) {
this.mImages = mImages;
}
#NonNull
#Override
public Viewholder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.itemlist,viewGroup,false);
return new Viewholder(view);
}
#Override
public void onBindViewHolder(#NonNull final Viewholder viewholder, int i) {
viewholder.img.setImageResource(mImages.get(i));
}
#Override
public int getItemCount() {
return mImages.size();
}
public class Viewholder extends RecyclerView.ViewHolder implements View.OnClickListener{
ImageView img;
ImageView imv;
public Viewholder(#NonNull View itemView) {
super(itemView);
img = itemView.findViewById(R.id.imgview);
imv = itemView.findViewById(R.id.imageView4);
itemView.setOnClickListener(this);
}
#Override
public void onClick(View view) {
Log.d("TEST", "Clicked");
int position = getLayoutPosition();
imv.setImageResource(R.drawable.vehicles);
}
}
}
But it doesn't work on click and the app just crashes. How do I resolve this issue?
You must define your itemView as clickable for receiving click events.
android:clickable="true"
please try move onClickListener to onBindViewHolder
#Override
public void onBindViewHolder(#NonNull final Viewholder viewholder, int i) {
viewholder.img.setImageResource(mImages.get(i));
viewholder.img.setOnClickListener(v -> {
// your code
});
}
Please try
itemView.setOnClickListener(new View.OnClickListener{
#Override
public void onClick(View view){
//your code
}
});

How to get image from recyclerview position clicked data and set it into another imageview?

I have an app which contains recyclerview with imageview and text.When I click on item based on position that is imageview i want image of that image view and set it to another Imageview.To do this I have made interface and implements its method in Activity but need to know that how do i do that
Adapter code:
public interface OnItemClicked {
void onItemClicked(int position);
}
public AvatarListAdapter(Context context, int[] arrayList) {
this.mContext = context;
this.dataModel = arrayList;
}
#Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
return new AvatarListAdapter.ViewHolder(view);
}
#Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
holder.mAvatarImage.setImageResource(dataModel[position]);
holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemClicked(position);
}
});
}
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick;
}
#Override
public int getItemCount() {
return dataModel.length;
}
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
}
}
}
Activity code:
private void initializeActions() {
int numberOfColumns = 3;
mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar);
mAvatarImages.setAdapter(mAdapter);
mAdapter.setOnClick(this);
}
#Override
public void onItemClicked(int position) {
String pos = "You clicked at position " + position;
Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
mAvatarImages.setBackgroundResource(avatar[position]);
}
You can get data by position on OnClick().As you are using local drawable for image resource, One way to do is Modify your viewHolder as follows.This is just a one way you can also use ViewTag.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Circle mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
mAvatarImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int postion =getAdapterPosition();
int image =arrayList[postion];
// Use this image to forward
}
}
Remove OnClick from onBindViewHolder.
Implement RecycleView onClick() method and get specified data while onclick position.have look
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
private Circle mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
mAvatarImage.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int position=arrayList[getAdapterPosition()];
String imagepath = arraylist.get(position).getImagePath
//you can use imagepath anywhere you want either be put in bundle or intent call
}
}
Adapter code should look like this
private final OnItemClickListener listenerOnItemClick;
public interface OnItemClickListener {
void onItemClick(int position); }
public AvatarListAdapter(Context context, int[] arrayList, OnItemClickListener listenerOnItemClick) {
this.mContext = context;
this.dataModel = arrayList;
this.listenerOnItemClick=listenerOnItemClick;
}
#Override
public AvatarListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.avatar_items, parent, false);
return new AvatarListAdapter.ViewHolder(view); }
#Override
public void onBindViewHolder(AvatarListAdapter.ViewHolder holder, final int position) {
holder.mAvatarImage.setImageResource(dataModel[position]);
holder.mAvatarImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClick.onItemClicked(position);
}
}); }
public void setOnClick(OnItemClicked onClick) {
this.onClick = onClick; }
#Override
public int getItemCount() {
return dataModel.length; }
public class ViewHolder extends RecyclerView.ViewHolder {
private CircleImageView mAvatarImage;
public ViewHolder(View itemView) {
super(itemView);
mAvatarImage = itemView.findViewById(R.id.img_avatar);
} }
Activity Code
AvatarListAdapter.OnItemClickListener onItemClickListener = new AvatarListAdapter.OnItemClickListener() {
#Override
public void onItemClick(int position) {
//change your Image here
String pos = "You clicked at position " + position;
Toast.makeText(getApplicationContext(), "You clicked position" + pos, Toast.LENGTH_LONG).show();
mAvatarImages.setBackgroundResource(avatar[position]);
}
};
private void initializeActions() {
int numberOfColumns = 3;
mAvatarImages.setLayoutManager(new GridLayoutManager(AvatarProfileImage.this, numberOfColumns));
mAdapter = new AvatarListAdapter(AvatarProfileImage.this, avatar,onItemClickListener );
mAvatarImages.setAdapter(mAdapter);
mAdapter.setOnClick(this);
}

Android RecyclerView onClick in different Activity

Everything works well and my onClick in my recyclerView is working in getting the positions of my items, but what my design calls for is to be able to click an item of the recyclerView and open up a new activity (as a popover or pop up). I can achieve this but my problems comes with the information I need to display on the popover. The information comes like this inside the activity (inside a Firebase value call)
attributeList.removeAll(attributeList);
for (DataSnapshot child : dataSnapshot.child("Attribute").getChildren()){
Attribute attribute = child.getValue(Attribute.class);
attribute_list newAttributeList = new attribute_list( attribute.Name + ": " + attribute.Value);
attributeList.add(newAttributeList);
}
attributeAdapter = new attribute_list_adapter(attributeList, getContext());
recyclerAttribute.setAdapter(attributeAdapter);
This works perfectly for displaying the information, but there's more then just a "value" and a "name" associated with the click.
Basically when I select an item, I need to get the position of the item clicked (which I have) and compare it to the position inside attributeList so I can call a Firebase call (or pass the data somehow) to the popover to display values from the "Attribute" class (such as Name, Value, Description, and another list (recyclerView).
My recyclerView:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
attribute_list listItem = listItems.get(position);
holder.txtTitle.setText(listItem.getTxtTitle());
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView txtTitle;
public ViewHolder(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
txtTitle = (TextView) itemView.findViewById(R.id.txtTitle);
}
#Override
public void onClick(View v) {
}
}
}
This is example:
public class attribute_list_adapter extends RecyclerView.Adapter<attribute_list_adapter.ViewHolder> {
private List<attribute_list> listItems;
private Context context;
public attribute_list_adapter(List<attribute_list> listItems, Context context) {
this.listItems = listItems;
this.context = context;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.attribute_list, parent, false);
return new ViewHolder(v);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.itemView.setOnClickListener(new View.onClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(position);
}
});
}
#Override
public int getItemCount() {
return listItems.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView txtberita;
ImageView imgberita;
TextView txtnama;
public ViewHolder(View itemView) {
super(itemView);
txtnama = (TextView) itemView.findViewById(R.id.txtnama);
txtberita = (TextView) itemView.findViewById(R.id.txtberita);
imgberita = (ImageView) itemView.findViewById(R.id.imgberita);
}
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener){
this.onItemClickListener = onItemClickListener;
}
OnItemClickListener onItemClickListener;
public interface OnItemClickListener{
void onItemClick(int position);
}
}
your Activity. in Oncreate()
public class TestActivity extends AppCompatActivity implements attribute_list_adapter.OnItemClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
attribute_list_adapter adapter = new attribute_list_adapter(listItems, this);
adapter.setOnItemClickListener(this);
}
#Override
public void onItemClick(int position) {
// code here
}
}
Create an interface something like
public interface OnSingleItemClickListener{
void onSingleItemClick(int position);
}
Then implement it on your ViewHolder like this
public class ViewHolder extends RecyclerView.ViewHolder implements OnSingleItemClickListener {
public ViewHolder(View itemView) {
super(itemView);
}
#Override
void onSingleItemClick(int position){
if(listItems.get(position) == listItems.get(getAdapterPosition)){
// TODO do something here
}
}
now on your OnBindViewHolder inside your adapter you must do this.
holder.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
holder.onSingleItemClick(position);
}
}):

Categories

Resources