Android: How can I check a particular item in a Checked ListView?

不打扰是莪最后的温柔 提交于 2019-12-21 08:14:30

问题


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"
    />

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);

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?


回答1:


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



来源:https://stackoverflow.com/questions/3904647/android-how-can-i-check-a-particular-item-in-a-checked-listview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!