Ich habe ein Problem beim Abrufen von Daten aus db und Iterieren arrayList
, zum Beispiel habe ich zwei Daten in sqlite eingefügt und ich möchte mit Looping auswählen. aber ich habe nur die letzten Zeilendaten in meiner Schleife ausgewählt. jeder Vorschlag, dieses ProblemAndroid SQLite Nur das neueste Ergebnis in Looping (ArrayList, HashMap)
public void postDataDtl(String DocNo) {
database = new ProductDatabase.ProductDatabaseHelper(activityContext);
try{
String selectQuery = "select * from BarcodeDtl where DocNo='"+DocNo+"'";
SQLiteDatabase db1 = database.getWritableDatabase();
cursor =db1.rawQuery(selectQuery, null);
cursor.moveToFirst();
Barlist = new ArrayList<>();
hashMap = new HashMap<String, String>();
data = new JSONObject();
array = new JSONArray();
while(cursor.isAfterLast()==false) {
branch = cursor.getString(cursor.getColumnIndex("Branch"));
docno = cursor.getString(cursor.getColumnIndex("DocNo"));
time = cursor.getString(cursor.getColumnIndex("Time"));
seqno = cursor.getString(cursor.getColumnIndex("SeqNo"));
barcode = cursor.getString(cursor.getColumnIndex("Barcode"));
qty = cursor.getString(cursor.getColumnIndex("Quantity"));
hashMap.put("branch",branch);
hashMap.put("docno",docno);
hashMap.put("time",time);
hashMap.put("seqno",seqno);
hashMap.put("barcode",barcode);
hashMap.put("qty",qty);
Barlist.add(hashMap);
cursor.moveToNext();
}
//the problem is this for loop getting same result
for(int i=0; i<Barlist.size();i++) {
data.put("Branch", hashMap.get("branch"));
data.put("DocNo", hashMap.get("docno"));
data.put("CTime", hashMap.get("time"));
data.put("Seq", hashMap.get("seq"));
data.put("Barcode", hashMap.get("barcode"));
data.put("Qty", hashMap.get("qty"));
array.put(data);
}
// }
// array.put(data);
}catch (Exception e){
}
Cursor = DB1 zu lösen. rawQuery (selectQuery, null); Was bekommst du im Cursor? zwei Daten oder eins? –
@ZakiPathan ich wählte zwei Daten – AnthonyTang
und meine Barlist.size() auch als zwei – AnthonyTang