Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - ListView with CHOICE_MODE_MULTIPLE using CheckedText in a custom view

There are plenty of questions of how to use CheckedTextView but I can't make it work correctly.

I have a CursorAdapter with a custom view which has a CheckedTextView with android:id="@android:id/text1". I have used android:id/text1 because there are different questions that mention that if you use it you will get the choice mode multiple for free.

If I do something like this:

final long[] checkedIds = mListView.getCheckedItemIds();
for ( int i = 0 ; i < mListView.getCheckedItemCount() ; i++ ) {
    Log.d(TAG, "id checked: " + checkedIds[i]);
}

I get all the checked ids without an issue, but I can't see any visual feedback in the ListView.

In other words the logic is fine but when I click the CheckedTextView The green tick doesn't show up.

I was reading the ListView src code and I couldn't find any reference to android:id/text1 and makes me wonder if I should handle widget's checked state myself.

Can anyone spot where android:id/text1 is used to make the widget checked or not?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

Based on my read of the code, the row has to implement Checkable:

if (mChoiceMode != CHOICE_MODE_NONE && mCheckStates != null) {
    if (child instanceof Checkable) {
        ((Checkable) child).setChecked(mCheckStates.get(position));
    }
}

This works for the stock row layouts for lists with choice mode because the row is a CheckedTextView, which implements Checkable.

So, add the Checkable interface to your custom View, delegating the interface's methods to the CheckedTextView, and see if that works out.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...