Populate a list view in on create android -


i trying populate listview on screen called leaderboard.xml have been around loads of different sites , nobody seems show clear way of doing (most seem require form of user input).

i have database helper class below:

package com.example.deepseadiver;  import java.util.arraylist;  import android.content.contentvalues; import android.content.context; import android.database.cursor; import android.database.sqlexception; import android.database.sqlite.sqlitedatabase; import android.database.sqlite.sqliteopenhelper;  public class databasehelper {      private static final string database_name = "scoredatabase";     private static final int database_version = 1;       private static final string table_name = "highscores";       private openhelper mdbhelper;     private sqlitedatabase mdb;     private final context dbcontext;       private static final string database_create =                         "create table " + table_name + " (" +                         "_id integer primary key autoincrement, " +                         "name text, " +                         "score text);";       public databasehelper(context ctx) {         this.dbcontext = ctx;     }       public databasehelper open() throws sqlexception {         mdbhelper = new openhelper(dbcontext);         mdb = mdbhelper.getwritabledatabase();         return this;     }       public void close() {         mdbhelper.close();     }       public boolean createuser(string name, string score) {         contentvalues initialvalues = new contentvalues();         initialvalues.put("name", name);         initialvalues.put("score", score);          return mdb.insert(table_name, null, initialvalues) > 0;     }       public boolean updateusers(long rowid, string name, string score) {         contentvalues args = new contentvalues();          args.put("name", name);         args.put("score", score);          return mdb.update(table_name, args, "_id=" + rowid, null) > 0;     }       public void deleteall() {         mdb.delete(table_name, null, null);     }       public void deleterecord(long rowid) {         mdb.delete(table_name, "_rowid=" + rowid, null);     }       public cursor getdata(string table)      {         return mdb.rawquery("select * "+table+"", null);      }      public cursor fetchall(sqlitedatabase db, string table){          cursor cursor = db.rawquery("select * "+table+"", null);         return cursor;      }       public arraylist<string[]> fetchscore(string name) throws sqlexception {          arraylist<string[]> myarray = new arraylist<string[]>();          int pointer = 0;               cursor mcursor = mdb.query(table_name, new string[] {"_id", "name",                 "score"}, "name '%" + name + "%'", null,                     null, null, "score desc");           int namecolumn = mcursor.getcolumnindex("name");         int scorecolumn = mcursor.getcolumnindex("score");                if (mcursor != null){            if (mcursor.movetofirst()){                 {                     myarray.add(new string[3]);                     myarray.get(pointer)[0] = mcursor.getstring(namecolumn);                     myarray.get(pointer)[1] = mcursor.getstring(scorecolumn);                     pointer++;                 } while (mcursor.movetonext());            } else {                 myarray.add(new string[3]);                myarray.get(pointer)[0] = "no results";                myarray.get(pointer)[1] = "";            }         }           return myarray;      }      public arraylist<string[]> selectall() {           arraylist<string[]> results = new arraylist<string[]>();           int counter = 0;          cursor cursor = this.mdb.query(table_name, new string[] { "id", "name", "score"}, null, null, null, null, "name desc");           if (cursor.movetofirst()) {         {             results.add(new string[2]);             results.get(counter)[0] = cursor.getstring(0).tostring();             results.get(counter)[1] = cursor.getstring(1).tostring();             results.get(counter)[2] = cursor.getstring(2).tostring();             counter++;             } while (cursor.movetonext());         }         if (cursor != null && !cursor.isclosed()) {             cursor.close();         }          return results;     }       private static class openhelper extends sqliteopenhelper {         openhelper(context context) {             super(context, database_name, null, database_version);         }          @override         public void oncreate(sqlitedatabase db) {             db.execsql(database_create);         }          @override         public void onupgrade(sqlitedatabase db, int oldversion, int newversion) {             db.execsql("drop table if exists " + table_name);             oncreate(db);         }     } } 

and leaderboard.java:

package com.example.deepseadiver;  import java.util.arraylist; import java.util.list;   import android.app.activity; import android.database.cursor; import android.database.sqlite.sqlitedatabase; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.arrayadapter; import android.widget.edittext; import android.widget.listview; import android.widget.relativelayout;  public class leaderboard extends activity {      relativelayout leaderexit;      string table = "highscores";      databasehelper dbh;     arraylist<string> listitems = new arraylist<string>();     arrayadapter<string> adapter;       @override         public void oncreate(bundle savedinstancestate) {             super.oncreate(savedinstancestate);             setcontentview(r.layout.leaderboard);              dbh = new databasehelper(this);             dbh.open();               leaderexit = (relativelayout) findviewbyid(r.id.exit);               leaderexit.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     leaderboard.this.finish();                 }             });      }      } 

the list view trying populate has id of lvitems , on leaderboard.xml screen.

thanks help


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

datatable - Matlab struct computations -