0

我正在使用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?

在此先感謝

回答

0
  1. 有沒有簡單的方法,但保持布爾相等的數組列表項的數量和存儲狀態。您需要將其設爲您從SimpleCursorAdapter派生的自定義適配器的成員變量。

  2. 同樣,它需要成爲自定義適配器的一部分。在適配器的getView()函數中,您需要實現複選框的onCheckedChangeListener()[現在不記得確切的名稱],並且在您擁有listview的主Activity中,將onItemClickListener()設置爲列表顯示。

如果這沒有任何意義,只需在stackoverflow中進行一下。這個問題已經處理了很多次。

+0

感謝您的回答。我的列表視圖不是固定的,它會動態變化..在這種情況下,將會是什麼答案..謝謝 – GoCrazy 2012-04-09 18:44:31

+0

你如何計劃更新列表視圖?你會在每次光標變化時創建一個新的遊標並將其分配給你的列表視圖,或者你打算做一個notifydatasetchanged()嗎?如果它是第一種情況,那麼你的適配器的構造函數需要處理布爾數組的重新創建。如果是第二種情況,則必須重寫notifydatasetchanged()方法並實現列表中項目數量及其效果的更改。 – Shubhayu 2012-04-10 01:21:27

+0

嗨,謝謝你的回答。我正在使用第一種方法。我正在更新使用SimpleCursorAdapter的列表視圖..我會嘗試你的答案.. – GoCrazy 2012-04-10 21:41:27