【问题标题】:saving and retrieving key-value pairs in an sqlite database android在sqlite数据库android中保存和检索键值对
【发布时间】:2012-06-29 02:47:36
【问题描述】:

美好的一天,基本上我正在寻找一种方法来检索和保存与 sqlite 数据库中的列项目相关的键值对,以便我可以更新列表视图。

示例:如果我有一个名为“group”的列,其中包含一个名为“family”的条目;关于那个项目,我希望能够为“家庭”这个项目保存或检索联系人列表(姓名和号码)。另一个项目条目相同。

最好的方法是什么?我尝试通过json使用Hashmap的ArrayList将联系人详细信息保存在表格的列中。但这样做有麻烦。我试过这个:

保存时:

//already have the name and number from using the android contacts api

    final ArrayList<HashMap<String,String>> contacts = new ArrayList<HashMap<String,String>>();     
    HashMap<String, String> map = new HashMap<String,String>();
            map.put(name, number);
            contacts.add(map);

            JSONObject json_contacts = new JSONObject();

                try {
            json_contacts.put("contacts_Arrays", new JSONArray(contacts));
                    arraylist_to_string = json_contacts.toString();

                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            ContentValues values = new ContentValues(); 
            values.put(ContactDB.GROUP, groupname);
            values.put(ContactDB.GROUP_CONTACTS, arraylist_to_string);

            Log.d(TAG, "Successfully updating contacts for group");
            dbadapter.updateGroup(rowId, values);
            cursoradapter.notifyDataSetChanged();

在我的 bindview 方法中检索时:

String contactDetails = cursor.getString(cursor.getColumnIndex(ContactDB.GROUP_CONTACTS));

        if(contactDetails != null){
            try {
                JSONObject json = new JSONObject(contactDetails);
                JSONArray json_array = json.optJSONArray("contacts_Arrays");

                for(int i=0; i < json_array.length(); i++){
    LinkedHashMap<String,String> map = new LinkedHashMap<String, String>();
            Iterator<String> myIterator = map.keySet().iterator();
                    while(myIterator.hasNext()){
                       key = (String)myIterator.next();
                       value = (String)map.get(key);

                       holder.contact_name.setText(key);
                       holder.contact_number.setText(value);                           
                       notifyDataSetChanged();

                    }
                }


            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }else{
            return;
        }
    }

根本没有任何运气,并且首先不确定它是否是正确的方法。构造数据库表的最佳方法是什么。我应该创建一个联系人对象并存储它还是有更好的方法?谢谢你

【问题讨论】:

    标签: java android sqlite


    【解决方案1】:

    仅通过查看您的代码,List 中的多个 HashMap 实例可能会占用过多内存,具体取决于存储数据的大小。

    由于您要存储键值对,因此关系数据库(如 SQLite 就是其中之一)通常不是正确的决定。 Android 有 "Shared Preferences" 来存储简单的键值对(带有原始数据类型)。


    如果您想/需要使用 SQLite 实现它,您应该采用关系方式。如果我正确理解了您的问题,您将拥有如下表格:

    Table "Groups"
    +----------+------------+
    | group_ID | group_name |
    +----------+------------+
    | 1        | Family     |
    | 2        | Work       |
    +----------+------------+
    
    Table "Contacts"
    +----------+------------+--------+
    | name     | number     | group  |
    +----------+------------+--------+
    | Jon      | 0173401... | 1      |
    | James    | 057123...  | 2      |
    | Lukas    | 012343...  | 2      |
    +----------+------------+--------+
    

    从该表中,您可以查询到“James”和“Lukas”是“Work”组的成员。然后,您可以通过简单地搜索将group-列设置为group-表中显示的值的条目来查询“工作”组的所有成员(对于“工作”组,2) .

    使用此系统,您可以添加新的组和联系人,并将联系人放入(或多个)组中。

    【讨论】:

    • 谢谢,这就是我想要的。会试一试,让你知道它是怎么回事。再次感谢
    猜你喜欢
    • 2013-06-28
    • 1970-01-01
    • 1970-01-01
    • 2018-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-24
    • 2021-07-12
    相关资源
    最近更新 更多