mysql - filling ListView from database in Android -


i'm new in android programming , i'm wondering witch appropriate way fill listview database. here method i'm using in database items

// getting stats public list<statparcours> getallsats() {        list<statparcours> statlist = new arraylist<statparcours>();         // select query         string selectquery = "select  * " + table_stats;          sqlitedatabase db = this.getwritabledatabase();         cursor cursor = db.rawquery(selectquery, null);          // looping through rows , adding list         if (cursor.movetofirst()) {             {                 statparcours stat = new statparcours();                 stat.setid(integer.parseint(cursor.getstring(0)));                 stat.setdate(cursor.getstring(1));                 stat.setduration(cursor.getstring(2));                 stat.setdistance(double.parsedouble(cursor.getstring(3)));                 stat.setspeed(double.parsedouble(cursor.getstring(4)));                 stat.setcondition(cursor.getstring(5));                  // adding contact list                 statlist.add(stat);             } while (cursor.movetonext());         }          // return contact list         return statlist; } 

and in main activity, i'm using this. know there wrong populatemystatslist method, still don't know how fix it.

public class history extends activity { public databasehandler db; private list<statparcours> mystats = new arraylist<statparcours>();      @override protected void oncreate(bundle savedinstancestate) {     log.i("oncreate", "ok");     super.oncreate(savedinstancestate);     setcontentview(r.layout.history);     populatemystatslist ();     populatelistview();     registerclickcallback();   }     private void populatemystatslist (){         mystats = db.getallsats();      }     private void populatelistview() {         arrayadapter<statparcours> adapter = new mylistadapter();         listview list = (listview) findviewbyid(r.id.histlistview);         list.setadapter(adapter);         log.i("populate", "ok");     }     private void registerclickcallback() {         listview list = (listview) findviewbyid(r.id.histlistview);         list.setonitemclicklistener(new adapterview.onitemclicklistener() {             @override             public void onitemclick(adapterview<?> parent, view viewclicked,                     int position, long id) {                  statparcours clickedcar = mystats.get(position);                 string message = "you clicked position " + position                                 + " car make " + clickedcar.getdate();                 toast.maketext(history.this, message, toast.length_long).show();             }         });     }     private class mylistadapter extends arrayadapter<statparcours> {         public mylistadapter() {             super(history.this, r.layout.item_view, mystats);             log.i("mylistadapter", "ok");         }            @override         public view getview(int position, view convertview, viewgroup parent) {              view itemview = convertview;             if (itemview == null) {                 log.i("make sure", "ok");                 itemview = getlayoutinflater().inflate(r.layout.item_view, parent, false);             }             log.i("getview", "ok");              statparcours currentstat = mystats.get(position);                         textview maketext = (textview) itemview.findviewbyid(r.id.item_txtmake);             maketext.settext(currentstat.getdate());               textview yeartext = (textview) itemview.findviewbyid(r.id.item_txtyear);             yeartext.settext("" + currentstat.getdistance());               textview condiontext = (textview) itemview.findviewbyid(r.id.item_txtcondition);             condiontext.settext(currentstat.getcondition());              return itemview;         }        }   } 

you need use simplecursor adapter. can't reach developer site documentation here example code above.

edit: here link android developer website.

http://developer.android.com/reference/android/widget/simplecursoradapter.html

second edit: go in populatelistview()

// select query string selectquery = "select  * " + table_stats; sqlitedatabase db = this.getwritabledatabase(); cursor cursor = db.rawquery(selectquery, null);  // set layout int[] int[] = new int[]{r.id.item_txtmake,r.id.item_txtyear,r.id.item_txtcondition};  //set columns string[]; fill in actual column names string[] = new string[]{"make","year","condition"};  //set adapter simplecursoradapter adapter = new simplecursoradapter(getactivity(),r.layout.item_view, cursor, from,to,null);  //set adapter listview listview list = (listview) findviewbyid(r.id.histlistview);     list.setadapter(adapter); 

Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -