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);
This class is used to store a set of values that the ContentResolver can process.
ContentValues is a maplike class that matches a value to a String key. It contains multiple overloaded put methods that enforce type safety.
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.
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()));
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With