如何反映各个TextView

时间:2017-09-15 04:44:14

标签: android listview onclicklistener

我正在开发一个应用程序,其中有一个ListViewin,ListView的每一行都有一个Button和一个TextView。我希望增加值并在按钮单击的相应行中的TextView中显示它,但是当我单击按钮时,它会为每行单独递增值,但显示最后一行的TextView中的更改,而不管单击的按钮是什么任何一行。如何在同一行的TextView

中显示特定行的更改

这是我的代码:

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


}

1 个答案:

答案 0 :(得分:0)

删除实例变量

MyViewHolder mvh;

并在getView() -

中声明为局部变量
final MyViewHolder mvh;

由于实例变量,mvh始终保持最后一个膨胀的视图持有者,因此,最后一个可见项始终更新。