Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, if you want to change the quantity value, you should have a variable representing quantity which you can change. You have included quantity value in the description text, which is bad design. What you should do is have a quantity variable <code>int</code> in the <code>RowItem</code> class as shown. You <code>desc</code> should just return a string from the quantity value as shown:</p> <pre><code>public class RowItem { private int imageId; private String title; private int quantity = 0; public RowItem(int imageId, String title, int quantity) { this.imageId = imageId; this.title = title; this.quantity = quantity; } public int getImageId() { return imageId; } public void setImageId(int imageId) { this.imageId = imageId; } public String getDesc() { return "Select " + quantity + " Item"; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity = quantity; } @Override public String toString() { return title + "\n" + quantity; } } </code></pre> <p>In your activity, instead of storing descriptions, store quantities:</p> <pre><code>public static final int[] quantities = new int[]{ 0 }; </code></pre> <p>In <code>onCreate</code> initialize the rowItems as follows</p> <pre><code>rowItems = new ArrayList&lt;RowItem&gt;(); for (int i = 0; i &lt; titles.length; i++) { RowItem item = new RowItem(images[i], titles[i], quantities[0]); rowItems.add(item); } </code></pre> <p>Finally, in your <code>OnClickListener</code>, get the quantity value, update it and set the new description.</p> <pre><code>final ViewHolder viewHolderFinal = holder; final RowItem finalRowItem = rowItem; holder.add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int quantity = finalRowItem.getQuantity(); // get the quantity for this row item finalRowItem.setQuantity(quantity + 1); // update it by adding 1 viewHolderFinal.txtDesc.setText(finalRowItem.getDesc()); // set the new description (that uses the updated qunatity) } }); holder.sub.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { int quantity = finalRowItem.getQuantity(); // get the quantity for this row item finalRowItem.setQuantity(quantity - 1); // update it by subtracting 1 viewHolderFinal.txtDesc.setText(finalRowItem.getDesc()); // set the new description (that uses the updated qunatity) } }); </code></pre> <p>*<em>Note: *</em> I have compiled and run this code. So if you do everything as I mentioned it will work.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload