将rawQuery结果提供给主要活动的最简单方法

时间:2015-06-21 14:23:45

标签: java android

在Android Studio中,我从SQLite表中选择所有行,并希望将所有数据传递给主要活动,但应用停止,我认为这是因为我正在错误地使用数组。将rawQuery结果提供给主要活动的最简单方法是什么?

//在DatabaseAccess.java中:

public String[][] getEveryRecord() {
    Cursor cursorTwo = database.rawQuery("SELECT * FROM positionsTable", null);
    cursorTwo.moveToFirst();
    String[][] myRecords = new String[cursorTwo.getCount()][4];
    int i = 0;
    while (!cursorTwo.isAfterLast()) {
        i++;
        myRecords[i][0] = Integer.toString(cursorTwo.getInt(0));
        myRecords[i][1] = cursorTwo.getString(1);
        myRecords[i][2] = Float.toString(cursorTwo.getFloat(2));
        myRecords[i][3] = Float.toString(cursorTwo.getFloat(3));
        cursorTwo.moveToNext();
    }
    cursorTwo.close();
    return myRecords;
}

//在MainActivity.java中:

private void setUpMap() {
    DatabaseAccess databaseAccess = DatabaseAccess.getInstance(this);
    databaseAccess.open();
String[][] everyRecord = databaseAccess.getEveryRecord();
    databaseAccess.close();
    for(int row=0; row < everyRecord.length; row++) {
    String myPlace = everyRecord[row][1];
            double myLat = Double.parseDouble(everyRecord[row][2]);
            double myLng = Double.parseDouble(everyRecord[row][3]);
            mMap.addMarker(new MarkerOptions().position(new LatLng(myLat, myLng)).title(myPlace));
    }
}

非常感谢

0 个答案:

没有答案