sqlite - Android Studio populating ListView by unexpected data -
i'm working on project , it's first android.
i'm trying populate listview
sqlite database. problem list gets populated related object , not name
or id
illustration want. can see mean in log illustration below
player count﹕ player count: 48
name here:﹕ name here: teris
td here:﹕ td here: packagename.player@435f3ea8
getallplayers function databasehelper.class
public list<player> getallplayers() { list<player> players = new arraylist<player>(); string selectquery = "select * " + table_players; log.e(log, selectquery); sqlitedatabase db = this.getreadabledatabase(); cursor c = db.rawquery(selectquery, null); // looping through rows , adding list if (c.movetofirst()) { { player td = new player(); td.setid(c.getint((c.getcolumnindex(key_id)))); td.setname((c.getstring(c.getcolumnindex(key_name)))); // adding player list log.e("name here: ","name here: " + td.getname()); log.e("td here: ","td here: " + td); players.add(td); } while (c.movetonext()); } homecoming players;
}
playersactivity.class
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_players); db = new databasehelper(getapplicationcontext()); player player1 = new player("teris"); long player1_id = db.createplayer(player1); log.e("player count", "player count: " + db.getplayerscount()); lv = (listview) findviewbyid(r.id.playerslist); your_array_list = db.getallplayers(); arrayadapter<string> adapter = new arrayadapter<string>( this, android.r.layout.simple_list_item_1, your_array_list ); lv.setadapter(adapter); db.closedb();
}
what doing wrong exactly?
arrayadapter<string>
tries convert every player
object string
. default implementation returns "packagename.player@435f3ea8". easy solution - override tostring
method within player class:
@override public string tostring() { homecoming name; }
that way every player
object represented name.
(btw looking @ class name "player" hurts. should start uppercase :) )
android sqlite listview
No comments:
Post a Comment