问题描述
我想为我的Android应用程序创建一个报警系统,和我有一个列表视图
中,我加入alamrs。这列表视图
适配器是一个 simple_list_item_multiple_choice
。我想,当我加入这个显示为列表视图
检查,但我还没有找到该解决方案报警。
I am trying to create an alarm system for my android app, and I have a listview
in which I am adding the alamrs. This listview
adapter is an simple_list_item_multiple_choice
. I want that when I add an alarm this appears as checked in the listview
, but I have not found the solution for that.
这code不工作如我所料:
This code is not working as I expected:
{
ListView list = (ListView)findViewById(R.id.listView1);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, this.alarmList);
list.setAdapter(adapter);
list.setItemChecked(itemPos, true);
}
你知道还有什么可以我尝试?
Do you know what else could I try?
先谢谢了。
推荐答案
多德试试这个code我希望这是你的意思是:
Dude try this code i hope that is that you mean:
final SparseBooleanArray mSelectedItemsIds = new SparseBooleanArray();
final ListView list = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, this.alarmList);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
list.setAdapter(adapter );
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//SparseBooleanArray Checked = list.getCheckedItemPositions();
if(!mSelectedItemsIds.get(position)){
list.setItemChecked(position, true);
mSelectedItemsIds.put(position, true);
}
else{
mSelectedItemsIds.delete(position);
list.setItemChecked(position, false);
}
}
});
编辑:我设置列表作为最后的,一定要做到这一点,如果你还剪掉,否则它不会工作。
i set the list as final, make sure you do it also if you didnt otherwise it wont work
这篇关于当项目在android的列表视图中创建托运的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!