java 如何从 ContentValues 中获取键?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the keys from ContentValues?
提问by Pentium10
My ContentValues object has string keys, and I would like to get a String[] result having all the keys in it?
我的 ContentValues 对象有字符串键,我想得到一个包含所有键的 String[] 结果?
How do I iterate a ContentValues object?
如何迭代 ContentValues 对象?
EDIT 1
编辑 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);
采纳答案by RickNotFred
According to the doc, the "valueSet()" method returns a set of all keys and values. You can then use an iterator on the resultant Set and getKey() on each of the iterated Entry elements to collect into a String array.
根据文档,“valueSet()”方法返回一组所有键和值。然后,您可以在结果 Set 上使用迭代器,并在每个迭代的 Entry 元素上使用 getKey() 以收集到 String 数组中。
回答by Soubhab Pathak
Try this code. Just pass your ContentValuesinto the method.
试试这个代码。只需将您的ContentValues传入方法即可。
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()));
}
}
回答by Apurva Sharma
You can try this one also:
你也可以试试这个:
String ar [] = {};
ContentValues cv=new ContentValues();
int i=0;
for(String key: cv.keySet()){
ar[i] = key;
}

