Good day, Basically i am looking for a way to retrieve and save a key-value pair with respect to a column item in sqlite database so that i can update a listview.
Example: if i have a column named “group” with an item entry named “family”; with respect to that item, i would like to be able to save or retrieve a list of contacts (name and number) just for that item “Family”. same for another item entry.
what would be the best way to do this?. i tried saving the contacts details in a column on the table, using an ArrayList of a Hashmap via json. but having troubles doing it this way. i tried this:
when saving:
//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();
when retrieving in my bindview method:
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;
}
}
Not having any luck with it at all and not sure if its the correct way to do that in the first place. what would the best way to structure the database table. should i create a contact object and store it or is there a better way? Thank you
Just by looking at your code, multiple
HashMapinstances in aListmight be a memory overkill, depending on the size of your stored data.Since you want to store key-value pairs, a relational database (like SQLite is one) is most often not the right decision. Android has the "Shared Preferences" to store simple key-value pairs (with primitive data-types).
If you want/need to implement it with SQLite, you should go the relational way. If I understood your question correctly, you would have tables like the following:
From this table, you can query that "James" and "Lukas" are members of the "Work"-group. You could then query all members of the "Work" group by simply searching for entries with the
group-column set to the value presented in thegroup-table (which is2for the "Work"-group).With this system, you can then add new groups and contacts and put contacts in on (or multiple) groups.