Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
432 views
in Technique[技术] by (71.8m points)

android - Unable to check/uncheck CheckedTextView inside getView

I'm loading phone contacts in a custom ListView. Each row is a checkable LinearLayout containing a CheckedTextView and another TextView.

I'm feeding the list view with a custom ArrayAdapter. My problem is that I can't control CheckedTextViews inside getView(). For example when I try the following

public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        if(row == null){            
            row = inflater.inflate(layout, parent, false);
        }

        CheckedTextView checkedTextView =  (CheckedTextView) row.findViewById(R.id.checkedTextView);
        checkedTextView.setText("A");
        checkedTextView.setChecked(true);
        return row;
    }

That's supposed to check every text view whenever I scroll the list view, but that's not happening. Can anybody tell me how to do it?

EDIT: It's important to check it inside getView(), I can't just check all after setListAdapter()

EDIT2: This is the xml file showing the view of each row

<?xml version="1.0" encoding="utf-8"?>
<com.example.multiplecontacts.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:paddingBottom="0dp"
        android:text="CheckedTextView"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/subTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:paddingTop="0dp"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</com.example.multiplecontacts.CheckableLinearLayout>

CheckableLinearLayout is a custom layout that extends LinearLayout and implements Checkable as I said before. And I've taken it from here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Did you set a checkMark property for your CheckedTextView in your layout xml?

For example: android:checkMark="?android:attr/listChoiceIndicatorMultiple

CheckedTextView is not just a checkbox with a text. You must also note, that CheckedTextView is not focusable, or clickable without some manipulation (since it was designed for ListView and therefore it's state must be controlled by ListView's setOnItemClickListener)

setChoiceMode should be set for a ListView. And checking of the row inside adapter's getView should be done via: listView.setItemChecked(position, value)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...