如何在按钮单击上更改列表项中的textView

时间:2015-09-12 13:15:28

标签: java android listview button onclicklistener

我想更改数量textview以反映特定商品的数量。但是在单击add和sub按钮时,setText()函数似乎不起作用。这是我的适配器类

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;
     }


}

需要进行哪些修改?

2 个答案:

答案 0 :(得分:1)

首先,您应该使用所需的所有值创建自定义模态类,而不是采用这些单独的数组。例如,在您的情况下,它将是这样的。

    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;
    }
}

现在将这个模态类传递给你的适配器构造函数。这就是你在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()));

现在进入您的添加/子点击监听器,无需在添加/子点击监听器内设置数量TextView的文本。您可以在更新itemList的相应索引处的数量值后通知您的适配器。例如。为了增加数量,你可以做这样的事情。

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
        } 
    }); 

您可以为减去数量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
        } 
    }); 

这肯定有用。在处理ListView / RecyclerView的自定义适配器时,最好还是维护单个列表而不是多个数组。

答案 1 :(得分:0)

您必须在添加按钮点击代码后调用 notifyDataSetChanged();

添加

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();
            } 
        }); 

对于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();
            } 
        });

希望它有所帮助!!