从java android中的函数返回一个数组

时间:2014-04-24 17:57:07

标签: java android arrays return

如何从class.i尝试返回一个数组,但它显示错误

public String gettobill(String bill) {
    // TODO Auto-generated method stub
    String col[]= new String[] { KEY_EBID, KEY_EMONTH, KEY_EBREAD, KEY_EBAMOUNT };
    Cursor l=ourDatabase.query(DATABASE_TABLE1, col, KEY_EBID + "='" + bill + "'", null, null, null, null);

    String[] eresult;
    int peid=l.getColumnIndex(KEY_EBID);
    int pemonth=l.getColumnIndex(KEY_EMONTH);
    int peread=l.getColumnIndex(KEY_EBREAD);
    int pamount=l.getColumnIndex(KEY_EBAMOUNT);

    for (l.moveToFirst(); !l.isAfterLast(); l.moveToNext()){
        eresult[0]= l.getString(peid);
        eresult[1]=l.getString(pemonth);
        eresult[2]=l.getString(peread);
        eresult[4]=l.getString(pamount);
    }
    return eresult;

}

2 个答案:

答案 0 :(得分:0)

与评论中一样,查看教程并查看错误消息,它通常会告诉您出了什么问题

public String[] gettobill(String bill) {
        // TODO Auto-generated method stub
        String col[]= new String[] { KEY_EBID, KEY_EMONTH, KEY_EBREAD, KEY_EBAMOUNT };
        Cursor l=ourDatabase.query(DATABASE_TABLE1, col, KEY_EBID + "='" + bill + "'", null, null, null, null);

    String[] eresult = new String[5];//initialize your array before you can populate it otherwise eresult will be null and throw an exception
    int peid=l.getColumnIndex(KEY_EBID);
    int pemonth=l.getColumnIndex(KEY_EMONTH);
    int peread=l.getColumnIndex(KEY_EBREAD);
    int pamount=l.getColumnIndex(KEY_EBAMOUNT);

    for (l.moveToFirst(); !l.isAfterLast(); l.moveToNext()){
        eresult[0]= l.getString(peid);
        eresult[1]=l.getString(pemonth);
        eresult[2]=l.getString(peread);
        eresult[3]=l.getString(pamount);
    }
    return eresult;

}

答案 1 :(得分:0)

变化

public String gettobill(String bill)

public String[] gettobill(String bill)