如何检查ArrayList中是否存在值<;Hashmap<;字符串,字符串>&燃气轮机;在android中?
首先,当数据库中没有记录时,我插入数据。 但当新数据将来自服务器时,我想检查服务器记录是否存在于数据库中,因为我从数据库中获取所有记录,然后使用hashmap循环,如果记录存在,则使用更新记录,否则插入新记录 我把代码放在这里,你可以检查一下如何检查ArrayList中是否存在值<;Hashmap<;字符串,字符串>&燃气轮机;在android中?,android,sqlite,insert-update,Android,Sqlite,Insert Update,首先,当数据库中没有记录时,我插入数据。 但当新数据将来自服务器时,我想检查服务器记录是否存在于数据库中,因为我从数据库中获取所有记录,然后使用hashmap循环,如果记录存在,则使用更新记录,否则插入新记录 我把代码放在这里,你可以检查一下 String data=loadJSONFromAsset(); Log.e("data","--->"+data); try { Op
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();
}
String data=loadJSONFromAsset();
Log.e(“数据”、“-->”+数据);
尝试
{
打开_数据库();
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)
{
用于(int i=0;i检查值是否存在于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.
HashMap hMap=newhashmap();
hMap.put(“1”、“1”);
hMap.put(“2”、“2”);
hMap.put(“3”、“3”);
System.out.println(hMap.containsValue(“两”);//输出为true。
如果为真,则更新或插入。根据需要修改代码。尝试这种方法,希望这将帮助您解决问题。
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);
}
布尔值isNew=true;
for(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))){
isNew=false;
打破
}否则{
isNew=true;
}
}
如果(是新的){
mDB_Helper.Insert_MYWIFI_表(mSQLiteDatabase,
getJSTORE.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME),
getJSTORE.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID),
getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE),
getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE));
Log.d(“记录更新”,“------------>错误”);
}否则{
int check=mDB_Helper.UpdateALLSSID(mSQLiteDatabase,
getJSTORE.getJSONObject(i).getString(WS_Constant.GETALLSSID.TERMINAL_ID),
getJSTORE.getJSONObject(i).getString(WS_Constant.GETALLSSID.WIFINAME),
getJSONObject(i).getString(WS_Constant.GETALLSSID.STORE_ID),
getJSONObject(i).getString(WS_Constant.GETALLSSID.LONGITUDE),
getJSONObject(i).getString(WS_Constant.GETALLSSID.LATITUDE));
Log.d(“记录更新”、“------------>True”+检查);
}
仍然存在的问题是,为什么要使用else条件插入重复记录?如果不存在,我想插入新记录,但如果记录存在,则在else条件下是go。请尝试打印两个id。当前我只检查终端id。可以检查终端id和wifi名称是否都与记录的更新相同nsert新记录?现在检查我用另一个wifi名称条件编辑的ans。
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);
}