【问题标题】:Android SQLite Only get the latest result in Looping (ArrayList,HashMap)Android SQLite 仅在 Looping (ArrayList,HashMap) 中获取最新结果
【发布时间】:2017-07-05 03:35:00
【问题描述】:

我在从 db 获取数据并迭代 arrayList 时遇到问题,例如,我在 sqlite 中插入了两个数据,我想使用循环进行选择。但我只选择了循环中的最后一行数据。任何解决此问题的建议

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

    }

【问题讨论】:

  • 光标 =db1.rawQuery(selectQuery, null);你在光标中得到了什么?两个数据还是一个?
  • @ZakiPathan 我选择了两个数据
  • 和我的 Barlist.size() 也算两个
  • 这令人困惑。 for(int i=0; i
  • 我的 hashMap 得到了两个数据,但是它得到了最新的数据并且重复了

标签: android json sqlite arraylist hashmap


【解决方案1】:

这是你的错误

移动你的hashMap = new HashMap&lt;String, String&gt;(); into while loop

然后

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);
    }

【讨论】:

  • 抱歉再次打扰,还有 1 个问题,String entity = new StringEntity(array.toString()); httppost.setEntity(实体);
  • 这是我在实体中调用数组的方式,但我仍然在数组中得到相同的结果,你知道吗?
  • 这意味着您的数据是相同的。首先确认您的数据库存储了 2 个不同的 row_data。然后是您实际将数据添加到 hashMap 然后到 ArrayList 的方式。最后解析arrayList数据并将JSONObject和JSONObject添加到JSONArray。
  • 如果您需要 JSONArray 那么为什么要混合整个代码。只需使用 JSONObject 将数据从光标添加到 JSONArray。
  • 哦,你是对的!我正在努力按照你的指示去做,谢谢!!
【解决方案2】:

将您的 hashMap = new HashMap&lt;String, String&gt;(); 移动到 while 循环中

【讨论】:

    猜你喜欢
    • 2023-04-09
    • 2014-03-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-08
    相关资源
    最近更新 更多