将 Android 光标转换为数组的 ArrayList

发布于 2024-12-03 21:46:54 字数 422 浏览 1 评论 0原文

我尝试将游标数据转换为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不可一世的女人 2024-12-10 21:46:54

试试这个

Cursor c = myDbHelper.getLvl1Cata();
String[] data;
if (c != null) {
    while(c.moveToNext()) {
        data = new String[3]; // Note this addition
        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.close();
}

data 是一个字符串数组。在原始代码中,您多次将相同的数组添加到 catalogueData 结构中。您每次都更改了数组内容的值,但它仍然是相同的数组对象。因此,最终 catalogueData 保存了对单个数组的多个引用,并且该数组只能有一个 data[0] 值:您设置的最后一个值。

这个答案通过为游标中的每一行使用一个新的不同的数组来解决这个问题。

Try this

Cursor c = myDbHelper.getLvl1Cata();
String[] data;
if (c != null) {
    while(c.moveToNext()) {
        data = new String[3]; // Note this addition
        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.close();
}

data is an array of strings. In the original code, you added the same array to your catalogueData 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 with catalogueData holding several references to a single array, and that array can only have one value for data[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.

醉殇 2024-12-10 21:46:54

试试这个:

if(mycursor!=null){    
            do{
    TextView name = (TextView)view.findViewById(R.id.contact_name);               
    name.setText(cursor.getString(cursor.getColumnIndex
                (Displayname)));
    mycursor.moveToNext();
            }while (mycursor.isLast());
        }

Try this:

if(mycursor!=null){    
            do{
    TextView name = (TextView)view.findViewById(R.id.contact_name);               
    name.setText(cursor.getString(cursor.getColumnIndex
                (Displayname)));
    mycursor.moveToNext();
            }while (mycursor.isLast());
        }
┼── 2024-12-10 21:46:54

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文