我正在将数据表格作为列表提取并将其添加到子列表中,但我得到的输出是我没想到的。我不知道如何修复,以便我可以得到我需要的输出 我附上代码请帮我解决这个问题??
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]]
答案 0 :(得分:0)
if (cursor.moveToFirst()) {
do {
List<String> sub= new ArrayList<String>();
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;
}
尝试在光标内声明子列表,它将正常工作