Umais Jan Umais Jan - 1 year ago 56
Android Question

Android : Can ContentValues be used as Associated Arrays?

I am creating a SQLiteOpenHelper and want to send multiple column with columns as key and returned values as associated string. Cant find any associated arrays in Android except using HashMap. Can I use contentValues as associated arrays?

Answer Source

You haven't described what you are trying to achieve very well, but there are two options:

  1. Use HashMap (or any Map for that matter) - and put values in it:

    Map<String, String> values = new HashMap<String, String>();
    values.put("key1", "value1");
    values.put("key2", "value2");

    Then, when you need to get the values, retrieve them from the map.

  2. You can use ContentValues - and put values in it. The code would be pretty much the same.

Which option you choose depends on your preference and also on what you're trying to do. If inside your SQLite Helper you are planning to use these values to insert/update records in a database, then you might as well just use ContentValues - otherwise you'd need to extract them from the HashMap and put into ContentValues anyway.