我正在使用listview来显示三个textviews(来自数据库的这些文本视图的值)和一个复选框。当用户点击复选框并在他们回来时显示它时,我想要存储复选框状态。但是,我对android开发并不熟悉,我也不知道如何去做。我曾尝试加入复选框,列表视图,但onitemclick是disabled.Below是代码,如何在listview android中存储检查列表状态?
这是光标从数据库中检索,并在列表视图列表值,
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);
}
});
万吨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.如何在使用复选框时启用onitemclicklistener?
在此先感谢
感谢您的回答。我的列表视图不是固定的,它会动态变化..在这种情况下,将会是什么答案..谢谢 – GoCrazy 2012-04-09 18:44:31
你如何计划更新列表视图?你会在每次光标变化时创建一个新的游标并将其分配给你的列表视图,或者你打算做一个notifydatasetchanged()吗?如果它是第一种情况,那么你的适配器的构造函数需要处理布尔数组的重新创建。如果是第二种情况,则必须重写notifydatasetchanged()方法并实现列表中项目数量及其效果的更改。 – Shubhayu 2012-04-10 01:21:27
嗨,谢谢你的回答。我正在使用第一种方法。我正在更新使用SimpleCursorAdapter的列表视图..我会尝试你的答案.. – GoCrazy 2012-04-10 21:41:27