Android - 从列表视图中删除多个项目

时间:2015-04-15 09:29:50

标签: android listview android-listview items

解决问题:

对于未来有这个问题的人,我发现这个很好的教程:http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/

并重新调整它以供我自己使用。我没有将MultiChoiceModeListener与ActionBar一起使用,我使用了原始的OnClickListener。

此外,我无法从xml突出显示工作,所以我保留了我原来的突出显示代码。删除项目后,颜色没有消失,因此我最终遍历所有View并在每次删除后将它们设置为透明。不是最有效的方式,但它有效。

原始问题:

我一直在尝试通过使用ListView ListView单击它们来选择多个项目,然后通过单击按钮进行删除。我已经制作了一个数组适配器,所以我可以格式化我的项目。但问题是每当我删除一个项目时,唯一被删除的是我列表末尾的项目。

我看过其他有类似问题的人,但遗憾的是无济于事。我很难过,需要一些帮助。

My Fragment使用ListView:

public class SellEquipment extends Fragment {
ListView listview;
String[] itemname ={
         "Safari",
         "Camera",
         "Global",
         "World",
         "Test"
         };

ArrayList<String> list;
private EquipmentViewAdapter adapter;
private ArrayList<Integer> sellList;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    View view = inflater.inflate(R.layout.sellequipment_activity, container, false);
    list = new ArrayList<String>();
    for (String item : itemname) {
        list.add(item);
    }


    sellList = new ArrayList<Integer>();
    adapter = new EquipmentViewAdapter(getActivity(), itemname, list);

    Button sellBtn = (Button) view.findViewById(R.id.sellBtn);

    sellBtn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
        /*  SparseBooleanArray itemPos = listview.getCheckedItemPositions();
            int itemCount = listview.getCount();
            for (int i = itemCount - 1; i >= 0; i--) {
                if (itemPos.get(i)) {
                    adapter.remove(list.get(i));
                }
            }
            listview.clearChoices();
            adapter.notifyDataSetChanged();*/

            for (int position : sellList) {
                list.remove(position);
            }
            sellList.clear();
            adapter.notifyDataSetChanged();
            listview.clearChoices();
        }
    });
    listview = (ListView) view.findViewById(R.id.list);
    listview.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
            //String SelectedItem = itemname[position];
            if (!sellList.contains(position)) {
                listview.setItemChecked(position, true);
                view.setBackgroundColor(Color.BLUE);
                sellList.add(position);
            } else {
                listview.setItemChecked(position, false);
                view.setBackgroundColor(Color.TRANSPARENT);
                sellList.remove(Integer.valueOf(position));
            }
            //list.remove(position);
            //adapter.notifyDataSetChanged();
            //Toast.makeText(getActivity().  getApplicationContext(), SelectedItem, Toast.LENGTH_SHORT).show();
        }
    });
    return view;
}  
}

这是我的适配器:

public class EquipmentViewAdapter extends ArrayAdapter<String> {

private final Activity context;
private final String[] itemname;
private final List<String> list;

public EquipmentViewAdapter(Activity context, String[] itemname, ArrayList<String> list) {
    super(context, R.layout.mylist, list);
    // TODO Auto-generated constructor stub

    this.context = context;
    this.itemname = itemname;
    this.list = list;
}
@Override
public View getView(int position,View view,ViewGroup parent) {
    View rowView = view;

    if (rowView == null) {
        LayoutInflater inflater = context.getLayoutInflater();
        rowView = inflater.inflate(R.layout.mylist, null);
    }

    TextView txtTitle = (TextView) rowView.findViewById(R.id.item);
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
    TextView lvl = (TextView) rowView.findViewById(R.id.textView1);
    TextView spd = (TextView) rowView.findViewById(R.id.textView2);
    TextView rch = (TextView) rowView.findViewById(R.id.textView3);

    txtTitle.setText(itemname[position]);
    //@todo get data from array list or 2D array
    imageView.setImageResource(R.drawable.ic_launcher); // have the pictured ordered correctly
    lvl.setText("lvl: " + itemname[position]);
    spd.setText("spd: " + itemname[position]);
    rch.setText("rch: " + itemname[position]);
    return rowView;
}
}

任何帮助都会非常感激,我真的会把头发拉出来! 谢谢!

3 个答案:

答案 0 :(得分:1)

更新:

sellList更改为包含String s

private ArrayList<String> sellList;

@Override
public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
    String item = adapter.getItem( position );
    if (!sellList.contains( item )) {
        listview.setItemChecked(position, true);
        view.setBackgroundColor(Color.BLUE);
        sellList.add( item );
    } else {
        listview.setItemChecked(position, false);
        view.setBackgroundColor(Color.TRANSPARENT);
        sellList.remove( item );
    }
}

然后

        for (String item : sellList) {
            adapter.remove( item );
        }
        sellList.clear();
        adapter.notifyDataSetChanged();
        listview.clearChoices();

答案 1 :(得分:0)

您删除的列表不是适配器正在使用的列表。将list传递到适配器时,会复制它。您需要从适配器正在使用的列表中删除。

答案 2 :(得分:0)

在 onActionItemClicked 和 switch 方法中:

case R.id.selectAll:
    for (int i = 0; i < listView.getAdapter().getCount(); i++) {
       customAdapter.selectAll(i);
       listView.setItemChecked(i, true);
                            }

并在您的适配器中创建:

public void selectAll(int i) {
  mSelectedItemsIds.put(i, false);
  }