我有一個由遊標數據庫填充的列表視圖,以及用於該行的自定義xml。 一切正常,但我需要檢查或取消選中我行中的複選框,取決於數據庫中的值。 我不知道如何。android:自定義列表視圖與複選框,如何填充它
這是我的課:
// inserisco gli elementi
db.open();
Cursor prendi_preventivi=db.prendi_preventivi();
while(prendi_preventivi.moveToNext()){
String nome_preventivo=prendi_preventivi.getString(prendi_preventivi.getColumnIndex("nome"));
String data_preventivo=prendi_preventivi.getString(prendi_preventivi.getColumnIndex("data"));
int approvato_preventivo=Integer.parseInt(prendi_preventivi.getString(prendi_preventivi.getColumnIndex("approvato")));
preventiviLista.add(new Preventivo_per_lista(nome_preventivo,data_preventivo,approvato_preventivo));
}
db.close();
ArrayList<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
for(int i=0;i<preventiviLista.size();i++){
Preventivo_per_lista p=preventiviLista.get(i);
HashMap<String,Object> preventivoMap=new HashMap<String, Object>();
preventivoMap.put("nome", p.getNome());
preventivoMap.put("data", p.getData());
data.add(preventivoMap);
}
String[] from={"nome","data"};
int[] to={R.id.nome_preventivo,R.id.data_preventivo};
SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(),data,R.layout.elemento_preventivo,from,to);
lista_preventivi.setAdapter(adapter);
lista_preventivi.setOnItemClickListener(new OnItemClickListener(){
// click di elemento
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long _id){
System.out.println(lista_preventivi.getItemAtPosition(position).toString());
}
});
這是我Preventivo_per_lista:
public class Preventivo_per_lista{
private String nome;
private String data;
private int approvato;
public Preventivo_per_lista(String name,String data,int approvato){
super();
this.nome=name;
this.data=data;
this.approvato=approvato;
}
public String getNome(){
return nome;
}
public String getData(){
return data;
}
public int getApprovato(){
return approvato;
}
}
這是我行個人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="wrap_content"
android:padding="5dip"
android:orientation="horizontal"
android:id="@+id/elemento_preventivo"
android:weightSum="3">
<TextView android:id="@+id/nome_preventivo"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_weight="1"
android:textStyle="bold" />
<TextView android:id="@+id/data_preventivo"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_weight="1"
android:textStyle="bold" />
<CheckBox android:id="@+id/check_preventivo"
android:layout_width="20dp"
android:layout_height="20dp"
android:textColor="#000000"
android:layout_weight="1" />
</LinearLayout>
如果我的approvato_preventivo是1,我需要檢查複選框,如果它是0我需要像正常一樣離開它。
有人可以幫助我嗎?
嗨。 SimpleAdapter被稱爲「簡單」,因爲它非常簡單。其實你可以擴展它並「發明」一些東西,但這不是個好主意。請使用CursorAdapter http://developer.android.com/reference/android/widget/CursorAdapter.html滿足您的需求。 – SSemashko
不,這不是問題 – Zak
不是。 CursorAdapter是您的任務的基本方法。從架構和性能角度來看,它要好得多。試一試。目前你正在發明一個「鉤子,如何使用SimpleAdapter來達到它不適合的目的」。 – SSemashko