gpt4 book ai didi

java - 如何从另一个函数获取java中的hashmap键和值

转载 作者:行者123 更新时间:2023-12-02 05:35:19 25 4
gpt4 key购买 nike

我有一个像这样的 json 解析

public class SecondFragment extends Fragment implements OnClickListener {


// URL to get contacts JSON
private static String contestUrl = "http://api.apps.com/contest";

// JSON Node names
private static final String TAG_ITEM_ID = "id";
private static final String TAG_URL = "url";
private static final String TAG_START_DATE = "start_date";
private static final String TAG_END_DATE = "end_date";

// contacts JSONArray
JSONArray foods = null;

// Hashmap for ListView
ArrayList<HashMap<String, String>> foodslist;



}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);

foodslist = new ArrayList<HashMap<String, String>>();




// Calling async task to get json
new GetContacts().execute();

}

/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();

// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(contestUrl, ServiceHandler.GET);

Log.d("Response: ", "> " + jsonStr);

if (jsonStr != null) {
try {

foods = new JSONArray(jsonStr);

// looping through All Contacts
for (int i = 0; i < foods.length(); i++) {
JSONObject c = foods.getJSONObject(i);

String id = c.getString(TAG_ITEM_ID);
String name = c.getString(TAG_URL);
String email = c.getString(TAG_START_DATE);
String address = c.getString(TAG_END_DATE);

// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();

// adding each child node to HashMap key => value
contact.put(TAG_ITEM_ID, id);
contact.put(TAG_URL, name);
contact.put(TAG_START_DATE, email);
contact.put(TAG_END_DATE, address);

String start_date = (String)contact.get(TAG_ITEM_ID);

// adding contact to contact list
foodslist.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}

return null;
}
}

}

我有两个函数,onCreateGetContacts。在 onCreate 的末尾,它调用 GetContacts 并调用此 json。

我的问题是,如何在 GetContacts 上获取 Hashmap 值,以便我可以在 onCreate 上使用它

到目前为止,我得到了这个来获取Hashmap的值

String start_date = (String)contact.get(TAG_ITEM_ID);

但是,它仅适用于 GetContacts。有人可以帮助我吗?

谢谢

最佳答案

有几种方法:

方式 1:创建 HashMap 联系人的实例变量(类级别),然后您可以在类中的任何位置使用它,包括 onCreate 和 getContacts 方法。

package stackoverflow.q_25034927;

import java.util.HashMap;
import java.util.Map;

public class PassVariable {
private static Map<String, String> contact = new HashMap<String, String>();

public void onCreate() {
//populate contact object as per your logic
getContacts();
}

private void getContacts() {
//Use contact object directly which was pre-populby onCreate method.
}
}

方式 2:将 map 传递给 getContacts() 方法:

package stackoverflow.q_25034927;

import java.util.HashMap;
import java.util.Map;

public class PassVariable {

public void onCreate() {
final Map<String, String> contact = new HashMap<String, String>();
//populate contact object as per your logic.
getContacts(contact);
}

private void getContacts(Map<String, String> contact) {
//Use contact object which is passed as argument.
}
}
<小时/>

另一方面,在命名 Java 方法或变量时请使用 comeCasing。 GetContacts 不对,改为 getContacts。

对于电子邮件和地址字段,使用TAG_START_DATE和TAG_END_DATE毫无乐趣。 :-)

String email = c.getString(TAG_START_DATE);
String address = c.getString(TAG_END_DATE);
<小时/>

要回答下面的问题 3,变量 s 在 NotInner 类中不可访问:

package com.test;

public class Test {
static String s = "";
}

class NotInner {
public static void main(String[] args) {
System.out.println(s); //Compilation error: s cannot be resolved to a variable
}
}

关于java - 如何从另一个函数获取java中的hashmap键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25034927/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com