0

我正在从表中获取数据作为列表并将其添加到子列表中,但我得到了我没想到的输出。我不知道如何解决这个问题,这样我才能得到我需要的输出。我附上了代码,请帮我解决这个问题?

public List<List<String>> fetchData(){
        List<List<String>> main= new ArrayList<List<String>>();
        List<String> sub= new ArrayList<String>();
        String qry="SELECT * FROM "+myTable+"";
        SQLiteDatabase db= this.getReadableDatabase();
        Cursor cursor=db.rawQuery(qry, null);
        if (cursor.moveToFirst()) {
            do {
                System.out.println("-TblPOmaster-");
                sub.add(cursor.getString(0));               
                sub.add(cursor.getString(1));
                sub.add(cursor.getString(2));
                main.add(sub);
                System.out.println("Data:"+main);
            } while (cursor.moveToNext());      
        }
        cursor.close();
        db.close();
        return main;
    }

Output what i'm Getting Now
Data:[[a1,a2,a3]]
Data:[[a1,a2,a3,b1,b2,b3],[a1,a2,a3,b1,b2,b3]]
Data:[[a1,a2,a3,b1,b2,b3,c1,c2,c3],[a1,a2,a3,b1,b2,b3,c1,c2,c3],[a1,a2,a3,b1,b2,b3,c1,c2,c3]]

    Output what i need
        Data:[[a1,a2,a3]]
        Data:[[a1,a2,a3],[b1,b2,b3]]
        Data:[[a1,a2,a3],[b1,b2,b3],[c1,c2,c3]]
4

1 回答 1

0

使用它,因为您必须重新引用新的数组列表

public List<List<String>> fetchData(){
    List<List<String>> main= new ArrayList<List<String>>();

    String qry="SELECT * FROM "+myTable+"";
    SQLiteDatabase db= this.getReadableDatabase();
    Cursor cursor=db.rawQuery(qry, null);
    if (cursor.moveToFirst()) {
        do {
            System.out.println("-TblPOmaster-");
            List<String> sub= new ArrayList<String>();
            sub.add(cursor.getString(0));               
            sub.add(cursor.getString(1));
            sub.add(cursor.getString(2));
            main.add(Sub);
            System.out.println("Data:"+main);
        } while (cursor.moveToNext());      
    }
    cursor.close();
    db.close();
    return main;
}
于 2012-10-31T07:34:18.550 回答