Android ListView::setItemChecked 不起作用 [英] Android ListView::setItemChecked doesn't work

查看:51
本文介绍了Android ListView::setItemChecked 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示简单的检查列表,但我需要检查一些项目.

I tried to show simple checked list and I need some items to be checked.

这是我的代码

ArrayAdaptertaskAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_checked, tasksList);this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);this.getListView().setItemChecked(2, true);setListAdapter(taskAdapter);

<ListView
   android:id="@android:id/list"
   android:layout_width="match_parent"
   android:layout_height="wrap_content">

还是不行.Checkable 接口的实现没有帮助.

And still it doesn't work. Implementation of Checkable interface didn't help.

这个 ListView 有什么技巧?

What's the trick of this ListView?

推荐答案

您需要先设置适配器,然后才能将项目设置为可检查.

You need to set the adapter before setting the item as checkable.

ArrayAdapter<Task> taskAdapter = new ArrayAdapter<Task>(this, android.R.layout.simple_list_item_checked, tasksList);
setListAdapter(taskAdapter);        
this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
this.getListView().setItemChecked(2, true); 

适配器包含存储在列表视图中的数据,因此在设置适配器之前,列表视图中不存在项目 2.

The adapter contains the data stored in the listview so Item 2 does not exist in the listview until the adapter is set.

这篇关于Android ListView::setItemChecked 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆