如何在我的列表中设置 simple_list_item_multiple_choice 中的 checked="checked" 项目,如果我希望始终检查 2 个元素?
how to set checked="checked" item in simple_list_item_multiple_choice in my list, if I want that 2 element was always checked?
private ListView LVmain;
private ArrayList<String> tasks = new ArrayList<String>();
private ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, tasks);
tasks.add("first");
tasks.add("second");
tasks.add("thirst");
tasks.add("fourth");
adapter.notifyDataSetChanged();
}
所以我希望在启动程序时已经检查了第二项
无论您将适配器分配给您的 ListView
,试试这个:
LVmain.setItemChecked(1, true);
LVmain.setSelection(1);
请注意,我们正在传递 1
来检查第二项,因为列表视图位置是从 0 开始的。
private ListView LVmain;
private ArrayList<String> tasks = new ArrayList<String>();
private ArrayAdapter<String> adapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_multiple_choice, tasks);
tasks.add("first");
tasks.add("second");
tasks.add("thirst");
tasks.add("fourth");
adapter.notifyDataSetChanged();
}
所以我希望在启动程序时已经检查了第二项
无论您将适配器分配给您的 ListView
,试试这个:
LVmain.setItemChecked(1, true);
LVmain.setSelection(1);
请注意,我们正在传递 1
来检查第二项,因为列表视图位置是从 0 开始的。