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 is my adapter class
public class ItemAdapter extends ArrayAdapter<String>{
Context context;
int[] images,rate,quant;
String[] names;
ImageButton add, sub;
ViewHolder holder; // Since will be accessed inside onclick
Integer count1=0;
HashMap<String,Integer> positiveNumbers = new HashMap<String,Integer>();
Integer count=0;
static class ViewHolder {
public TextView textView;
public ImageView imageView;
public TextView number;
public TextView rates;
public String uniqueKey;
}
public ItemAdapter(Context c, String[] items, int quantity[], int[] rate, int[] img) {
// TODO Auto-generated constructor stub
super(c, R.layout.item_fooditems, R.id.tvitemname, items);
this.context = c;
this.images = img;
this.names = items;
this.rate=rate;
this.quant=quantity;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View row = convertView;
// reuse views
if (row == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.item_fooditems, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) row.findViewById(R.id.ivimage);
viewHolder.textView = (TextView) row.findViewById(R.id.tvitemname);
viewHolder.number = (TextView)row.findViewById(R.id.tvquantity);
viewHolder.rates = (TextView)row.findViewById(R.id.tvprice);
viewHolder.uniqueKey = String.valueOf(position);
add = (ImageButton) row.findViewById(R.id.bincrement);
add.setTag(position);
sub = (ImageButton) row.findViewById(R.id.bdecrement);
sub.setTag(position);
row.setTag(viewHolder);
}
holder = (ViewHolder) row.getTag(); //keeping one global memory
holder.imageView.setImageResource(images[position]);
holder.textView.setText(names[position]);
holder.rates.setText(Integer.toString(rate[position]));
holder.number.setText(Integer.toString(quant[position]));
holder.number.setText("x");
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
}
});
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
}
});
return row;
}
public HashMap<String, Integer> getPositiveNumbers()
{
return positiveNumbers;
}
}
What modifications are required?
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.
And now pass this modal class to you adapter constructor.And this is how you can set value in your getView()
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.
Same you can do for the your subtract quantity onClickListener().
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
For Sub
Hope it's help !!