从多个选择ListView的返回值 [英] Returning values from multiple selection ListView

查看:135
本文介绍了从多个选择ListView的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑:好吧,我找到了解决办法。不知道这是正确的解决方案,但它工作正常。添加到下面的code。

我试图让用户从清单中选择一个号码目录,并在单击提交按钮返回它们。下面是我的code片段。它填充与/ SD卡/所有目录的ListView控件,并初步选择时,我提交(然而很多我挑),日志显示了正确的选择返回。不过,如果我取消的项目,然后单击提交再次,它仍然显示,如果所有的被选中。我是否需要写一个处理程序,取消的项目?我认为这是由choiceMode选择的照顾?谢谢!

 私人SparseBooleanArray一个;
directoryList.setAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_list_item_multiple_choice,directoryArray));
    提交按钮=(按钮)findViewById(R.id.submit_button);
    submitButton.setOnClickListener(新OnClickListener()
        {
        @覆盖
        公共无效的onClick(视图v)
        {
            一个新的= SparseBooleanArray();
            a.clear();
            一个= directoryList.getCheckedItemPositions();

            的for(int i = 0; I< a.size();我++)
            {
                //添加if语句来检查正确的。该SparseBooleanArray
                //似乎保持了检查项目的关键,但它集
                //值设置为false。添加一个布尔检查返回正确的结果。
                如果(a.valueAt(一)==真)
                    Log.v(返回的,directoryArray [a.keyAt(ⅰ)]);

            }
        }
    });
 

解决方案

做了一些更多的调试,发现为我工作的解决方案。上述编辑成code。出于某种原因,SparseBooleanArray不空本身;它保持已检查过的框的键。当getCheckedItemPositions()被调用,但是,它将该值设置为false。所以关键还是在返回的数组中,但它有一个false值。只有检查框将被打上true值。

Edit: Okay, I found a solution. Don't know that it's the proper solution, but it does work correctly. Added to the code below.

I'm trying to allow a user to select a number of directories from a checklist, and return them upon clicking a "Submit" button. Here's a snippet of my code. It populates the ListView with all the directories on /sdcard/, and for the initial selection (of however many I pick) when I submit, the log shows the correct choices returned. However, if I uncheck an item, and click "Submit" again, it still shows as if all are selected. Do I need to write a handler to uncheck an item? I thought that was taken care of by the choiceMode selection? Thanks!

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

            }                
        }
    });

解决方案

Did some more debugging and found a solution that worked for me. Edited into code above. For some reason, the SparseBooleanArray doesn't empty itself; it maintains the keys of the boxes that have been checked. When getCheckedItemPositions() is called, however, it sets the VALUE to false. So the key is still in the returned array, but it has a value of false. Only the checked boxes will be marked with a value of true.

这篇关于从多个选择ListView的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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