Android ListView中的自定义ID
我正在尝试用产品填充listview。我可以这样做,但我需要它们具有自定义id(来自SQLite数据库的id)。我该怎么做?这是我的代码,到目前为止没有id实现:Android ListView中的自定义ID,android,sqlite,Android,Sqlite,我正在尝试用产品填充listview。我可以这样做,但我需要它们具有自定义id(来自SQLite数据库的id)。我该怎么做?这是我的代码,到目前为止没有id实现: ArrayList<String> productNameList = new ArrayList<String>(); ArrayList<String> productIdList = new ArrayList<String>(); Cursor results =
ArrayList<String> productNameList = new ArrayList<String>();
ArrayList<String> productIdList = new ArrayList<String>();
Cursor results = myDbHelper.getProducts();
ListView lv = (ListView) findViewById(R.id.productlist);
while (results.moveToNext()){
productNameList.add(results.getString(results.getColumnIndex("name")));
productIdList.add(results.getString(results.getColumnIndex("id")));
}
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , productNameList));
ArrayList productNameList=new ArrayList();
ArrayList productIdList=新的ArrayList();
游标结果=myDbHelper.getProducts();
ListView lv=(ListView)findViewById(R.id.productlist);
while(results.moveToNext()){
productNameList.add(results.getString(results.getColumnIndex(“名称”));
add(results.getString(results.getColumnIndex(“id”));
}
lv.setAdapter(新的ArrayAdapter(这个,android.R.layout.simple\u list\u item\u 1,productNameList));
您应该使用SimpleCorsorAdapter:
String[] columns = new String[] { "_id", "name" };
int[] to = new int[] { android.R.id.text1, android.R.id.text2 };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursor, columns, to);
lv.setAdapter(mAdapter));
“到”和“列”定义映射:
“colums”是希望SimpleCursorAdapter读取的数据库列名列表。“to”是一个android列表,指定在哪个小部件上设置光标的值
在后一种代码中,这意味着“_id”的值将显示在带有android id“android.R.id.text1”的文本字段中,“name”列的值将显示在文本字段“android.R.id.text2”中。to”的作用是什么?my_cell_布局是什么?谢谢