从多选ListView返回值
编辑:好的,我找到了解决方案。不知道这是正确的解决方案,但它确实可以正常工作。添加到下面的代码中。
我试图允许用户从清单中选择多个目录,并在单击“提交”按钮后返回它们。这是我的代码片段。它使用 /sdcard/ 上的所有目录填充 ListView,并且对于提交时的初始选择(无论我选择多少目录),日志都会显示返回的正确选择。但是,如果我取消选中某个项目,然后再次单击“提交”,它仍然显示好像所有项目都被选中。我需要编写一个处理程序来取消选中某个项目吗?我认为这是通过 choiceMode 选择来解决的?谢谢!
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)]);
}
}
});
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)]);
}
}
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
进行了更多调试并找到了适合我的解决方案。编辑成上面的代码。由于某种原因,SparseBooleanArray 本身并没有清空;它维护已检查的框的密钥。然而,当调用 getCheckedItemPositions() 时,它将 VALUE 设置为 false。所以键仍然在返回的数组中,但它的值为 false。只有选中的框才会标记为 true 值。
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.
我知道您找到了一个适合您的解决方案,但在大多数情况下可能有效的更干净、更简单的解决方案是这样的(我想保留所选元素的所有 id):(
在我的 ListActivity 中):
I know you found a solution that works for you, but the cleaner and simpler solution that will probably work most of the time is this (I want to persist all the ids of the elements selected):
(in my ListActivity):
并不是想以此作为答案,但我必须扩展您所做的多选操作。为什么你要为你的选择做一个字段变量?我刚刚做了本地 SparseBooleanArray...
didn't mean to do this as an answer but i had to expand on what you did to do multi select. Why did you do a field variable for your selections? i just did local SparseBooleanArray...
无需使用
SparseBooleanArray Choices = Parent.getCheckedItemPositions();
StringBuilder
就足够了。No need to use
SparseBooleanArray choices = parent.getCheckedItemPositions();
StringBuilder
is enough for this.