获取数据库信息android的数组?

时间:2016-10-29 06:53:44

标签: android android-database

这是我从数据库中获取数据的代码,但是没有显示任何内容。它有什么问题

public String[] GetData() {

    String[] result =new String[]{};
    int counter=0;
    String[] Columns = new String[]{KEY_ID, KEY_NAME, KEY_PHONE, KEY_EMAIL, KEY_ADDRESS};

    Cursor cr = myDB.query(DATABASE_TABLE, Columns, null, null, null, null, null);

    int ROW_NAME = cr.getColumnIndex(KEY_NAME);
    int ROW_PHONE = cr.getColumnIndex(KEY_PHONE);
    int ROW_EMAIL = cr.getColumnIndex(KEY_EMAIL);
    int ROW_ADDRESS = cr.getColumnIndex(KEY_ADDRESS);


    for (cr.moveToFirst(); !cr.isAfterLast(); cr.moveToNext()) {
       result[counter]= cr.getString(ROW_NAME) + " " + cr.getString(ROW_PHONE) + " " + cr.getString(ROW_EMAIL) + " " + cr.getString(ROW_ADDRESS)+"\n";
        counter++;
    }
    return result;
}

1 个答案:

答案 0 :(得分:0)

以这种方式初始化结果:

String[] result =new String[cr.getCount()];