customAdapter中的删除按钮始终删除最后一个元素

时间:2016-02-13 12:34:20

标签: android listview custom-adapter

我不知道为什么如果我点击列表行中包含的删除按钮,总是删除最后一项,onClick删除功能(deleteTag)中的toast msg显示要删除的正确项目。删除的行也不是永久性的 - 如果我旋转设备列表再次满了。我真的不知道我做错了什么。

tagList(活动)

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

1 个答案:

答案 0 :(得分:0)

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

解决方案:

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

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

活动:

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

这将解决所有问题。