Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the keys from ContentValues?

Tags:

java

android

My ContentValues object has string keys, and I would like to get a String[] result having all the keys in it?

How do I iterate a ContentValues object?

EDIT 1

After getting two responses I came up with this, do you see problems with it?

        ArrayList<String> ar = new ArrayList<String>();
        ContentValues cv=data;
        Set<Entry<String, Object>> s=cv.valueSet();
        for (Entry<String, Object> entry : s) {
            ar.add(entry.getKey());
        }
        String[] projection=new String[ar.size()];
        ar.toArray(projection);
like image 998
Pentium10 Avatar asked Mar 05 '10 22:03

Pentium10


People also ask

What is the use of ContentValues object?

This class is used to store a set of values that the ContentResolver can process.

What is the ContentValues class?

ContentValues is a maplike class that matches a value to a String key. It contains multiple overloaded put methods that enforce type safety.

What is the role of the content values class and content resolver class?

ContentProvider and ContentResolver are part of android. content package. These two classes work together to provide robust, secure data sharing model among applications. ContentProvider exposes data stored in the SQLite database to other application without telling them the underlying implementation of your database.


1 Answers

Try this code. Just pass your ContentValues into the method.

public void printContentValues(ContentValues vals)
{
   Set<Entry<String, Object>> s=vals.valueSet();
   Iterator itr = s.iterator();

   Log.d("DatabaseSync", "ContentValue Length :: " +vals.size());

   while(itr.hasNext())
   {
        Map.Entry me = (Map.Entry)itr.next(); 
        String key = me.getKey().toString();
        Object value =  me.getValue();

        Log.d("DatabaseSync", "Key:"+key+", values:"+(String)(value == null?null:value.toString()));
   }
}
like image 152
Soubhab Pathak Avatar answered Sep 21 '22 06:09

Soubhab Pathak