listview - Android List.add function overwriting previous entries -
im trying populate list results database query following code.
public list<entry> getallentries() { list<entry> entries = new arraylist<>(); cursor cursor = database.query(sqlitehelper.table_clients, allcolumns, null, null, null, null, null); cursor.movetofirst(); while (!cursor.isafterlast()) { entry entry = cursortoentry(cursor); entries.add(entry); log.v("andy", "entry::: " + entry.tostring()); log.v("andy","entries::: " + entries.tostring()); cursor.movetonext(); } cursor.close(); return entries; }
and here outputs when run following
list<entry> e = getallentries(); 08-16 19:53:28.274 13698-13698/uk.co.ab.ci v/andy﹕ entry::: id: 1 / name: andy / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 19:53:28.274 13698-13698/uk.co.ab.ci v/andy﹕ entries::: [id: 1 / name: andy / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0] 08-16 19:53:28.274 13698-13698/uk.co.ab.ci v/andy﹕ entry::: id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 19:53:28.274 13698-13698/uk.co.ab.ci v/andy﹕ entries::: [id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0, id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0] 08-16 19:53:28.274 13698-13698/uk.co.ab.ci v/andy﹕ entry::: id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 19:53:28.274 13698-13698/uk.co.ab.ci v/andy﹕ entries::: [id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0, id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0, id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0]
edit:: changing
log.v("andy","entries::: " + entries.tostring()); log.v("andy","entries::: " + entries.get(0)); adding: log.v("andy","entries size::: " + entries.size()); outputs: 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entry::: id: 1 / name: andy / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entries::: id: 1 / name: andy / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entries size::: 1 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entry::: id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entries::: id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entries size::: 2 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entry::: id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entries::: id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:33:06.054 30909-30909/uk.co.ab.ci v/andy﹕ entries size::: 3
edit*2: further log testing
change function to:
public list<entry> getallentries() { log.v("andy","getallentries"); list<entry> entries = new arraylist<entry>(); cursor cursor = database.query(sqlitehelper.table_clients, allcolumns, null, null, null, null, null); cursor.movetofirst(); int = 0; while (!cursor.isafterlast()) { i++; entry entry = cursortoentry(cursor); log.v("andy", "entry::: " + entry.tostring()); entries.add(new entry(i, "testlistitemname"+i, null, null, null, null, null, null, null, null, 0, 0)); cursor.movetonext(); } log.v("andy","entries 0::: " + entries.get(0)); log.v("andy","entries 1::: " + entries.get(1)); log.v("andy","entries 2::: " + entries.get(2)); log.v("andy","entries size::: " + entries.size()); cursor.close(); return entries; } outputs : 08-16 20:51:03.184 6357-6357/uk.co.ab.ci v/andy﹕ entries 0::: id: 3 / name: testlistitemname3 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:51:03.184 6357-6357/uk.co.ab.ci v/andy﹕ entries 1::: id: 3 / name: testlistitemname3 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:51:03.184 6357-6357/uk.co.ab.ci v/andy﹕ entries 2::: id: 3 / name: testlistitemname3 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0 08-16 20:51:03.184 6357-6357/uk.co.ab.ci v/andy﹕ entries size::: 3
why new entries overwriting of added entries? can't figure out how make code simpler figure out whats going wrong.
any appreciated!
if see log new entries not overwriting of added entries. adding new entry.
your loop running 3 times here ,
log.v("andy","entries::: " + entries.tostring());
is printing item list 1 first time, 2 second time , 3 third time.
first time
entries::: [id: 1 / name: andy / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0]
2nd time
entries::: [id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0, id: 2 / name: cg1 / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0]
3rd time
entries::: [id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0, id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0, id: 3 / name: yjh / address 1: null / address 2: null / address 3: null / postcode: null / tel: null / mob: null / email: null / website: null / deleted: 0]
please have @ logs properly.
Comments
Post a Comment