Android:如何在Checked ListView中检查特定项目? [英] Android: How can I check a particular item in a Checked ListView?
问题描述
我正在使用ListView,一次只能检查一项. 这是我的自定义list_row.xml:
I am using a ListView in which only one item can be checked at a time. This is my custom list_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="20sp"
android:checkMark="?android:attr/listChoiceIndicatorSingle"
/>
我使用普通的数组适配器填充onCreate()中的列表:
I populate the list in onCreate() using a normal array adapter:
ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.list_row, strings);
myList.setAdapter(myAdapter);
显示列表时,我想让列表中的第5个项目显示为已选中".我该怎么做呢?我知道CheckedTextView有一个名为setChecked()的函数,但是如何从列表中获取我的第5个项目以对该函数应用呢?
When the list is displayed, I want to have, say, the 5th item in the list appear as Checked. How can I go about doing this? I know CheckedTextView has a function called setChecked(), but how can I get my 5th item from the list to apply this function on it?
推荐答案
在StackOverflow中引用另一个答案,我发现最简单的方法是使用
Referring another answer in StackOverflow, I have found that the simplest way to achieve this is using
myList.setItemChecked(pos, true);
在这里您可以找到实际的线程:链接
Here you can find the actual thread : link
这篇关于Android:如何在Checked ListView中检查特定项目?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!