代码之家  ›  专栏  ›  技术社区  ›  Kevin Coppock

从多个选择列表视图返回值

  •  10
  • Kevin Coppock  · 技术社区  · 14 年前

    编辑:好的,我找到了解决方案。不知道这是正确的解决方案,但它确实能正常工作。添加到下面的代码中。

    我试图允许用户从清单中选择多个目录,并在单击“提交”按钮时返回它们。这是我的代码片段。它用/sdcard/上的所有目录填充listview,对于提交时的初始选择(不管我选择了多少),日志显示返回的正确选择。但是,如果取消选中某个项目,然后再次单击“提交”,它仍然显示为“全部选中”。是否需要编写一个处理程序来取消选中某个项目?我以为是选歌节选的?谢谢!

    private SparseBooleanArray a;    
    directoryList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, directoryArray));
        submitButton = (Button)findViewById(R.id.submit_button);
        submitButton.setOnClickListener(new OnClickListener()
            {
            @Override
            public void onClick(View v)
            {
                a = new SparseBooleanArray();
                a.clear();
                a = directoryList.getCheckedItemPositions();
    
                for (int i = 0; i < a.size(); i++)
                {
                    //added if statement to check for true. The SparseBooleanArray
                    //seems to maintain the keys for the checked items, but it sets
                    //the value to false. Adding a boolean check returns the correct result.                    
                    if(a.valueAt(i) == true)
                        Log.v("Returned ", directoryArray[a.keyAt(i)]);
    
                }                
            }
        });
    
    4 回复  |  直到 6 年前
        1
  •  4
  •   ansjob    6 年前

    我知道您找到了一个适合您的解决方案,但大多数情况下可能工作的更干净、更简单的解决方案是这样的(我希望保留所选元素的所有ID):

    (在我的ListActivity中):

    SparseBooleanArray selectedPos = getListView()
            .getCheckedItemPositions();
    
    ListAdapter lAdapter = getListAdapter();
    List<Long> ids = new ArrayList<Long>();
    for (int i = 0; i < lAdapter.getCount(); i++) {
        if (selectedPos.get(i)) {
            ids.add(lAdapter.getItemId(i));
        }
    }
    
        2
  •  3
  •   Kevin Coppock    14 年前

    做了更多的调试,找到了一个适合我的解决方案。编辑成上面的代码。由于某种原因,SpareBooleanarray本身并没有清空;它维护已检查过的框的键。但是,当调用getcheckeditempositions()时,它会将值设置为false。因此键仍然在返回的数组中,但它的值为false。只有复选框才会标记为“真”。

        3
  •  1
  •   Kevin Coppock    14 年前

    我不是有意这样做作为答案,但我必须扩展你做的多选。为什么要为您的选择做一个字段变量?我刚做了当地的斯巴塞波列亚纳雷…

    public class NaughtyAndNice extends ListActivity {
    TextView selection;
    String[] items={"lorem","ipsum", "dolor", "sit", "amet",
            "consectetuer", "adipisc", "jklfe", "morbi", "vel",
            "ligula", "vitae", "carcu", "aliequet"};
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setListAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_multiple_choice,items));
               selection = (TextView)findViewById(R.id.selection);
        this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
    
    public void onListItemClick(ListView parent, View view, int position, long id){
        SparseBooleanArray choices = parent.getCheckedItemPositions();
        StringBuilder choicesString = new StringBuilder();
        for (int i = 0; i < choices.size(); i++)
        {
        //added if statement to check for true. The SparseBooleanArray
        //seems to maintain the keys for the checked items, but it sets
        //the value to false. Adding a boolean check returns the correct result.                    
            if(choices.valueAt(i) == true)
                choicesString.append(items[choices.keyAt(i)]).append(" ");
    
        } 
        selection.setText(choicesString);
    }
    }
    

        4
  •  1
  •   Kai    13 年前

    无需使用 SparseBooleanArray choices = parent.getCheckedItemPositions();

    StringBuilder 足够了。