Reputation: 281
I am new to android programming. I want to update the items in a card view layout of a recyclerview. This is the ListRowViewHolder for the recyclerview
public static class ListMenuRowViewHolder extends RecyclerView.ViewHolder
{
protected NetworkImageView thumbnail;
protected TextView itemname;
protected TextView price;
protected TextView itemtype;
protected TextView quantity;
protected ImageView add;
protected ImageView sub;
protected ImageView imageView;
protected CardView item_layout;
public ListMenuRowViewHolder(View itemView) {
super(itemView);
this.thumbnail=(NetworkImageView)itemView.findViewById(R.id.recom);
this.imageView=(ImageView)itemView.findViewById(R.id.categ);
this.itemname=(TextView)itemView.findViewById(R.id.itemvalue);
this.add=(ImageView)itemView.findViewById(R.id.add);
this.sub=(ImageView)itemView.findViewById(R.id.sub);
this.price=(TextView)itemView.findViewById((R.id.price));
this.quantity=(TextView)itemView.findViewById((R.id.quantity));
this.item_layout=(CardView)itemView.findViewById((R.id.item_layout));
itemView.setClickable(true);
itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e("myname", "rohit");
ItemscardClickListener.onClick(v, getAdapterPosition());
}
});
}
}
This is where i am trying to update quantity
mAdapter.setItemsCardClickListener(new ItemsCardClickListener() {
@Override
public void onClick(View v, int position) {
int q;
switch (v.getId())
{
case R.id.add : q= Integer.parseInt(mAdapter.getmOrder().get(position).getQuantity());
q++;
Log.e("mishra", String.valueOf(q));
mAdapter.getmOrder().get(position).setQuantity(String.valueOf(q));
mAdapter.notifyDataSetChanged();
break;
case R.id.sub : q=Integer.parseInt(mAdapter.getmOrder().get(position).getQuantity());
if(q>0)
q--;
mAdapter.getmOrder().get(position).setQuantity(String.valueOf(q));
mAdapter.notifyDataSetChanged();
break;
case R.id.quantity:
Log.e("mishra", "hihi");
break;
default:Log.e("mishra", "hi");
}
}
});
Everytime when i am clicking on an item in the view they are clicked but i am not getting the id using v.getId(). The switch is always entering the default case. Please provide some solution.
Upvotes: 4
Views: 1924
Reputation: 2428
You are setting the OnClickListener
on your View
that contains all of the other subviews and not on the individual subviews themselves. You should let the ListMenuRowViewHolder
implement View.OnClickListener
and set a listener on each individual view like so:
sub.setOnClickListener(this);
price.setOnClickListener(this);
//etc
You can implement
@Override
public void onClick(View v) {
//Now the correct subview gets passed to your switch in the Activity
ItemscardClickListener.onClick(v, getAdapterPosition());
}
in the ListMenuRowViewHolder
.
So it will look like this:
public static class ListMenuRowViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
protected NetworkImageView thumbnail;
protected TextView itemname;
protected TextView price;
protected TextView itemtype;
protected TextView quantity;
protected ImageView add;
protected ImageView sub;
protected ImageView imageView;
protected CardView item_layout;
public ListMenuRowViewHolder(View itemView) {
super(itemView);
this.thumbnail = (NetworkImageView) itemView.findViewById(R.id.recom);
this.imageView = (ImageView) itemView.findViewById(R.id.categ);
this.itemname = (TextView) itemView.findViewById(R.id.itemvalue);
this.add = (ImageView) itemView.findViewById(R.id.add);
this.sub = (ImageView) itemView.findViewById(R.id.sub);
this.price = (TextView) itemView.findViewById(R.id.price);
this.quantity = (TextView) itemView.findViewById(R.id.quantity);
this.add.setOnClickListener(this);
this.sub.setOnClickListener(this);
//Do this for each view
}
@Override
public void onClick(View v) {
Log.e("myname", "rohit");
ItemscardClickListener.onClick(v, getAdapterPosition());
}
}
Upvotes: 3
Reputation: 29260
You are setting a click listener of the container (itemView). You need to set click listeners on your individual views:
this.add.setOnClickListener(mListener);
this.sub.setOnClickListener(mListener);
this.price.setOnClickListener(mListener);
this.quantity.setOnClickListener(mListener);
Then declare a listener:
View.OnClickListener mListener = new View.OnClickListener() {
public void onClick(View view) {
switch (view.getId()) {
case R.id.add:
break;
case R.id.sub:
break;
// add others
}
}
}
Upvotes: 0