问题
i have a listview filled by a cursor database, and with a custom xml for the row. all works well, but i need to check or uncheck a checkbox in my row, dependig of the value in the db. i don't know how.
this is my class:
// 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());
}
});
this is my 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;
}
}
this is my personal xml for the row
<?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>
if my approvato_preventivo is 1, i need to check the checkbox, if it is 0 i need to leave it like normal.
Can someone help me?
回答1:
In your getView() method of your adapter you need to have some check like this
if the "approvato_preventivo" is value type
if(approvato_preventivo==1)
{
checkbox.setchecked(true);
}
else if(approvato_preventivo==0)
{
checkbox.setchecked(false);
}
hope this will help
回答2:
i've solved it by myself:
this was the edited part:
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());
if(p.getApprovato()==1){
preventivoMap.put("approvato",true);
}else{
preventivoMap.put("approvato",false);
}
data.add(preventivoMap);
}
String[] from={"nome","data","approvato"};
int[] to={R.id.nome_preventivo,R.id.data_preventivo,R.id.check_preventivo};
来源:https://stackoverflow.com/questions/17489559/android-custom-listview-with-checkbox-how-to-fill-it