How to change textView in list item on Button clic

2020-04-20 12:04发布

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?

2条回答
你好瞎i
2楼-- · 2020-04-20 12:31

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.

查看更多
The star\"
3楼-- · 2020-04-20 12:44

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 !!

查看更多
登录 后发表回答