Android - Removing Multiple Items From List View

ぃ、小莉子 提交于 2019-12-24 02:52:43

问题


Problem Solved:

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/

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

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.

Original Question:

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.

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

And here's my adapter:

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!


回答1:


UPDATE:

change your sellList to contain Strings

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

then

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



回答2:


The list you delete from is not the one the adapter is using. When you pass list into your adapter it is copied. You need to delete from the list that the adapter is using.



来源:https://stackoverflow.com/questions/29646557/android-removing-multiple-items-from-list-view

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