Android-从列表视图中删除多个项目 [英] Android - Removing Multiple Items From List View

查看:107
本文介绍了Android-从列表视图中删除多个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已解决的问题:

对于以后遇到此问题的任何人,我都找到了这个不错的教程:

For anyone in the future who has this problem, I found this this nice tutorial: http://www.androidbegin.com/tutorial/android-delete-multiple-selected-items-listview-tutorial/

,然后将其重新适应我的用途.我没有将MultiChoiceModeListener与ActionBar一起使用,而是使用了原始的OnClickListener.

and re-adapted it to my own use. I didn't use the MultiChoiceModeListener with the ActionBar, I used my original OnClickListener.

我也无法从xml中突出显示它们,因此我保留了原始的突出显示代码.删除项目后颜色没有消失是有问题的,因此我最终遍历了所有视图,并在每次删除后将它们设置为透明.不是最有效的方法,但是它可以工作.

Also I couldn't get their highlighting from the xml to work so I kept my original highlighting code. There was a problem with the color not disappearing after I removed the items so I ended up iterating through all of my View's and setting them to transparent after each removal. Not the most efficient way, but it works.

原始问题:

我一直试图通过使用ListView ListView单击多个项目来选择多个项目,然后通过单击一个按钮将其删除.我已经制作了一个阵列适配器,以便可以格式化我的项目.但是问题是无论何时我删除一个或多个项目,唯一被删除的都是列表末尾的项目.

I've been trying to select multiple items by clicking them with a ListView ListView and then delete by clicking a button. I've made a array adapter so I can format my items. However the problem is whenever I delete an item(s), the only ones that gets deleted are the items in the end of my list.

我看过其他有类似问题的人,但不幸的是没有结果.我很尴尬,需要帮助.

I've looked at other people who had similar problems, but unfortunately to no avail. I'm stump and need some help.

使用ListView的我的片段:

My Fragment that uses the 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;
}
}

任何帮助将不胜感激,我真的是在拉扯我的头发!谢谢!

Any help would be greatly appreciated, I'm literally pulling my hair out on this! Thank you!

推荐答案

更新:

更改您的 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();

这篇关于Android-从列表视图中删除多个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆