在Android上滚动listview时,复选框未选中

我是Android开发的新手。我listview用a textbox和创建了一个checkbox。


当我检查checkbox并向下滚动以检查列表视图中的其他项目时,将取消选中较旧的项目。


我如何避免这个问题listview?请引导我使用我的代码。


这是代码:


main.xml中:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="vertical"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent">


    <TextView

        android:id="@+id/TextView01"

        android:layout_height="wrap_content"

        android:text="List of items"

        android:textStyle="normal|bold"

        android:gravity="center_vertical|center_horizontal"

        android:layout_width="fill_parent"/>


    <ListView

        android:id="@+id/ListView01"

        android:layout_height="250px"

        android:layout_width="fill_parent"/>


    <Button

        android:text="Save"

        android:id="@+id/btnSave"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"/>


</LinearLayout>

这是我用来创建动态列表行的XML页面:


listview.xml:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_height="wrap_content"

    android:gravity="left|center"

    android:layout_width="wrap_content"

    android:paddingBottom="5px"

    android:paddingTop="5px"

    android:paddingLeft="5px">


    <TextView

        android:id="@+id/TextView01"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:gravity="center"

        android:textColor="#FFFF00"

        android:text="hi"/>


    <TextView

        android:text="hello"

        android:id="@+id/TextView02"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_marginLeft="10px"

        android:textColor="#0099CC"/>


</LinearLayout>

这是我的活动课。

请帮我解决这个问题。

我在很多地方都提到了这个问题。但我无法得到正确答案来解决这个问题。

请提供代码以避免在向上和向下滚动时取消选中复选框。


牧羊人nacy
浏览 606回答 3
3回答

茅侃侃

在自定义listview适配器中,在调用setOnCheckedChangeListener&nbsp;之后调用setChecked&nbsp;。这样,您可以从循环视图中切断与旧侦听器的链接。

慕侠2389804

大段引用**在Side getView()方法中只需调用>>check.setOnCheckedChangeListener(new OnCheckedChangeListener() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void onCheckedChanged(CompoundButton arg0, boolean arg1)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(check.isChecked())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //checked&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chkArray[position]=true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp;//uncheck&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; chkArray[position]=false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });//check condition for checkboxes&nbsp; &nbsp; &nbsp; &nbsp; if(chkArray.length>0)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < chkArray.length; i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(chkArray[position]!=null && chkArray[position])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; check.setChecked(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android