我有一个从JSON中检索对象的活动。 第一次显示活动时,一切正常。
但是如果用户前进并后退并再次打开它,则会显示任何数据。
这是我的代码:
package com.solinpromex.casajuventudtrescantos;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.StringTokenizer;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
public class DondeEsta_T1_MainActivity extends Activity {
// Declare Variables
private static final String TAG_NAME = "nombreCategoria";
private static final String TAG_ID = "idPrueba";
private String name = "Categoria";
private String id = "id";
JSONObject jsonobject;
JSONArray jsonarray;
ListView listview;
DondeEsta_T1_ListViewAdapter adapter;
ProgressDialog mProgressDialog;
ArrayList<HashMap<String, String>> arraylist;
static String ID_DES= "id_des";
static String TITULO_DES = "titulo_des";
static String CATEGORIAS_DES = "categoria_des";
static String LUGAR_DES = "lugar_des";
static String LATITUD_DES = "latitud_des";
static String LONGITUD_DES = "longitud_des";
static String IMAGEN_DES = "imagen_des";
static String DESCRIPCION_DES = "descripcion_des";
static String WEB_DES = "web_des";
static String MAIL_DES = "mail_des";
static String TEL_DES = "tel_des";
static String LUGAR_CORTO = "lugar_corto";
static String idPrueba = "idPrueba";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v("MVASCO", "context is null!");
// getting intent data
Intent in = getIntent();
// Get JSON values from previous intent
name = in.getStringExtra(TAG_NAME);
id = in.getStringExtra(TAG_ID);
idPrueba =in.getStringExtra(idPrueba);
setContentView(R.layout.dondeesta_t1_listview_main);
// Execute DownloadJSON AsyncTask
//new DownloadJSON().execute();
}
// DownloadJSON AsyncTask
private class DownloadJSON extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Create a progressdialog
}
@Override
protected Void doInBackground(Void... params) {
// Create an array
// Log.v("categoria para pasar a la URL", id_categoria_donde_esta);
arraylist = new ArrayList<HashMap<String, String>>();
// Retrieve JSON Objects from the given URL address
jsonobject = JSONfunctions
.getJSONfromURL("http://.hidden url.../app_php_files/recuperar_categorias_donde_esta_t1.php?cat="+idPrueba);
try {
// Locate the array name in JSON
jsonarray = jsonobject.getJSONArray("Categorias");
for (int i = 0; i < jsonarray.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
jsonobject = jsonarray.getJSONObject(i);
// Retrive JSON Objects
map.put("id_des", jsonobject.getString("id_des"));
map.put("titulo_des", jsonobject.getString("titulo_des"));
map.put("categoria_des", jsonobject.getString("categoria_des"));
map.put("lugar_des", jsonobject.getString("lugar_des"));
map.put("latitud_des", jsonobject.getString("latitud_des"));
map.put("longitud_des", jsonobject.getString("longitud_des"));
map.put("descripcion_des", jsonobject.getString("descripcion_des"));
map.put("web_des", jsonobject.getString("web_des"));
map.put("mail_des", jsonobject.getString("mail_des"));
map.put("imagen_des", "http://www.solinpromex.com/casajuventud/sitios/"+jsonobject.getString("imagen_des"));
map.put("tel_des", jsonobject.getString("tel_des"));
map.put("lugar_corto", jsonobject.getString("lugar_corto"));
// Set the JSON Objects into the array
arraylist.add(map);
}
} catch (JSONException e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void args) {
// Locate the listview in listview_main.xml
listview = (ListView) findViewById(R.id.listview);
// Pass the results into ListViewAdapter.java
adapter = new DondeEsta_T1_ListViewAdapter(DondeEsta_T1_MainActivity.this, arraylist);
// Set the adapter to the ListView
listview.setAdapter(adapter);
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
new DownloadJSON().execute();
}
}
我做错了什么?
答案 0 :(得分:0)
把这个
Intent in = getIntent();
// Get JSON values from previous intent
name = in.getStringExtra(TAG_NAME);
id = in.getStringExtra(TAG_ID);
idPrueba =in.getStringExtra(idPrueba);
进入方法onResume然后它应该工作我认为
另请参阅此活动生命周期概述 http://www.android-app-market.com/wp-content/uploads/2012/03/Android-Activity-Lifecycle.png