如何检查值是ArrayList的&LT存在;&的Hashmap LT;字符串,字符串>>在android系统? [英] How to check value is exist in ArrayList<Hashmap<String,String>> in android?

查看:170
本文介绍了如何检查值是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();
            }


  

//获取记录从数据库


 公开的ArrayList<&HashMap的LT;字符串,字符串>> GetAllRecord(SQLiteDatabase分贝,字符串表名)
      {            ArrayList的<&HashMap的LT;字符串,字符串>>数据=新的ArrayList<&HashMap的LT;字符串,字符串>>();            查询字符串=SELECT * FROM+表名;
            光标光标= db.rawQuery(查询,NULL);
            如果(cursor.getCount()&下; = 0)
            {
                       cursor.close();
                        返回的数据;
            }
            其他
            {
                 如果(cursor.moveToFirst())
                 {
                     做
                     {
                         HashMap的<字符串,字符串>地图=新的HashMap<字符串,字符串>();
                         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(地图);                     }而(cursor.moveToNext());                 }
             }
            cursor.close();
            返回的数据;
      }公众诠释UpdateALLSSID(SQLiteDatabase分贝,字符串terminalid,
                             串wifiname,
                             串STOREID,
                             字符串的经度,
                             串纬度)
      {
          Log.e(terminalid,--->中+ terminalid);          ContentValues​​ CV =新ContentValues​​();
          cv.put(DB_Constant.MYWIFI.WIFINAME,wifiname);
          cv.put(DB_Constant.MYWIFI.STORE_ID,STOREID);
          cv.put(DB_Constant.MYWIFI.LONGITUDE,经度);
          cv.put(DB_Constant.MYWIFI.LATITUDE,纬度);
          返回db.update(DB_Constant.TABLE.MYWIFI,CV,DB_Constant.MYWIFI.TERMINAL_ID +=+ terminalid,NULL);
      }


解决方案

 试试这个办法,希望这将帮助你解决你的问题。        布尔是否新款= TRUE;
        对(HashMap的<字符串,字符串>地图: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))){
                是否新款= FALSE;
                打破;
            }其他{
                是否新款= TRUE;
            }
        }        如果(是否新款){
            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(记录更新,---------->假);
        }其他{
            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(记录更新,---------->真正的+查);
        }

First i insert the data when no records in database it works. but when new data will come from server i want to check server record is exist in database or not for that i get all records from database and than i use hashmap loop for this if record is exist than update record otherwise insert new record

Here i put my code you can check it

String data=loadJSONFromAsset();
            Log.e("data","--->"+data);
            try 
            {   

                Open_Database();

                JSONObject jobject= new JSONObject(data);
                JSONArray jstore= jobject.getJSONArray(WS_Constant.GETALLSSID.STORETERMINALINFO);

                myDBwifilist=mDB_Helper.GetAllRecord(mSQLiteDatabase,DB_Constant.TABLE.MYWIFI);
                Log.d("Record Count","----->"+myDBwifilist.size());

                // json array must > 0
                if(jstore.length()>0)
                {

                    for(int i=0;i<jstore.length();i++)
                    {

                        if(myDBwifilist.size()<=0)
                        {   
                                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));
                        }
                        else
                        {   

                                for (HashMap<String, String> map : myDBwifilist) 
                                {
                                      for (String key : map.keySet()) 
                                      {
                                              if (key.equals(DB_Constant.MYWIFI.TERMINAL_ID)) 
                                              {
                                                     if(map.get(key).equals(jstore.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID)))
                                                     {
                                                        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);
                                                     }
                                                     else
                                                     {
                                                         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
                {


                }

                Close_Database();

            } 
            catch (JSONException e) 
            {
                e.printStackTrace();
            }

// Get Records From Database

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系统?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆