开发者

Android:CheckedTextView isChecked returns incorrect value

Android version: 3.1

API version: Android 2.2

Device: Motorola MX604

I dynamically create a multi-select ListView of CheckedTextView items, and attach a OnItemClickListener to the ListView. In the onItemClick method of the listener, I invoke the isChecked method of CheckedTextView to determine if the associated checkbox is checked or unchecked. Simple enough.

The problem: When I select a previously unselected item, the isChecked method returns false. When I select a previously selected item, the method returns true. The checkbox icon itself checks and unchecks correctly.

Here is the layout for the CheckedTextView:

    <CheckedTextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/text1" 
    android:layout_width="match_parent" 
    android:layout_height="?android:attr/listPreferredItemHeight" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:gravity="center_vertical" 
    android:drawableLeft="?android:attr/listChoiceIndicatorMultiple" 
    android:paddingLeft="6dip" android:paddingRight="6dip" 
    /> 

This is how I create the ListView:

    private void createSortedChannelList() {

    emptyViewContainer();

    ListView sortedListView = new ListView(this);
    sortedListView.setId(CHANNEL_LISTVIEW_ID);
    sortedListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    sortedListView.setItemsCanFocus(false);

    sortedListView.setOnItemClickListener(new OnItemClickListener(){

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,long id) {

            CheckedTextView selectedItem = (CheckedTextView) view;
            boolean isChecked = selectedItem.isChecked();
            Log.e(mLogTag,"item clicked position = " + position + " isChecked = " + isChecked);

        }

    });

    ArrayAdapter<Channel> listAdapter = 
        new ArrayAdapter<Channel>(this,R.layout.favorite_channel_list_select_channel_row,mAllChannels);
    sortedListView.setAdapter(listAdapter);

    for(int channelIndex = 0;channelIndex < mChannelIds.length;channelIndex++){
        if(mSelectedChannelIds.contains(mChannelIds[channelIndex]))
            sortedListView.setItemChecked(channelIndex, true);
    }

    addViewToViewContainer(sortedListView);

}

开发者_StackOverflow中文版This is the log output that is produced when I select a previously unselected item:

09-23 09:08:59.650: item clicked position = 19 isChecked = false

and when I select a previously selected item

09-23 09:10:20.800: item clicked position = 18 isChecked = true

I have done an extensive search and I can only find one other report of similar behavior. This leads me to believe that the problem probably lies in my code, rather than the android class :p I have also looked at numerous examples that are set up in a similar fashion. Can anyone spot a problem?

thanks

PS This is my first post on any forum, so if I'm missing something that would be helpful to the readers of this post, please let me know.


I believe the code is behaving the way it should. Selecting a previously unselected method will invoke the click listener before changing the checked state of the item in the list. In other words, isChecked() won't return true for the previously unselected item until after the onClick() method is finished.


I've noticed that for me, at least, the behavior of when the state changes isn't consistent; on an emulator, isChecked() returned the pre-click state, but on a device it returned the post-click state.

I got around this by bypassing the "isChecked" altogether, and just looking at the state of the underlying object I am toggling, since that won't change unless I explicitly do so. However, this solution may depend on how your code is set up, and there may be other gotcha's that I am overlooking.


You should be using MultiChoiceModeListener for listening to checks. Here is the documentation

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜