使用数据库填充对象数组

时间:2012-06-22 10:51:06

标签: java android

我正在尝试使用从数据库查询返回的信息来创建和填充对象数组。我似乎无法使用下面的代码正常工作。

感谢任何帮助。提前谢谢。

模块对象:

public class Module {
public String moduleEnglishTitle;
public String moduleJapaneseTitle;
public String moduleCompletionRate;
public int moduleRating;
public Module() {
    super();
}

public Module(String moduleEnglishTitle, String moduleJapaneseTitle, String moduleCompletionRate, int moduleRating) {
    super();
    this.moduleEnglishTitle = moduleEnglishTitle;
    this.moduleJapaneseTitle = moduleJapaneseTitle;
    this.moduleCompletionRate = moduleCompletionRate;
    this.moduleRating = moduleRating;
}

}

对象的这四个变量将从数据库填充到要使用数据的活动中的Module对象数组中。然后使用自定义ArrayAdapter将此数据填充到ListView中。如果我使用以下方法手动创建列表,ArrayAdapter,Listview和Module对象都可以很好地协同工作:

Module module_data[] = new Module[] {
            new Module("hello","hello","hello", 3),
            new Module("hello","hello","hello", 2)
    };

但是,我不希望以这种方式填充它,我想使用这个游标来使用数据库中的数据:

public Cursor getModuleList() {
    Cursor mCursor = myDataBase.rawQuery("SELECT mod, engtitle, japtitle FROM master WHERE type='module'", null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

到目前为止,我有这段代码,但它不起作用:

    Module module_data[] = new Module[moduleInfo.getCount()];
    int i = 0;
    while(moduleInfo.isAfterLast()) {

        module_data[i].moduleJapaneseTitle = moduleInfo.getString(1);
        module_data[i].moduleEnglishTitle = moduleInfo.getString(2);
        module_data[i].moduleCompletionRate = "済み: " + moduleStats.getString(7);
        module_data[i].moduleRating = moduleStats.getInt(4);

        moduleInfo.moveToNext();
        moduleStats.moveToNext();
        i++;
    }

.................

06-22 20:42:27.045: E/AndroidRuntime(440): FATAL EXCEPTION: main
06-22 20:42:27.045: E/AndroidRuntime(440): java.lang.RuntimeException: Unable to start activity ComponentInfo{jp.atomicideas.ne/jp.atomicideas.ne.Story2}: java.lang.NullPointerException
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1659)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1675)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.ActivityThread.access$1500(ActivityThread.java:121)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.os.Looper.loop(Looper.java:130)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.ActivityThread.main(ActivityThread.java:3701)
06-22 20:42:27.045: E/AndroidRuntime(440):  at java.lang.reflect.Method.invokeNative(Native Method)
06-22 20:42:27.045: E/AndroidRuntime(440):  at java.lang.reflect.Method.invoke(Method.java:507)
06-22 20:42:27.045: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
06-22 20:42:27.045: E/AndroidRuntime(440):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
06-22 20:42:27.045: E/AndroidRuntime(440):  at dalvik.system.NativeStart.main(Native Method)
06-22 20:42:27.045: E/AndroidRuntime(440): Caused by: java.lang.NullPointerException
06-22 20:42:27.045: E/AndroidRuntime(440):  at jp.atomicideas.ne.Story2.onCreate(Story2.java:59)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-22 20:42:27.045: E/AndroidRuntime(440):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1623)
06-22 20:42:27.045: E/AndroidRuntime(440):  ... 11 more

5 个答案:

答案 0 :(得分:0)

这里有问题while(moduleInfo.isAfterLast())

将您的代码更改为以下代码段。

int i = 0;
if (moduleInfo.moveToFirst() && moduleStats.moveToFirst())
{
  do {

    module_data[i].moduleJapaneseTitle = moduleInfo.getString(1);
    module_data[i].moduleEnglishTitle = moduleInfo.getString(2);
    module_data[i].moduleCompletionRate = "済み: " + moduleStats.getString(7);
    module_data[i].moduleRating = moduleStats.getInt(4);

    moduleStats.moveToNext();
    i++;
  } while (moduleInfo.moveToNext());
}

答案 1 :(得分:0)

使用

 module_data[i] = new Module ();

“!”缺少..........

 while(!moduleInfo.isAfterLast()) {
        module_data[i] = new Module ();//<--------------------------
        module_data[i].moduleJapaneseTitle = moduleInfo.getString(1);
        module_data[i].moduleEnglishTitle = moduleInfo.getString(2);
        module_data[i].moduleCompletionRate = "済み: " + moduleStats.getString(7);
        module_data[i].moduleRating = moduleStats.getInt(4);

        moduleInfo.moveToNext();
        moduleStats.moveToNext();
        i++;
    }

答案 2 :(得分:0)

moduleInfo.isAfterLast()添加感叹号。

答案 3 :(得分:0)

试试这个,我使用 ArrayList代替......

Module mod;

ArrayList<Module> module_data = new ArrayList<Module>();

final Cursor cursor = db.query(DbHelper.TABLE_NAME, null, null, null, null, null, null,null );

while(cursor.moveToNext()){

mod = new Module();

module_data.add(new module(cursor.getString(0),cursor.getString(1),cursor.getString(2),cursor.getInt(3));

}

答案 4 :(得分:0)

我能够这样解决:

    Module module_data[] = new Module[moduleInfo.getCount()];
    int i;
    for(i=0; i<7; i++) {
        module_data[i] = new Module(moduleInfo.getString(1), moduleInfo.getString(2),  "済み: " + moduleStats.getString(7), moduleStats.getInt(4));
        moduleInfo.moveToNext();
        moduleStats.movetoNext();
    }

问题是,我通过构造函数错误地创建和使用了Module对象。

感谢大家的帮助!