我尝试使用以下代码使用数据库查询的结果填充列表。
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;
}
以下是运行以下内容时输出的内容
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 :: 改变
Log.v("andy","Entries::: " + entries.toString());
to
Log.v("andy","Entries::: " + entries.get(0));
also 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
编辑* 2:进一步记录测试
将功能更改为:
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 i = 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
为什么新条目总是覆盖所有以前添加的条目?我无法弄清楚如何使代码变得更简单,以弄清楚什么是错误的。
感谢任何帮助!
答案 0 :(得分:0)
如果您正确地看到日志,则新条目不会覆盖所有先前添加的条目。它只是添加了一个新条目。
你的循环在这里运行了3次
Log.v("andy","Entries::: " + entries.toString());
首次打印项目列表1,第二次打印时间为3秒。
第一次
条目::: [ID:1 /名称:andy /地址1:null /地址2:null / 地址3:null / Postcode:null / Tel:null / Mob:null / Email:null /网站:null / deleted:0]
第二次
条目::: [ID:2 /名称:cg1 /地址1:null /地址2:null / 地址3:null / Postcode:null / Tel:null / Mob:null / Email:null /网站:null / deleted:0,ID:2 /名称:cg1 /地址1:null / 地址2:null /地址3:null / Postcode:null / Tel:null / Mob: null / Email:null / Website:null / deleted:0]
第3次
条目::: [ID:3 /名称:yjh /地址1:null /地址2:null / 地址3:null / Postcode:null / Tel:null / Mob:null / Email:null /网站:null /删除:0,ID:3 /名称:yjh /地址1:null / 地址2:null /地址3:null / Postcode:null / Tel:null / Mob: null / Email:null / Website:null / deleted:0,ID:3 / Name:yjh / 地址1:null /地址2:null /地址3:null / Postcode:null / 电话:null / Mob:null /电子邮件:null /网站:null / deleted:0]
请正确查看日志。
答案 1 :(得分:0)
事实证明,这是因为我将所有的Entry类变量设置为静态,我学到的将使它们在类的所有实例中都相同。所以这就是覆盖以前的条目。
只是让其他人有这个问题!