java - Populate ListView with an arrayList<String> with data obtained from a cursor -
helo trying populate listview data stored in sqlite. after select product want reference of product go on same line drew in picture.
can make arrayadapter put records in same xml?
my code looks this: cursor returns records:
public cursor getallrows() { string = null; // query dbadapter cursor cursor = db.query(true, table_name, all_keys, where, null, null, null, null, null); if (cursor != null) { cursor.movetofirst(); } return cursor; }
adding data arraylist:
public arraylist<string> fromcursortoarrayliststring(cursor c){ arraylist<string> result = new arraylist<string>(); c.movetofirst(); for(int = 0; < c.getcount(); i++){ string row_product = c.getstring(c.getcolumnindex(key_product)); string row_price = c.getstring(c.getcolumnindex(key_price)); string row_type = c.getstring(c.getcolumnindex(key_type)); result.add(row_product); result.add(row_price); result.add(row_type); c.movetonext(); } return result; }
in mainactivity wrote this:
listview listview = (listview) findviewbyid(r.id.new_list_listview); cursor cursor = newlistdbadapter.getallrows(); arrayadapter<string> arrayadapter = new arrayadapter<string>(this,r.layout.custom_product_layout_new_list,r.id.custom_product_layout_new_list_productname,newlistdbadapter.fromcursortoarrayliststring(cursor)); listview.setadapter(arrayadapter);
cursoradapter adapter more reliable . pass cursor adapter no need maintain collection . must follow few things
adapter
public class yourcursoradapter extends cursoradapter { public yourcursoradapter(context context, cursor cursor, int flags) { super(context, cursor, 0); } // newview method used inflate new view , return it, // don't bind data view @ point. @override public view newview(context context, cursor cursor, viewgroup parent) { return layoutinflater.from(context).inflate(r.layout.your_item_view, parent, false); } // bindview method used bind data given view // such setting text on textview. @override public void bindview(view view, context context, cursor c) { // find fields populate in inflated template textview tvproduct = (textview) view.findviewbyid(r.id.tcproduct); textview tvprice = (textview) view.findviewbyid(r.id.tvprice); textview tvtype = (textview) view.findviewbyid(r.id.tvtype); // extract properties cursor string row_product = c.getstring(c.getcolumnindex(key_product)); string row_price = c.getstring(c.getcolumnindex(key_price)); string row_type = c.getstring(c.getcolumnindex(key_type)); // populate fields extracted properties tvproduct.settext(row_product); tvprice.settext(row_price); tvtype.settext(row_type); } }
retrieve record db
public cursor getallrows() { string = null;
// query dbadapter cursor cursor = db.query(true, table_name, all_keys, where, null, null, null, null, null); if (cursor != null) { cursor.movetofirst(); } return cursor;
}
now set cursor adapter ,adapter set listview
listview listview = (listview) findviewbyid(r.id.new_list_listview); cursor cursor = newlistdbadapter.getallrows(); yourcursoradapter arrayadapter = new yourcursoradapter(this, cursor); listview.setadapter(arrayadapter );
Comments
Post a Comment