[英]How can i get (keys,values) from an array contentvalue in android
I speak a little english, sorry if its hard to read. 我说一点英语,对不起,如果很难读。
This is my problem. 这是我的问题。 I did a class extending Asyntask to set values to a database. 我做了一个扩展Asyntask的类,以将值设置为数据库。 But the paremeter I set in generic types was ContentValues. 但是我在通用类型中设置的参数是ContentValues。
class putInformationToDB extends AsyncTask<ContentValues, Integer, Integer>
Then when I call the execute method to run it, I set a contentvalues that I made previously to get all the values from registry and dialogs. 然后,当我调用execute方法运行它时,我设置了一个先前设置的contentvalues,以从注册表和对话框中获取所有值。 But now in the method: 但是现在在方法中:
protected Integer doInBackground(ContentValues... params)
I have a problem. 我有个问题。 The paremeter is a array contentvalues, and I don't know how to take values/key back. 参数表是一个contentvalues数组,我不知道如何取回值/密钥。 If it was a contentvalues, I would take the values back with iterator, and map. 如果它是一个contentvalues,我将使用迭代器取回这些值并进行映射。 But I don't know how to transform it in a contentvalues first. 但是我不知道如何首先将其转换为contentvalues。 :-( :-(
I was thinking about to pass another kind of parameter like String and then get it with a loop and put it in a ContentValues. 我当时正在考虑传递诸如String之类的另一种参数,然后通过循环获取它并将其放入ContentValues中。
You can simply iterate over the array: 您可以简单地遍历数组:
for(ContentValues c : params){
//do something with c
}
or if you're sure that there is only an element in the array you can access it directly: 或者,如果您确定数组中只有一个元素,则可以直接访问它:
ContentValues c = params[0];
if you've passed an array. 如果您已传递数组。 You may simply get it using ` ContentValues[] arr = params[0]; 您可以使用`ContentValues [] arr = params [0];来简单地获取它。
To get the keys from ContentValues use a for to get every Key set: 要从ContentValues获取密钥,请使用for获取每个密钥集:
ContentValues c = new ContentValues();
...
...
for(String key : response.keySet()){
System.out.println("key : " + key);
}
But if you want to get the value of each key into the ContentValue , use the method get(String key) : 但是,如果要将每个键的值获取到ContentValue中 ,请使用方法get(String key) :
for(String key : response.keySet()){
System.out.println("key : " + key + " value: " + response.get(key));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.