tags:

views:

39

answers:

1

Hello with regard to the known issue of unwanted checkboxes being checked while scrolling through a listview, can anyone please give me an idea how to use a checkedTextView properly so that i can maintain check state properly when scrolling through the list. I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case. or do i have to use the default Id for the checkedTextView. I can't seem to find an example that uses a cursorAdapter or a SimpleCursorAdapter for this case. Thank you

i have tried using this, but am not really sure what to do with the set tag position. here is a small piece of code:

  //bindView() method in SimpleCursorAdapter
 //viewHolder holder;
    //Cursor c;

    holder.checkedText = (CheckedTextView)view.findViewById(R.id.view_checked);
    holder.checkedText.setTag(c.getPosition());
        holder.checkedText.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v)
                        {
                              v.getTag();
                            ((CheckedTextView) v).toggle();
                        }
                    });

my xml standard layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:choiceMode="multipleChoice"/>

    <TextView android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_notes"/>
</LinearLayout>

my custom layout:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:orientation="horizontal"
  android:padding="6dp">

 <LinearLayout
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="2dp">

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/viewNameText"
            android:id="@+id/viewNameId">
        </TextView>

    </LinearLayout>

    <LinearLayout
        android:orientation="vertical"
        android:gravity="center_horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:padding="2dp">


        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start Date"
            android:layout_gravity="right"
            android:id="@+id/DateId">
        </TextView>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="start Time"
            android:layout_gravity="right"
            android:id="@+id/TimeId">
        </TextView>
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="right"
            >



     <CheckedTextView 
          android:id="@+id/title" 
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content" 
          android:checked="false"
          android:focusable="false"
          android:clickable="true"
          android:textAppearance="?android:attr/textAppearanceLarge" 
          android:checkMark="?android:attr/listChoiceIndicatorMultiple" 
          /> 

    </LinearLayout>

 </LinearLayout>        
A: 

I thought the listview takes care of maintaining the checkbox state when using a checkedtextView but that does not seem to be the case.

It should, if your ListView is android:choiceMode="multiple".

do i have to use the default Id for the checkedTextView

You might. The only times I have implemented a multiple choice ListView, I have used the standard row layout rather than a custom one.

CommonsWare
yes my list view has the attribute choiceMode to multiple. it does not have the default Id though. if this help, i am using a standard listview\layout in onCreate and then using my custom layout to display the items. but my checkedTextView is in my custom layout. should i move it to my standard layout?
manuelJ
i have added my xml layout above to show what i meant. thanks for the time again
manuelJ
@manuelJ: Try changing the `android:id` of your `CheckedTextView` to `android:id="@android:id/text1`. You can refer to this from your Java code as `android.R.id.text1`.
CommonsWare
i tried what you suggested, but its still the same thing. any other idea?.. how do i implement the code to remember the items clicked after calling v.getTag();
manuelJ
@manuelJ: Perhaps `android:choiceMode="multiple"` only works with the standard multiple choice row layout. That would be disappointing. Anyway, you probably need to add OnCheckChangedListeners, not OnClickListeners, to the checkboxes, then update a data model with the state. Plus, you will need to apply that state when you are inflating/recycling rows. Here is a sample project showing the use of RatingBar in list rows that works on the same basic principle: http://github.com/commonsguy/cw-android/tree/master/FancyLists/RateList/
CommonsWare
sorry for the disturbance..but am not really clear based on the concept and in your example. you said i will need to apply that state, when inflating or recycling rows. does that mean i have to call findViewById() again in the OnCheckChangedListener and inflate the layout again or do i do that again in the bindView() method? Am so miserable right now, i just wanted to keep the checkbox state when scrolling but i might have to rethink the design and just remove the item from the list when it has been checked!
manuelJ
@manuelJ: Here is an excerpt from one of my books that explains that example a bit better, and related stuff besides: http://commonsware.com/Android/excerpt.pdf
CommonsWare
thank you very much. will look into it.
manuelJ