尝试制作一种方法来获取二维字符串数组来表示它时,仅从游标的记录中获取第一个值

Only getting first value from a record from cursor when trying to make a method to get a 2d array of strings to represent it

所以我有一个数据库,其中有一个 table,有两个字段和一条记录。我正在使用 select 从 table 中检索所有内容,并将生成的游标传递给此函数以尝试将其转换为表示 table 的锯齿状字符串数组。

public String[][] cursorTo2dString (Cursor cursor, String[] fields) {

    int columnCount = cursor.getColumnCount();
    int count = cursor.getCount();
    String[][] string2dArray = new String[columnCount][count];

    Log.i("StringLengths", count + " & " + columnCount);
    Log.i("CursorConts", DatabaseUtils.dumpCursorToString(cursor));

    if (cursor.moveToFirst()) {

        for (int j=0; j<count && !cursor.isAfterLast(); j++) {

            for (int i=0; i<columnCount; i++) {

                Log.i("Cursor", cursor.getString(i));

                string2dArray[j][i] = cursor.getString(i);

                Log.i("StringPoint", string2dArray[j][i]);
            }

            cursor.moveToNext();
        }

    }

作为参考,TagIdNo 值目前只是一个随机生成的数字,我实际上不会在实现时随机生成它,我只是将其用于测试目的。

这一直崩溃,控制台中出现以下内容:

I/StringLengths: 1 & 2
I/CursorConts: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@bd9f011
               0 {
                  TagIdNo=998
                  TagName=No Parental Permission
               }
               <<<<<
I/Cursor: 998
I/StringPoint: 998
I/Cursor: No Parental Permission
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.programming.workingpomproject, PID: 23796
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5226)
                      at android.view.View$PerformClick.run(View.java:21266)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:168)
                      at android.app.ActivityThread.main(ActivityThread.java:5845)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5226) 
                      at android.view.View$PerformClick.run(View.java:21266) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:168) 
                      at android.app.ActivityThread.main(ActivityThread.java:5845) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
                   Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
                      at com.example.programming.workingpomproject.DbHelper.cursorTo2dString(DbHelper.java:132)
                      at com.example.programming.workingpomproject.DbHelper.select(DbHelper.java:89)
                      at com.example.programming.workingpomproject.DbHelper.select(DbHelper.java:92)
                      at com.example.programming.workingpomproject.MainActivity.testDbMethodGet(MainActivity.java:66)
                      at com.example.programming.workingpomproject.MainActivity.dbStuff(MainActivity.java:61)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5226) 
                      at android.view.View$PerformClick.run(View.java:21266) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:168) 
                      at android.app.ActivityThread.main(ActivityThread.java:5845) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

我使用了很多不同的方法来获取游标的内容,但无论如何我只能得到我放入数据库的第一个值(在这种情况下是随机数)。我想知道是否有人能明白为什么我没有得到我的第二个值(目前 "no parental permission"),以及他们是否可以建议我如何检索它。

抱歉,如果我搞砸了任何编码标准;这是我的第一个 Android 项目,也是我第一次在 Java 中,但我已经在这个应用程序上工作了好几个月,所以我希望我会变得更好。

欢迎提出任何建议!

编辑 1: 如果按照 Giovanni 的建议,我在实例化中交换了列和行,我得到的是:

String[][] string2dArray = new String[count][columnCount];

这导致控制台中出现以下结果,代替其他结果:

    I/CursorConts: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@45e64e9
           0 {
              TagIdNo=3259
              TagName=No Parental Permission
           }
           <<<<<
I/Cursor: 3259
I/StringPoint: 3259
I/Cursor: No Parental Permission
I/StringPoint: No Parental Permission
I/MovePos: true & false
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.programming.workingpomproject, PID: 24606
                  java.lang.IllegalStateException: Could not execute method for android:onClick
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
                      at android.view.View.performClick(View.java:5226)
                      at android.view.View$PerformClick.run(View.java:21266)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:168)
                      at android.app.ActivityThread.main(ActivityThread.java:5845)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Method.invoke(Native Method)
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
                      at android.view.View.performClick(View.java:5226) 
                      at android.view.View$PerformClick.run(View.java:21266) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:168) 
                      at android.app.ActivityThread.main(ActivityThread.java:5845) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 
                   Caused by: android.database.CursorIndexOutOfBoundsException: Index 1 requested, with a size of 1
                      at android.database.AbstractCursor.checkPosition(AbstractCursor.java:460)
                      at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
                      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
                      at com.example.programming.workingpomproject.DbHelper.cursorTo2dString(DbHelper.java:155)
                      at com.example.programming.workingpomproject.DbHelper.select(DbHelper.java:89)
                      at com.example.programming.workingpomproject.DbHelper.select(DbHelper.java:92)
                      at com.example.programming.workingpomproject.MainActivity.testDbMethodGet(MainActivity.java:66)
                      at com.example.programming.workingpomproject.MainActivity.dbStuff(MainActivity.java:61)
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
                      at android.view.View.performClick(View.java:5226) 
                      at android.view.View$PerformClick.run(View.java:21266) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:168) 
                      at android.app.ActivityThread.main(ActivityThread.java:5845) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)

这个游标问题似乎是根本问题,数组索引 1 只是我为了避免游标错误而弄乱的东西。 非常感谢任何进一步的帮助!

你得到一个 ArrayIndexOutOfBoundsException,这意味着你要分配的数组没有你试图访问的正确大小。例如,如果我创建一个像这样的数组:

String[] array = new String[2];
int i = 3;
String s = array[i];

这将引发异常,因为我的数组只能放置 2 个对象,而不是 3 个。

你的数组是String[][] string2dArray = new String[columnCount][count];

我认为您可以通过将其更改为来修复它:

String[][] string2dArray = new String[count][columnCount];

因为这里:

for (int j = 0; j < count && !cursor.isAfterLast(); j++) {
    for (int i = 0; i < columnCount; i++) {
        Log.i("Cursor", cursor.getString(i));

        // j should be count size, see the first for loop

        string2dArray[j][i] = cursor.getString(i);
        Log.i("StringPoint", string2dArray[j][i]);
    }
    cursor.moveToNext();
}