简体   繁体   中英

Checkbox state problem in ListView

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>        

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM