2014-12-07 90 views
0

我有一個從JSON中檢索對象的活動。 第一次顯示活動時,所有工作正常。JSON數據在第二次打開活動時未顯示

但是,如果用戶前進並返回並再次打開,則會顯示任何數據。

這是我的代碼:

包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

把這個

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

+0

感謝您的生命週期的概述一下,但如果我把這個放入了onresum方法,那麼當我第一次打開這個活動時,這個列表就不會顯示出來......? – novato 2014-12-07 21:24:10

+1

因此,如果它在第一次嘗試中將你的代碼切入一個方法,並在onCreate中調用它,並在onResume – tung 2014-12-08 13:02:57

相關問題