如何在listview android中存储选中列表状态?

如何在listview android中存储选中列表状态?,android,checkbox,simplecursoradapter,listadapter,onitemclicklistener,Android,Checkbox,Simplecursoradapter,Listadapter,Onitemclicklistener,我使用listview显示三个文本视图(这些文本视图的值来自数据库)和一个复选框。我想在用户单击复选框时存储复选框状态,并在用户返回时显示它。但是,我对android开发还不熟悉,我不知道该怎么做。我已尝试将复选框添加到列表视图,但onitemclick已禁用。下面是代码 这是从数据库和listview中的列表检索值的光标 Cursor yearcursor = db.paymentall(this); String[] yearfrom = new String[] { Payment

我使用listview显示三个文本视图(这些文本视图的值来自数据库)和一个复选框。我想在用户单击复选框时存储复选框状态,并在用户返回时显示它。但是,我对android开发还不熟悉,我不知道该怎么做。我已尝试将复选框添加到列表视图,但onitemclick已禁用。下面是代码

这是从数据库和listview中的列表检索值的光标

Cursor yearcursor = db.paymentall(this); 
 String[] yearfrom = new String[]  
{ PaymentAppDataBase.REQUESTEDDATE,PaymentAppDataBase.PAYMENTNAME,PaymentAppDataBase.AMOUNT };
 int[] yearto = new int[] { R.id.Date,R.id.Name,R.id.Amount };

            SimpleCursorAdapter yearadapter =

 new SimpleCursorAdapter(this, R.layout.listview, yearcursor, yearfrom, yearto);

setListAdapter(yearadapter);

 amount.setOnItemClickListener(new OnItemClickListener() {


@Override

public void onItemClick(AdapterView<?> parent, View view, int position,

 long id) {


 Cursor cursor = (Cursor) parent.getItemAtPosition(position);


 String toaddressreview = cursor.getString(4);

 String subjectreview = cursor.getString(5);

 String emailbodyreview = cursor.getString(6);

 Intent todayreview = new Intent(ReviewPayment.this,ReviewandResend.class);

todayreview.putExtra("toadd", toaddressreview);

 todayreview.putExtra("subjectreveiew", subjectreview);

 todayreview.putExtra("emailbody", emailbodyreview);

 startActivity(todayreview);

 }

}); 
Cursor yearcursor=db.paymentall(此项);
字符串[]yearfrom=新字符串[]
{PaymentAppDataBase.REQUESTEDDATE,PaymentAppDataBase.PAYMENTNAME,PaymentAppDataBase.AMOUNT};
int[]yearto=新int[]{R.id.Date,R.id.Name,R.id.Amount};
SimpleCursorAdapter适配器=
新的SimpleCursorAdapter(this,R.layout.listview,yearcursor,yearfrom,yearto);
setListAdapter(yearadapter);
amount.setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView父视图、视图、整型位置、,
长id){
Cursor Cursor=(Cursor)parent.getItemAtPosition(位置);
String toaddressreview=cursor.getString(4);
String subjectreview=cursor.getString(5);
String emailbodyreview=cursor.getString(6);
意向todayreview=新意向(ReviewPayment.this、ReviewandResend.class);
todayreview.putExtra(“toadd”,toaddress review);
todayreview.putExtra(“subjectreview”,subjectreview);
todayreview.putExtra(“emailbody”,emailbodyreview);
startActivity(今日回顾);
}
}); 
Mt-xml文件:

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

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

android:paddingTop="4dip"

android:paddingBottom="6dip"

 android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">


 <TextView android:id="@+id/Date"

 android:layout_width="100dip"

 android:layout_height="wrap_content"

 android:textSize="14sp"/>


<TextView android:id="@+id/Name"

 android:layout_width="120dip"

android:layout_height="wrap_content" 

android:layout_weight="2" 

 android:scrollbarAlwaysDrawHorizontalTrack="true"

 android:layout_toRightOf="@+id/Date"/>


<TextView android:id="@+id/Amount"

 android:layout_width="50dip"

android:layout_height="20dip"  

 android:scrollbarAlwaysDrawHorizontalTrack="true" 

 android:layout_weight="2"

 android:layout_toRightOf="@+id/Name"/>


<CheckBox

 android:id="@+id/checkBox1"

 android:layout_width="wrap_content"

 android:layout_height="50dip"

 android:layout_alignBottom="@+id/Amount"

 android:layout_alignParentRight="true"

 android:layout_toRightOf="@+id/Amount" />

</RelativeLayout>

如果我能得到两个问题的答案那就太好了

1.如何保存复选框的状态

2.如何在使用复选框时启用McClickListener

提前谢谢

  • 没有简单的方法,只能保持布尔值数组等于列表项的数量并存储状态。您需要使其成为从SimpleCursorAdapter派生的自定义适配器的成员变量

  • 同样,它需要是自定义适配器的一部分。在适配器的getView()函数中,需要为复选框实现onCheckedChangeListener()


  • 如果这不合理,只需在stackoverflow中进行一些检查。这个问题已经讨论过很多次了。

    谢谢你的回答。我的列表视图不是常量,它将动态更改。。那样的话,答案是什么。。thanksHow您是否计划更新listview?每次光标更改时,您是要创建一个新光标并将其分配给listview,还是要执行notifydatasetchanged()?如果是第一种情况,那么适配器的构造函数需要处理布尔数组的重新创建。如果是第二种情况,那么您必须重写notifydatasetchanged()方法,并实现列表中项目数量的更改及其效果。您好,谢谢您的回答。我使用第一种方法。我正在使用SimpleCursorAdapter更新listview。。我试试你的答案。。