机器人:使用复选框定制列表视图,如何来填补它 [英] android: custom listview with checkbox, how to fill it

查看:115
本文介绍了机器人:使用复选框定制列表视图,如何来填补它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个游标填充数据库列表视图,并通过该行自定义XML。
一切运作良好,但我需要检查或在我行取消复选框,dependig的分贝值。
我不知道怎么办。

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.

这是我的类:

    // 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:

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;
}
}

这是我行个人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,我需要把它像正常。

if my approvato_preventivo is 1, i need to check the checkbox, if it is 0 i need to leave it like normal.

有人能帮助我吗?

推荐答案

我已经通过我自己解决了它:

i've solved it by myself:

这是编辑的部分:

    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};

这篇关于机器人:使用复选框定制列表视图,如何来填补它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆