我正在做一个从json文件读取的Asynctask函数。我希望在postExecute传递上像字符串联系人的“nombre,categoria,hora,lugar ......”到另一个我有的活动。如何在postExecute上获取联系人的值?有人可以帮帮我吗?这是我的代码
private class GetProgramaSC extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
if(idioma.equalsIgnoreCase("es")){
pDialog.setMessage("Por favor espere...");
}
else{
pDialog.setMessage("Itxaron mesedez...");
}
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
try {
JSONObject jsonObj = new JSONObject(loadJSONFromAsset());
// Getting JSON Array node
eventos = jsonObj.getJSONArray("results");
// looping through All Contacts
for (int i = 0; i < eventos.length(); i++) {
JSONObject c = eventos.getJSONObject(i);
String categoria = c.getString(TAG_CATEGORIA);
String nombre = c.getString(TAG_NOMBRE);
String hora = c.getString(TAG_HORA);
String lugar = c.getString(TAG_LUGAR);
String fecha = c.getString(TAG_FECHA);
String coordenadas = c.getString(TAG_COORDENADAS);
String info = c.getString(TAG_INFO);
String imagen= c.getString(TAG_IMAGEN);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("categoria", categoria);
contact.put("nombre", nombre);
contact.put("hora", hora);
contact.put("nombre_lugar", lugar);
contact.put("fecha", fecha);
contact.put("coordenadas", coordenadas);
contact.put("info", info);
contact.put("imagen", imagen);
// adding contact to contact list
eventosList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
非常感谢
答案 0 :(得分:2)
将AsyncTask签名更改为:
private class GetProgramaSC extends AsyncTask<Void, Void, List<String>>
然后doInBackground
必须返回List。因此,在eventos
结束时返回doInBackground
,您将收到onPostExecute
。
private class GetProgramaSC extends AsyncTask<Void, Void, List<String>> {
...
@Override
protected List<String> doInBackground(Void... arg0) {
...
return eventos;
}
@Override
protected void onPostExecute(List<String> result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
答案 1 :(得分:0)
Dispatcher
稍后将hashmap添加到bundle以传递活动
尝试: 在后执行
private class GetProgramaSC extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(getActivity());
if(idioma.equalsIgnoreCase("es")){
pDialog.setMessage("Por favor espere...");
}
else{
pDialog.setMessage("Itxaron mesedez...");
}
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(String... arg0) {
return loadJSONFromAsset();
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// Dismiss the progress dialog
try {
JSONObject jsonObj = new JSONObject(result);
// Getting JSON Array node
eventos = jsonObj.getJSONArray("results");
// looping through All Contacts
for (int i = 0; i < eventos.length(); i++) {
JSONObject c = eventos.getJSONObject(i);
String categoria = c.getString(TAG_CATEGORIA);
String nombre = c.getString(TAG_NOMBRE);
String hora = c.getString(TAG_HORA);
String lugar = c.getString(TAG_LUGAR);
String fecha = c.getString(TAG_FECHA);
String coordenadas = c.getString(TAG_COORDENADAS);
String info = c.getString(TAG_INFO);
String imagen= c.getString(TAG_IMAGEN);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put("categoria", categoria);
contact.put("nombre", nombre);
contact.put("hora", hora);
contact.put("nombre_lugar", lugar);
contact.put("fecha", fecha);
contact.put("coordenadas", coordenadas);
contact.put("info", info);
contact.put("imagen", imagen);
// adding contact to contact list
eventosList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
if (pDialog.isShowing())
pDialog.dismiss();
}
}
和另一个活动
Bundle extras = new Bundle();
extras.putSerializable("HashMap",hashMap);
intent.putExtras(extras);
答案 2 :(得分:0)
传递大量的String数据无法借助于另一个活动的意图来完成,因此在解析JSON之后,您可以将它们存储在Application类中,该类可以在项目内的任何位置调用。
答案 3 :(得分:0)
使用Object扩展您的类(无论您想要什么作为结果)以获得结果
public class GetProgramaSC extends AsyncTask<Void, Void, Object> {}
使用get()方法调用asyncTask,但请记住它将在主线程上运行。
Object result = new GetProgramaSC().get();
希望它会对你有所帮助!
答案 4 :(得分:0)
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
Intent i = new Intent(getActivity, NewActivity.class);
i.putExtra("arraylist", eventolist);
startActivity(i);
}
然后关于新活动:
ArrayList<HashMap<String, String>> arraylist = (ArrayList<HashMap<String,String>>) getIntent().getSerializableExtra("arraylist");
现在只需从新活动中的arraylist中提取hashmap及其值! 例如: - arraylist.get(0).get(&#34; nombre&#34;);