将 Android 光标转换为数组的 ArrayList
我尝试将游标数据转换为 arraylist
Cursor c = myDbHelper.getLvl1Cata();
String[] data = new String[3];
c.moveToFirst();
while(!c.isAfterLast()) {
data[0] = Integer.toString(c.getInt(0));
data[1] = c.getString(1);
data[2] = Integer.toString(c.getInt(2));
Log.e("cc", data[1]);
catalogueData.add(data);
c.moveToNext();
}
I try to convert my Cursor data to a arraylist<String[]>. But at the end all the data in the arraylist is overwrited with the last row. What do i do wrong?
Cursor c = myDbHelper.getLvl1Cata();
String[] data = new String[3];
c.moveToFirst();
while(!c.isAfterLast()) {
data[0] = Integer.toString(c.getInt(0));
data[1] = c.getString(1);
data[2] = Integer.toString(c.getInt(2));
Log.e("cc", data[1]);
catalogueData.add(data);
c.moveToNext();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个
data
是一个字符串数组。在原始代码中,您多次将相同的数组添加到catalogueData
结构中。您每次都更改了数组内容的值,但它仍然是相同的数组对象。因此,最终catalogueData
保存了对单个数组的多个引用,并且该数组只能有一个data[0]
值:您设置的最后一个值。这个答案通过为游标中的每一行使用一个新的不同的数组来解决这个问题。
Try this
data
is an array of strings. In the original code, you added the same array to yourcatalogueData
structure several times. You changed the value of the array's contents each time, but it was still the same array object. So you ended up withcatalogueData
holding several references to a single array, and that array can only have one value fordata[0]
: the last thing you set it to.This answer fixes that by using a new and different array for each row in the cursor.
试试这个:
Try this:
将
String[] data = new String[3];
放入 while 循环中。每次迭代都会覆盖数组对象。Put
String[] data = new String[3];
into the while loop. You're overwriting the array object with each iteration.