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

android listview onclicklistener

36 观看


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:

public class MainActivity extends AppCompatActivity  implements MyAd.customButtonListener {

    ListView lv;

int q=0;
    protected void onCreate(Bundle savedInstanceState) {

    lv= (ListView) findViewById(;
       MyAd ma=new MyAd(this);


    public void onButtonClickListner(int position,TextView t) {

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;

    public int getCount() {
        return li.size();

    public Object getItem(int i) {
        return i;

    public long getItemId(int i) {
        return i;

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

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

        ListItem sr = li.get(position);
//String s=sr.title;
 View.OnClickListener() {
            public void onClick(View view) {
                q = a[position];


                a[position] = q;
                System.out.println("position:" + position + "-" + q);
                System.out.println("array: " + Arrays.toString(a));

        return row;

    class MyViewHolder {
        TextView txt, quan;
        Button bt;

        MyViewHolder(View row) {
            txt = row.findViewById(;
            quan = row.findViewById(;
            bt = row.findViewById(;


public class ListItem {

    String title;

    ListItem(String title)

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

回应 (1)


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 发布者: 15.09.2017 07:14