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!
我试图允许用户从检查表中选择一些目录,并在单击“提交”按钮时返回它们。这是我的代码片段。它用/sdcard/上的所有目录填充ListView,对于初始选择(无论我选择多少),日志显示返回的正确选项。但是,如果我取消选中一个项目,然后再次点击“Submit”,它仍然显示为所有的都被选中了。我是否需要编写一个处理程序来取消选中项?我还以为那是由选择器决定的呢?谢谢!
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 个解决方案
#1
4
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):
我知道您找到了一种适合您的解决方案,但是更干净、更简单的解决方案可能在大多数情况下都有效(我希望保留所选元素的所有id):
(in my ListActivity):
(在我ListActivity):
SparseBooleanArray selectedPos = getListView()
.getCheckedItemPositions();
ListAdapter lAdapter = getListAdapter();
List<Long> ids = new LinkedList<Long>();
for (int i = 0; i < lAdapter.getCount(); i++) {
if (selectedPos.get(i)) {
ids.add(lAdapter.getItemId(i));
}
}
#2
3
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.
做了更多的调试,找到了一个适合我的解决方案。编辑成上面的代码。由于某些原因,SparseBooleanArray并没有清空自己;它维护已选中的盒子的键。但是,当调用getCheckedItemPositions()时,它将值设置为false。所以键仍然在返回的数组中,但是它的值为false。只有复选框将被标记为true。
#3
1
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…
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
No need to use SparseBooleanArray choices = parent.getCheckedItemPositions();
不需要使用SparseBooleanArray选项= parent.getcheckeditemposition ();
StringBuilder
is enough for this.
StringBuilder就足够了。
#1
4
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):
我知道您找到了一种适合您的解决方案,但是更干净、更简单的解决方案可能在大多数情况下都有效(我希望保留所选元素的所有id):
(in my ListActivity):
(在我ListActivity):
SparseBooleanArray selectedPos = getListView()
.getCheckedItemPositions();
ListAdapter lAdapter = getListAdapter();
List<Long> ids = new LinkedList<Long>();
for (int i = 0; i < lAdapter.getCount(); i++) {
if (selectedPos.get(i)) {
ids.add(lAdapter.getItemId(i));
}
}
#2
3
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.
做了更多的调试,找到了一个适合我的解决方案。编辑成上面的代码。由于某些原因,SparseBooleanArray并没有清空自己;它维护已选中的盒子的键。但是,当调用getCheckedItemPositions()时,它将值设置为false。所以键仍然在返回的数组中,但是它的值为false。只有复选框将被标记为true。
#3
1
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…
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
No need to use SparseBooleanArray choices = parent.getCheckedItemPositions();
不需要使用SparseBooleanArray选项= parent.getcheckeditemposition ();
StringBuilder
is enough for this.
StringBuilder就足够了。