我有一个包含checkedtextview的listview。我的应用程序从列表视图的顶部移动到底部。我想在调用操作之前检查项目是否被选中。如果未选中,我想移动到列表中的下一项。
例如。
项目1-已勾选
项目2-已勾选
项目3-未勾选
第4项-已选中
因此,我希望应用程序按如下方式处理:
项目1
项目2
第四项。
我不确定如何从listview位置访问项目的选中状态。
我想要的逻辑如下:
Is Current Item checked?
Yes:
Call action
No:
Move to next item.
Reloop to top of void.我需要一些东西来阻止无限循环。
发布于 2012-09-08 04:51:45
一种解决方案是使用位置的ArrayList。当用户选中/取消选中某个checkbox时,请相应地在您的ArrayList中添加/删除该位置。然后,当用户结束时,只需遍历列表以了解选择了哪个位置。
发布于 2012-09-08 05:55:31
1.)首先创建一个数组,表示适配器中的项处于选中状态
(假设您为此扩展了BaseAdapter类):
private boolean [] itemsChecked = new boolean [getCount()];2.)然后创建一个OnCheckedChangeListener
private OnCheckedChangeListener listener = new OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton button, boolean checked)
{
Integer index = (Integer)button.getTag();
itemsChecked[index] = checked;
}
}3.)在适配器的getView()方法中使用:
public View getView(int index, View view, ViewGroup parent)
{
/*...*/
CheckBox checkBox = /*get the checkbox*/;
checkbox.setTag(index);
checkBox.setOnCheckedChangeListener(listener);
/*...*/
}4.) onClick()方法中的:
public void onClick(View view)
{
//just get the boolean array somehow
boolean [] itemsChecked = adapter.getItemsCheckedArray();
for(int i=0; i<itemsChecked.length; i++)
{
if(itemsChecked[i])
{
//the i th item was checked
}
else
{
//it isnt checked
}
}
}https://stackoverflow.com/questions/12325153
复制相似问题