2017-02-16 1 views
0

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){ 

    } 
+0

Cursor = DB1 zu lösen. rawQuery (selectQuery, null); Was bekommst du im Cursor? zwei Daten oder eins? –

+0

@ZakiPathan ich wählte zwei Daten – AnthonyTang

+0

und meine Barlist.size() auch als zwei – AnthonyTang

Antwort

1

hier ist dein Fehler Ihre

Bewegen hashMap = new HashMap<String, String>(); into while loop

und dann

for(int i=0; i<Barlist.size();i++) { 
     HashMap hashMap = Barlist.get(i); // you have to get hashMap from Barlist again. 
     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); 
    } 
+0

danke für die Antwort! es ist Arbeit!! – AnthonyTang

+0

Entschuldigung für Störung erneut, 1 weitere Frage, String entity = new StringEntity (array.toString()); httppost.setEntity (Entität); – AnthonyTang

+0

So rufe ich das Array in Entity auf, aber ich bekomme immer noch das gleiche Ergebnis im Array, weißt du darüber? – AnthonyTang

0

Bewegen Sie hashMap = new HashMap<String, String>(); in while-Schleife

Verwandte Themen