set text in textview of a list item on click of button

前端 未结 3 460
北恋
北恋 2020-12-22 01:53

I have created a list view as shown in this image.Link:

Image

Problem: I want that if the user clicks on the button + or

3条回答
  •  执笔经年
    2020-12-22 02:56

    I know its very late but i thought this may help other who have similar problem

    Problem: I want that if the user clicks on the button + or - the textview which is currently showing 0 is incremented or decremented accordingly. But if I click first item's button the textview is updated in some other row. I don't know how to implement this.

    look at your code

    ViewHolder holder = (ViewHolder) row.getTag();
        holder.imageView.setImageResource(images[position]);
        holder.textView.setText(names[position]);
        add.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                count=count+1;
                number.setText(count.toString());
            }
        });
    
        sub.setOnClickListener(new OnClickListener() {
    
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
    
                count=count-1;
                number.setText(count.toString());
            }
        });
        return row;
    

    in your ViewHolder you have set values for holder.imageview and holder.textview which is for name i think, but you forgot to set value for holder.number and you are only changing its value in onClick method. so when the adapter creates its view it do not find any specific value that is why it uses the previously used view to inflate the data.

    Solution:- just use a separate list to store the number with unique key (use adapter position as key) and check if the list has that key that means you previously changed the value so get the value from list and set it on textview if the key is not present that means you never changed the value so set textview with 0

提交回复
热议问题