How to reflect the changed values of each row in respective TextViews

android listview onclicklistener

36 观看

1回复

14 作者的声誉

I am developing an application in which there is a ListViewin which there is a Button and a TextView in each row of the ListView. I want to increment value and show it in TextView in respective row on Button click but when I am clicking button it is incrementing the value separately for each row but showing the changes in the last row's TextView irrespective of the button clicked in any row. How to show the changes of a particular row in same row's TextView

Here is my code:

MainActivity.java

public class MainActivity extends AppCompatActivity  implements MyAd.customButtonListener {


    ListView lv;

int q=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

    lv= (ListView) findViewById(R.id.list);
       MyAd ma=new MyAd(this);
        ma.setCustomButtonListner(this);
        lv.setAdapter(ma);

    }


    @Override
    public void onButtonClickListner(int position,TextView t) {
        q=q+1;
       t.setText(""+q);
    }
}

MyAd.java

class MyAd extends BaseAdapter {
    int q = 0, a[];
    List<ListItem> li = new ArrayList<>();
    Context c;
    MyViewHolder mvh;
    customButtonListener customListner;


    MyAd(Context c) {
        this.c = c;
        Resources res = c.getResources();
        String title[] = res.getStringArray(R.array.title);
        a = new int[title.length];


        for (int i = 0; i < title.length; i++) {
            li.add(new ListItem(title[i]));
        }
    }


    public interface customButtonListener {
        void onButtonClickListner(int position, TextView t);
    }


    public void setCustomButtonListner(customButtonListener listener) {
        this.customListner = listener;
    }


    @Override
    public int getCount() {
        return li.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public View getView(final int position, View view, ViewGroup parent) {
        View row = view;


        if (row == null) {

            LayoutInflater inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row = inflater.inflate(R.layout.list_item, parent, false);
            mvh = new MyViewHolder(row);

            row.setTag(mvh);
        } else {
            mvh = (MyViewHolder) row.getTag();
        }


        ListItem sr = li.get(position);
//String s=sr.title;
        mvh.txt.setText(sr.title);


        mvh.bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                q = a[position];

                q++;

                a[position] = q;
                mvh.quan.setText(String.valueOf(a[position]));
                System.out.println("position:" + position + "-" + q);
                System.out.println("array: " + Arrays.toString(a));
            }
        });

        notifyDataSetChanged();
        return row;
    }


    class MyViewHolder {
        TextView txt, quan;
        Button bt;

        MyViewHolder(View row) {
            txt = row.findViewById(R.id.txt);
            quan = row.findViewById(R.id.quan);
            bt = row.findViewById(R.id.button);
        }
    }

}

ListItem.java

public class ListItem {

    String title;

    ListItem(String title)
    {
        this.title=title;
    }


}
作者: Ankit Srivastava 的来源 发布者: 2017 年 9 月 15 日

回应 1


0

250 作者的声誉

Remove as instance variable

MyViewHolder mvh;

and declare as local variable inside getView() -

final MyViewHolder mvh;

Due to instance variable, mvh always holds last inflated view holder and therefore, last visible item is always updated.

作者: nomag 发布者: 2017 年 9 月 15 日
32x32