customAdapter 中的删除按钮总是删除最后一个元素

Delete button in customAdapter always removes last element

我不知道为什么如果我单击列表行中包含的删除按钮,总是会删除最后一项,onClick 删除功能 (deleteTag) 中的吐司消息显示要删除的正确项目。此外,删除的行不是永久性的——如果我旋转设备,列表又会变满。我真的不知道我做错了什么。

tagList (activity)

public class tagList extends AppCompatActivity {

    MyCustomAdapter myCustomAdapter;
    ListView tagLv;    

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag_list);

        //Generate list View from ArrayList
        displayListView();
    }


    private void displayListView() {

        //Array list of tags, Tag(id,name,checked)
        ArrayList<Tag> TagList = new ArrayList<Tag>();

        TagList.add(new Tag(1, "aaa", false));
        TagList.add(new Tag(2, "bbb", false));
        TagList.add(new Tag(3, "ccc", false));

        //my adapter for listview
        myCustomAdapter = new MyCustomAdapter(tagList.this,R.layout.activity_tag_list,TagList);

        tagLv = (ListView) findViewById(R.id.tagLv);
        tagLv.setAdapter(myCustomAdapter);

    }

    // onClick
    public void deleteTag(View view){

        Tag itemToRemove = (Tag) view.getTag(R.string.tagListTag); //tagListTag= 1

        myCustomAdapter.remove(itemToRemove);
        myCustomAdapter.notifyDataSetChanged();

        Toast.makeText(getApplicationContext(),"item removed: " +itemToRemove.getName(),Toast.LENGTH_SHORT).show();
    }
}

MyCustomAdapter

public class MyCustomAdapter extends ArrayAdapter<Tag>{

    private ArrayList<Tag> tagList;
    private LayoutInflater layoutInflater;
    private Context context;


    public MyCustomAdapter(Context context, int resource, List<Tag> objects) {
        super(context, resource, objects);

        this.layoutInflater = LayoutInflater.from(context);
        this.tagList = new ArrayList<Tag>();
        this.tagList.addAll(objects);
        this.context = context;
    }

    private class ViewHolder {
        TextView textName;
        CheckBox cbName;
        Button rmBtn;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {

        ViewHolder holder = null;

        if (convertView == null) {

            convertView = layoutInflater.inflate(R.layout.row_tag_item, null);

            holder = new ViewHolder();
            holder.textName = (TextView) convertView.findViewById(R.id.textName);
            holder.cbName = (CheckBox) convertView.findViewById(R.id.cbName);
            holder.rmBtn = (Button) convertView.findViewById(R.id.removeTagBtn);

            convertView.setTag(holder);

        }
        else {
            holder = (ViewHolder) convertView.getTag();
        }

        Tag myTag = tagList.get(position);

        holder.textName.setText(" #" +  myTag.getId());

        holder.cbName.setText(myTag.getName());
        holder.cbName.setChecked(myTag.getSelected());

        holder.rmBtn.setTag(R.string.id, myTag.getId()); // id for future delete from DB
        holder.rmBtn.setTag(R.string.tagListTag, myTag);

        return convertView;
    }
}

问题是您没有在 activity 和适配器中使用相同的列表。如果旋转它 activity 将再次创建,您将再次使用原始列表设置适配器。

解决方案:

public MyCustomAdapter(Context context, int resource, List<Tag> objects) {
        super(context, resource, objects);

        this.layoutInflater = LayoutInflater.from(context);
        this.tagList = objects
        this.context = context;
    }

在 activity 中:

 public class tagList extends AppCompatActivity {

    MyCustomAdapter myCustomAdapter;
    ListView tagLv;    
   //Array list of tags, Tag(id,name,checked)
    ArrayList<Tag> TagList = new ArrayList<Tag>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tag_list);

        //Generate list View from ArrayList
        displayListView();
    }


    private void displayListView() {
        TagList.add(new Tag(1, "aaa", false));
        TagList.add(new Tag(2, "bbb", false));
        TagList.add(new Tag(3, "ccc", false));

        //my adapter for listview
        myCustomAdapter = new MyCustomAdapter(tagList.this,R.layout.activity_tag_list,TagList);

        tagLv = (ListView) findViewById(R.id.tagLv);
        tagLv.setAdapter(myCustomAdapter);

    }

    // onClick
    public void deleteTag(View view){

        Tag itemToRemove = (Tag) view.getTag(R.string.tagListTag); //tagListTag= 1

        TagList.remove(itemToRemove);
        myCustomAdapter.notifyDataSetChanged();

        Toast.makeText(getApplicationContext(),"item removed: " +itemToRemove.getName(),Toast.LENGTH_SHORT).show();
    }
}

这将解决所有问题。