Listview duplicates item every 6 times

偶尔善良 提交于 2019-12-01 18:35:09

I see the problem now. And no, there is no duplication here. Carry out these few changes:

In your static class ListViewItem, add boolean isChecked;

static class ListViewItem{
    public String ItemTitle;
    public int price;
    public String Description;
    public TextView title;
    public TextView pricetitle;
    public TextView Descriptiontitle;
    public CheckBox cb;
    public Spinner sp;
    public boolean isChecked;    // <--- added
}

Change the initialization of items:

items.add(new ListViewItem(){{
        ItemTitle = "Starter Title";
        Description= "Your description goes here";
        price=i;
        isChecked = false;
    }});

In the getView() method, after holder.Descriptiontitle.setText(item.Description);, add:

holder.cb.setOnClickListener(new OnClickListener() {

  @Override
  public void onClick(View v) {

    if (((CheckBox) v).isChecked()) {
                item.isChecked = true;
    } else {
                item.isChecked = false;
            } 
  }
});


if (item.isChecked) {
    holder.cb.setChecked(true);
} else {
    holder.cb.setChecked(false);
}

Add the final keyword to ListViewItem item = items.get(position);:

final ListViewItem item = items.get(position);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!