Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

}

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.

like image 268
JavaJumper Avatar asked Jun 30 '26 11:06

JavaJumper


1 Answers

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.

like image 84
John Leehey Avatar answered Jul 03 '26 02:07

John Leehey



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!