简体   繁体   English

我如何从Android中的数组contentValue获取(键,值)

[英]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.

相关问题 如何从 LinkedHashMap 中获取具有“最佳”值的键 - How can I get the keys with the 'best' values from a LinkedHashMap 如何从线程获取数组值 - how can I get array values back from a thread 如何从数组中获取唯一输入的值? - how can I get the only entered values from an array? 如何在POJO中使用动态键设置此JSON并在Android JAVA中从中获取数据? - How can i set this JSON with dynamic keys in POJO and get data from it in Android JAVA? 如何以正确的顺序从属性文件中获取包含所有键的数组? - How can I get an array containing all the Keys from a properties file in the correct sequence? 如何从另一个数组中的数组中获取数据,该数组在 Java 中存储值? - How can I get the data from an array inside another array that stores values in Java? 如何从HashMap获取值和键? - How to get values and keys from HashMap? 如何从Hashmap获取嵌套Gson键(不是键的值) - How to get Nested Gson keys (not values of keys) from Hashmap 如何在 Android 中使用 SQLite 从微调器中根据选定的月份和年份从列中获取值? - How can I get values from a column based on selected month and year from spinner using SQLite in Android? 如何从数组android studio JSONArray中的数组中获取数据 - How can i get the data from array inside an array android studio JSONArray
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM