本文介绍了如何检查值是ArrayList的&LT存在;&的Hashmap LT;字符串,字符串>>在android系统?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我插入数据时,数据库中没有记录它的作品。
但是,当新的数据将来自服务器,我想检查服务器记录是存在于数据库或不是,我从数据库中获得,比所有记录我使用HashMap中环这是否存在记录不是更新的记录,否则插入新记录。

下面我把我的code您可以检查

 字符串数据= loadJSONFromAsset();
            Log.e(数据, - >中+数据);
            尝试
            {                Open_Database();                JSONObject的jobject =新的JSONObject(数据);
                JSONArray jstore = jobject.getJSONArray(WS_Constant.GETALLSSID.STORETERMINALINFO);                myDBwifilist = mDB_Helper.GetAllRecord(mSQLiteDatabase,DB_Constant.TABLE.MYWIFI);
                Log.d(记录计数,----->中+ myDBwifilist.size());                // JSON数组必须> 0
                如果(jstore.length()大于0)
                {                    的for(int i = 0; I< jstore.length();我++)
                    {                        如果(myDBwifilist.size()&下; = 0)
                        {
                                mDB_Helper.Insert_MYWIFI_Table(mSQLiteDatabase,
                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.WIFINAME),
                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.STORE_ID),
                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.LONGITUDE),
                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.LATITUDE));
                        }
                        其他
                        {                                对(HashMap的<字符串,字符串>图:myDBwifilist)
                                {
                                      对于(字符串键:map.keySet())
                                      {
                                              如果(key.equals(DB_Constant.MYWIFI.TERMINAL_ID))
                                              {
                                                     如果(map.get(键).equals(jstore.getJSONObject(I).getString(WS_Constant.GETALLSSID.TERMINAL_ID)))
                                                     {
                                                        INT检查= mDB_Helper.UpdateALLSSID(mSQLiteDatabase,
                                                                jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
                                                                jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.WIFINAME),
                                                                jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.STORE_ID),
                                                                jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.LONGITUDE),
                                                                jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.LATITUDE));
                                                         Log.d(记录更新,---------->真正的+查);
                                                     }
                                                     其他
                                                     {
                                                         mDB_Helper.Insert_MYWIFI_Table(mSQLiteDatabase,
                                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.WIFINAME),
                                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
                                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.STORE_ID),
                                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.LONGITUDE),
                                                                    jstore.getJSONObject(ⅰ).getString(WS_Constant.GETALLSSID.LATITUDE));                                                         Log.d(记录更新,---------->假);
                                                     }
                                              }
                                       }
                                }                        }                    }
                }
                其他
                {
                }                Close_Database();            }
            赶上(JSONException E)
            {
                e.printStackTrace();
            }

public ArrayList<HashMap<String,String>> GetAllRecord(SQLiteDatabase db, String TableName)
      {

            ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String,String>>();

            String Query = "SELECT * FROM " + TableName;
            Cursor cursor = db.rawQuery(Query, null);
            if(cursor.getCount()<=0)
            {
                       cursor.close();
                        return data;
            }
            else
            {
                 if (cursor.moveToFirst())
                 {
                     do
                     {
                         HashMap<String, String> map = new HashMap<String, String>();
                         map.put(DB_Constant.MYWIFI.WIFINAME, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.WIFINAME)));
                         map.put(DB_Constant.MYWIFI.TERMINAL_ID, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.TERMINAL_ID)));
                         map.put(DB_Constant.MYWIFI.STORE_ID, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.STORE_ID)));
                         map.put(DB_Constant.MYWIFI.LONGITUDE, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.LONGITUDE)));
                         map.put(DB_Constant.MYWIFI.LATITUDE, cursor.getString(cursor.getColumnIndex(DB_Constant.MYWIFI.LATITUDE)));
                        data.add(map);

                     } while (cursor.moveToNext());

                 }
             }
            cursor.close();
            return data;
      }

public int UpdateALLSSID(SQLiteDatabase db,String terminalid,
                             String wifiname,
                             String storeid,
                             String longitude,
                             String latitude)
      {


          Log.e("terminalid","--->"+terminalid);

          ContentValues cv = new ContentValues();
          cv.put(DB_Constant.MYWIFI.WIFINAME,wifiname);
          cv.put(DB_Constant.MYWIFI.STORE_ID,storeid);
          cv.put(DB_Constant.MYWIFI.LONGITUDE,longitude);
          cv.put(DB_Constant.MYWIFI.LATITUDE,latitude);
          return db.update(DB_Constant.TABLE.MYWIFI, cv,DB_Constant.MYWIFI.TERMINAL_ID +" = "+terminalid, null);


      }
解决方案
Try this way,hope this will help you to solve your problem.

        boolean isNew=true;
        for (HashMap<String, String> map : myDBwifilist){
            if(map.get(DB_Constant.MYWIFI.TERMINAL_ID).equals(jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID)) && map.get(DB_Constant.MYWIFI.WIFINAME).equals(jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME))){
                isNew = false;
                break;
            }else{
                isNew = true;
            }
        }

        if(isNew){
            mDB_Helper.Insert_MYWIFI_Table(mSQLiteDatabase,
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE));

            Log.d("Record Update", "----------> false");
        }else{
            int check=mDB_Helper.UpdateALLSSID(mSQLiteDatabase,
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE),
                    jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE));
            Log.d("Record Update", "----------> True"+check);
        }

这篇关于如何检查值是ArrayList的&LT存在;&的Hashmap LT;字符串,字符串&GT;&GT;在android系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 03:11