sqlite - Loading a ListView Adapter in Android -


i trying load adapter sqlite database , cannot seem wrap head around how works. extremely new android , java programming forgive if stumble around of obvious.

i understand have "proper" database setup must have 3 java files, main, databasehandler , class whatever table is, getting there. have of working in form cannot seem listview load rows in table.

i have 1 table 2 columns, id , item. code in databasehandler items shown below:

public list<item> getallitems() {     list<item> itemlist = new arraylist<item>();     string selectquery = "select * " + table_items;     sqlitedatabase db = this.getwritabledatabase();     cursor cursor = db.rawquery(selectquery, null);      if (cursor.movetofirst())     {         {             item item = new item();             item.setid(integer.parseint(cursor.getstring(0)));             item.setitem(cursor.getstring(1));             itemlist.add(item);         } while (cursor.movetonext());     }     return itemlist; } 

how put listview adapter in main activity? tried below , did not work getting pointer positions instead:

    list itemarray = db.getallitems();     //define new adapter     arrayadapter<list> adapter = new arrayadapter<list>(this,             android.r.layout.simple_list_item_1, android.r.id.text1, itemarray);      //bind adapter listview     itemview.setadapter(adapter); 

how load items in?

thanks given.

socaprice

you can use simplecursoradapter here. won't have parse , won't need custom adapter.

first adjust db method return cursor:

public cursor getdata() {     string selectquery = "select * " + table_items;     sqlitedatabase db = this.getwritabledatabase();     cursor cursor = db.rawquery(selectquery, null);      return cursor; } 

next create , set simplecursoradapter:

cursor cursor = db.getdata(); string[] = new string[] { "stringcolumn" }; int[] = new int[] { android.r.id.text1 };  simplecursoradapter adapter = new simplecursoradapter(this,         android.r.layout.simple_list_item_1, cursor, from, to, 0);  listview.setadapter(adapter); 

note: change "stringcolumn" db column name used.

edit: can fetch column names afterwards this:

string itemcolumn = cursor.getcolumnname(1); 

and use simplecursoradapter:

string[] = new string[] { itemcolumn }; 

Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -