Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/198.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android ListView中的复选框状态问题_Android - Fatal编程技术网

Android ListView中的复选框状态问题

Android ListView中的复选框状态问题,android,Android,您好,关于在滚动列表视图时选中不需要的复选框的已知问题,有人能告诉我如何正确使用checkedTextView,以便在滚动列表时正确保持选中状态吗。我认为listview会在使用checkedtextView时维护复选框状态,但事实似乎并非如此。或者我必须使用checkedTextView的默认Id。对于这种情况,我似乎找不到使用游标适配器或SimpleCursorAdapter的示例。多谢各位 我尝试过使用这个,但不确定如何处理设置的标记位置。下面是一小段代码: //bindView()

您好,关于在滚动列表视图时选中不需要的复选框的已知问题,有人能告诉我如何正确使用checkedTextView,以便在滚动列表时正确保持选中状态吗。我认为listview会在使用checkedtextView时维护复选框状态,但事实似乎并非如此。或者我必须使用checkedTextView的默认Id。对于这种情况,我似乎找不到使用游标适配器或SimpleCursorAdapter的示例。多谢各位

我尝试过使用这个,但不确定如何处理设置的标记位置。下面是一小段代码:

  //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();
                        }
                    });
我的xml标准布局:

<?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>

我的自定义布局:

 <?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>        

我认为listview会在使用checkedtextView时维护复选框状态,但事实似乎并非如此

如果您的
ListView
android:choiceMode=“multiple”
,则应该是这样

我必须为checkedTextView使用默认Id吗


也许吧。在我实现多选
列表视图
的唯一一次,我使用的是标准的行布局,而不是自定义的行布局。

是的,我的列表视图具有属性choiceMode to multiple。但它没有默认Id。如果有此帮助,我将在onCreate中使用标准listview\layout,然后使用自定义布局显示项目。但是我的checkedTextView在我的自定义布局中。我应该把它移到我的标准布局中吗?我已经在上面添加了我的xml布局来说明我的意思。谢谢你的时间again@manuelJ:尝试将
CheckedTextView
android:id
更改为
android:id=“@android:id/text1
。您可以从Java代码中将其称为
android.R.id.text1
。还有别的想法吗?。。如何实现代码以记住调用v.getTag()后单击的项目@manuelJ:android:choiceMode=“multiple”可能只适用于标准的多选行布局。那将令人失望。无论如何,您可能需要在复选框中添加OnCheckChangedListeners,而不是OnClickListeners,然后使用状态更新数据模型。另外,在膨胀/回收行时,需要应用该状态。下面是一个示例项目,展示了在列表行中使用RatingBar的过程,该过程遵循相同的基本原则: