android - Custom IDs in ListView -
i'm trying populate listview products. can it, need them have custom id's (the ones sqlite database). how can this? code without id implementation far:
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));
you should use simplecursoradapter:
string[] columns = new string[] { "_id", "name" }; int[] = 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));
"to" , "columns" define mapping: "colums" list of database column names want simplecursoradapter read. , "to" list of android ids specifying on widget values cursor set.
in latter code means value of "_id" displayed in textfield android id "android.r.id.text1" , value of column "name" go textfield "android.r.id.text2".
Comments
Post a Comment