如何取消选择所选的列表视图项?

时间:2015-09-08 09:07:46

标签: android android-listview custom-adapter

在我的应用程序中,可以选择从列表视图中选择不同的项目。所选项目将存储到一个数组中,后台将设置为listview中的选定项目。哪个工作正常。问题是我想要取消选择项目,如果我以前选择它。也想要从所选项目的数组中删除它。我正在寻找它几个小时。请帮助我。谢谢你提前。

    list = (ListView) findViewById(R.id.list);
            // list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

            adapter=new LocationListAdapter(mActivity, locationList);

            list.setAdapter(adapter);
            selectedlocations = new String[locationList.size()];

            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub


                    if (selected != null) {

    //                  selected.setBackgroundResource(R.drawable.list_item_bg_pressed);
                    }

                    selected = view;
                    selected.setBackgroundResource(R.drawable.list_item_bg_pressed);

                    selecteditem = position;
                    selectedlocations[position] = locationList.get(selecteditem)
                            .getId();
                    ((LocationListAdapter)list.getAdapter()).toggleSelected(new Integer(position));

                    System.out.println("Selected position:" + selecteditem + ","
                            + selectedlocations[position]);


                }
            });

3 个答案:

答案 0 :(得分:0)

你试过这个吗?

myListView.clearChoices();
myAdapter.notifyDataSetChanged();

这将清除您所做的所有选择。

答案 1 :(得分:0)

myListView.clearChoices();
myListView.requestLayout();

答案 2 :(得分:0)

你的适配器中的

使用一个列表来存储选定的对象。如果它已经存在,那么删除它。还有一件事你不能从数组中删除元素,所以使用List。

list = (ListView) findViewById(R.id.list);

            selectedlocations = new ArrayList<String>();

            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // TODO Auto-generated method stub


                    if (selected != null) {

    //                  selected.setBackgroundResource(R.drawable.list_item_bg_pressed);
                    }

                    selected = view;
                    selected.setBackgroundResource(R.drawable.list_item_bg_pressed);

                    selecteditem = position;
                    String item = ""+locationList.get(selecteditem).getId();
                    if(selectedlocations.contains(item)){
                     selectedlocations.remove(item);
                    }else{
                    selectedlocations.add(item);
                    }

                    ((LocationListAdapter)list.getAdapter()).toggleSelected(new Integer(position));

                    System.out.println("Selected position:" + selecteditem + ","
                            + selectedlocations[position]);


                }
            });