如何检查给定值是否已存在于Arraylist <hashmap <key,value>&gt;?</hashmap <key,value>

时间:2014-02-16 12:49:17

标签: android arraylist hashmap contains

我有一个Arraylist<Hashmap<String,String>> val2,它从sqlite database检索数据。我使用相同的密钥名称将联系人插入数据库。现在我想检查arraylist<hashmap<>>中是否已存在给定值。我想使用contains() function。我得到的结果是假的。谁能建议我如何比较? 这是我的代码:

                  Databsehandler.java
                public void insertcontact(HashMap<String, String> queryValues) {
        SQLiteDatabase database = this.getWritableDatabase();
         ContentValues values = new ContentValues();
                 values.put("contacts", queryValues.get("c")); 
                 database.insert("userlogin", null, values);
               database.close();
             }   
                            public ArrayList<HashMap<String, String>> getAllContacts() {
                 ArrayList<HashMap<String, String>> wordList;
                wordList = new ArrayList<HashMap<String, String>>();
                 String selectQuery = "SELECT DISTINCT * FROM userlogin";
                      SQLiteDatabase database = this.getReadableDatabase();
                        Cursor cursor = database.rawQuery(selectQuery, null);
                       if (cursor.moveToFirst()) {
                         do {
                        HashMap<String, String> map = new HashMap<String, String>();

                 map.put("contacts", cursor.getString(3));
                      wordList.add(map);
         }                while (cursor.moveToNext());
       }


       return wordList;
      }                        

Contacts.java

ArrayList<HashMap<String, String>>val2=new ArrayList<HashMap<String,String>>();
val2=db.getAllContacts();
for(int i=0;i&lt;jArray.length();i++){


    JSONObject json_data = jArray.getJSONObject(i);

    s11=json_data.getString("phno");
    Log.e("phno from json",""+s11);
    Log.e("name from phone",""+name);
    if(s11.compareTo(phoneNumber)==0)
    {
        Toast.makeText(this,"value of s11"+s11,Toast.LENGTH_LONG).show();
        for(int i1=0;i1&lt;val2.size();i1++)
        {
            if(val2.get(i1).containsValue(s11))
            {
                Toast.makeText(this,"true"),Toast.LENGTH_LONG).show();
            }
        }

Datbasehandler.java

       query = "CREATE TABLE userlogin ( phoneId INTEGER PRIMARY KEY,username TEXT,userid INTEGER,contacts INTEGER)";

0 个答案:

没有答案