Farry
Farry

Reputation: 21

How to get item name when an item is clicked in recyclerview?

I'm using library to get clicked item of recyclerView, item click is working fine, but when I try to get data i.e. models.get(position).getName(); it is returning a null.

Model is a ArrayList Class including getter and setter method.

 RecycleClick.addTo(mRecyclerView).setOnItemClickListener(new RecycleClick.OnItemClickListener() 
{
                @Override
                public void onItemClicked(RecyclerView recyclerView, int 
         position, View v) 
{

   try 
{

    String category=models.get(position).getCategory();

   Toast.makeText(v.getContext(),category,Toast.LENGTH_SHORT).show();

                    }
    catch (Exception e)
                    {
                        Toast.makeText(HomeActivity.this,"exception "+e.getMessage(),Toast.LENGTH_SHORT).show();

                    }
                }
            });

Adapter Class:

public class MyAdapter extends RecyclerView.Adapter {

Context c;
ArrayList<ModelHome> models;
public MyAdapter(Context c, ArrayList<ModelHome> models) {
    this.c = c;
    this.models = models;
}

@NonNull
@Override
public MyHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i)
{
    View v= LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.home_screen_row,null);
    return new MyHolder(v);
}

@Override
public void onBindViewHolder(@NonNull final MyHolder myHolder, int i)
{

    myHolder.category.setText(models.get(i).category);
    try
    {
        Picasso.get().load(models.get(i).image).into(myHolder.Image);
    }
    catch (Exception e)
    {
        Picasso.get().load(R.drawable.loading).into(myHolder.Image);

    }

}

@Override
public int getItemCount() {
    return models.size();
}

}

Holder Class:

public class MyHolder extends RecyclerView.ViewHolder{

TextView category;
ImageView Image;
RelativeLayout relativeLayout;

public MyHolder(@NonNull View itemView) {
    super(itemView);
    this.category=itemView.findViewById(R.id.image_title_home);
    this.Image=itemView.findViewById(R.id.firebaseImage_home);
     }

}

Model Class:

public class ModelHome { String category,image;

public ModelHome(String category, String image) {
    this.category = category;
    this.image = image;
}

public ModelHome() {
}

public String getCategory() {
    return category;
}

public void setCategory(String category) {
    this.category = category;
}

public String getImage() {
    return image;
}

public void setImage(String image) {
    this.image = image;
}

}

When i click an item and try to fetch corresponding text like : String category=models.get(position).getCategory(); it is returning null. i am using try{} catch block

Upvotes: 0

Views: 588

Answers (2)

Farry
Farry

Reputation: 21

I solved it by eliminating the local ArrayList object , use the ModelHome object Globally and the issue will be resolved.

Upvotes: 0

Md. Shameem Hossain
Md. Shameem Hossain

Reputation: 126

Did you set any data in the RecyclerView Adapter? Please first of all check that.

Upvotes: 0

Related Questions