I want to change my quantity textview to reflect the quantity of a particular item. But on clicking the add and sub buttons, the setText() function does not seem to work. Here i
First of all instead of taking these individual arrays, you should simply make a custom modal class with all the values you need. For example in your case, it will be something like this.
public class Item {
private String itemName = "";
private int quantity;
private int rate;
private int image;
public String getItemName() {
return itemName;
}
public void setItemName(String itemName) {
this.itemName = itemName;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public int getRate() {
return rate;
}
public void setRate(int rate) {
this.rate = rate;
}
public int getImage() {
return image;
}
public void setImage(int image) {
this.image = image;
}
}
And now pass this modal class to you adapter constructor.And this is how you can set value in your getView()
Item item=itemList.get(position); //here itemList is the list you are passing to your adapter.
holder.imageView.setImageResource(item.getImage());
holder.textView.setText(item.getItemName());
holder.rates.setText(String.valueOf(item.getRate()));
holder.number.setText(String.valueOf(item.getQuantity()));
Now coming to your add/sub click listener,there is no need to set text of your quantity TextView inside add/sub click listener. You can simply notify your adapter after updating the value of quantity at corresponding index for the itemList. For eg. for add quantity you can do something like this.
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Item item=itemList.get(position);// getting the item at particular position
item.setQuantity(item.getQuantity()+1);// incrementing item quantity by 1
itemList.set(position,item);// updating the itemList to that new item with incremented quantity
notifyDataSetChanged();//notifying the adapter
}
});
Same you can do for the your subtract quantity onClickListener().
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Item item=itemList.get(position);//getting the item at particular position
item.setQuantity(item.getQuantity()-1);//decrementing item quantity by 1
itemList.set(position,item);// updating the itemList to that new item with decremented quantity
notifyDataSetChanged();//notifying the adapter
}
});
This will surely work. Also it is a good practice to maintain a single list instead of having multiple arrays when dealing with custom adapters for ListView/RecyclerView.
You have to just call notifyDataSetChanged(); after add and sub button click code.
For Add
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
quant[position]=quant[position]+1;
count1 +=1;
//holder.number.setText(Integer.toString(quant[position]));
holder.number.setText(count1.toString());
Toast.makeText(context, Integer.toString(quant[position]), Toast.LENGTH_SHORT).show();
Log.d("Quantity",Integer.toString(quant[position]));
positiveNumbers.put(holder.uniqueKey,count); //Key -> String.valueOf(position) and Value -> int count
notifyDataSetChanged();
}
});
For Sub
sub.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
quant[position]--;
Toast.makeText(context, Integer.toString(quant[position]), Toast.LENGTH_SHORT).show();
holder.number.setText(Integer.toString(quant[position]));
positiveNumbers.put(holder.uniqueKey,count); //Key -> String.valueOf(position) and Value -> int count
notifyDataSetChanged();
}
});
Hope it's help !!