问题
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);
}
回答1:
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);
}
回答2:
General example to check whether the value exists in HashMap
HashMap<String, String> hMap = new HashMap<String, String>();
hMap.put("1", "One");
hMap.put("2", "Two");
hMap.put("3", "Three");
System.out.println(hMap.containsValue("Two")); // the output will be true.
If it is true then update or else insert. Modify the code according to your need.
来源:https://stackoverflow.com/questions/24283263/how-to-check-value-is-exist-in-arraylisthashmapstring-string-in-android