【问题标题】:How to deselect selected List View items?如何取消选择选定的列表视图项目?
【发布时间】:2015-12-03 21:25:46
【问题描述】:

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

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


                }
            });

【问题讨论】:

    标签: android android-listview custom-adapter


    【解决方案1】:

    你试过了吗?

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

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

    【讨论】:

      【解决方案2】:
      myListView.clearChoices();
      myListView.requestLayout();
      

      【讨论】:

        【解决方案3】:

        在你的适配器中使用一个列表来存储选定的对象。如果它已经存在然后删除它。还有一点你不能从数组中删除元素,所以使用列表。

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

        【讨论】:

        • 我认为你应该使用 ArrayList 而不是数组,因为你不能从数组中删除元素。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-01-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-11-23
        • 1970-01-01
        相关资源
        最近更新 更多