0

我的问题ListView

ListView 项目 xml 有一个 textarea 和一个 Imageview,对于从 DB 中获取的每个项目,我需要放置一个专用图像(基于 _id)。

现在我有这个有效的代码(但没有图像):

mio_db.openDataBase(); 
final Cursor data=mio_db.catalogo_prodotti();
final ListView listView1 = (ListView) findViewById(R.id.list_mia_2); 
String[] from = new String[] {"prodotto","_id"};
int[] to = new int[] {R.id.nome_prodotto_lista,R.id.freccia_prodotto_lista};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.mia_lista_2, data, from, to); 
listView1.setAdapter(adapter);

例如,如果我_id是 4,我需要freccia_4R.id.freccia_prodotto_lista

我不知道该怎么做。我已经阅读了自定义适配器,但我不了解如何实现它。

谁能帮我?

4

3 回答 3

0

我已经以另一种方式解决了,也许不是正确的,但它现在有效:

    setContentView(R.layout.prodotti);
    final Database_mio database_classe=new Database_mio(this); 
    database_classe.openDataBase(); 
    final Cursor data_mio=database_classe.catalogo_prodotti();
    ArrayList<Prodotto_per_lista> personList=new ArrayList<Prodotto_per_lista>();
    int prodotto=data_mio.getColumnIndex("prodotto");
    int id_immagine=data_mio.getColumnIndex("_id");
    while(data_mio.moveToNext()){
        switch(Integer.parseInt(data_mio.getString(id_immagine))){
            case 1:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_1,data_mio.getString(prodotto)));
                break;
            case 18:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_18,data_mio.getString(prodotto)));
                break;
            case 23:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_23,data_mio.getString(prodotto)));
                break;
            case 28:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_28,data_mio.getString(prodotto)));
                break;
            case 30:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_30,data_mio.getString(prodotto)));
                break;
            case 35:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_35,data_mio.getString(prodotto)));
                break;
            case 41:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_41,data_mio.getString(prodotto)));
                break;
            case 48:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_48,data_mio.getString(prodotto)));
                break;
            case 53:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_53,data_mio.getString(prodotto)));
                break;
            case 57:
                personList.add(new Prodotto_per_lista(R.drawable.freccia_57,data_mio.getString(prodotto)));
                break;
        }
    }
    ArrayList<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>();
    for(int i=0;i<personList.size();i++){
            Prodotto_per_lista p=personList.get(i);
            HashMap<String,Object> personMap=new HashMap<String, Object>();
            personMap.put("name", p.getName());
            personMap.put("image", p.getPhotoRes());
            data.add(personMap);
    }
    String[] from={"name","image"};
    int[] to={R.id.nome_prodotto_lista,R.id.freccia_prodotto_lista};
    SimpleAdapter adapter=new SimpleAdapter(
                    getApplicationContext(),
                    data,
                    R.layout.mia_lista_2,
                    from,
                    to);
    ((ListView)findViewById(R.id.list_mia_2)).setAdapter(adapter);
于 2012-08-24T14:02:54.677 回答
0

使用光标为您的列表视图创建 CustomAdapter

步骤1

按照本教程

第2步

而不是扩展数组适配器扩展光标适配器,它应该工作

class ListViewAdapter extends CursorAdapter {
LayoutInflater mInflater;
SimpleDateFormat sdf;

RequestLogAdapter(Context context, Cursor cursor) {
    super(context, cursor);
    mInflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

     //Get your details here
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
                //Create layout for image with text here
    return mInflater.inflate(R.layout.mulitcolumnlistview, parent, false);
}

@Override
public void bindView(View row, Context context, Cursor cursor) {
                //You can customise here

   }
 }

希望能帮助到你

于 2012-08-24T13:16:37.930 回答
0

现在唯一的问题是项目的 id 现在取自数组(data.add)而不是数据库中的 _id。

于 2012-08-24T15:37:03.827 回答