2013-03-03 66 views
0

我從我的服務器檢索JSON數據,我想用它來更新我的ListView。一切工作正常與檢索數據,但它完成後,它試圖更新列表我只是得到一個空的頁面沒有錯誤。 Log.d("ListLocations", "after")能夠讓我看不到的SimpleAdapter爲什麼我的ListView不能用SimpleAdapter更新?

我已經適應從本教程中我的代碼以PostExecute任何問題,intiliasation運行,以及:http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/#ff0000

這是我的ListActivity類:

public class ListLocations extends ListActivity 
{ 
    //Root url and controller 
    private static String root = "someURL/"; 
    private static String locationsController = "locations"; 

    // Progress Dialog 
    private ProgressDialog pDialog; 

    //JSON node names for locations 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_TERRAIN = "terrain"; 
    private static final String TAG_DIFFICULTY = "difficulty"; 
    private static final String TAG_RATINGS = "ratings"; 
    private static final String TAG_UID = "uid"; 
    private static final String TAG_LONG= "long"; 
    private static final String TAG_LAT= "lat"; 
    private static final String TAG_DISTANCE= "distance"; 
    private static final String TAG_COMID= "comid"; 

    //Will be used to contain the list of locations extracted from JSON 
    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_list_locations); 

     new GetLocations().execute(root + locationsController); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) 
    { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.activity_list_locations, menu); 
     return true; 
    } 
    class GetLocations extends AsyncTask<String, Void, Void> 
    { 
     //  private ProgressDialog pDialog; 
     protected void onPreExecute() 
     { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(ListLocations.this); 
      pDialog.setMessage("Loading locations. Please wait..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(false); 
      pDialog.show(); 
     } 


     @Override 
     public Void doInBackground(String... urls) 
     { 
      try 
      {    
       JSONHandler jsonHandler = new JSONHandler(); 
       JSONObject json= jsonHandler.getJSONFromUrl(urls[0]); 
       Log.d("ListLocations",json.toString()); 

       JSONArray locations = json.getJSONArray("locations"); 
       int length = locations.length(); 

       ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 
       for(int i = 0; i < length; i++) 
       { 
        JSONObject loc = locations.getJSONObject(i); 

        //Create HashMap  
        HashMap<String, String> map = new HashMap<String, String>(); 

        //Get and store values into map 
        map.put("id", loc.getString(TAG_ID)); 
        map.put("name", loc.getString(TAG_NAME)); 
        map.put("distance", loc.getString(TAG_DISTANCE)); 
        map.put("difficulty", loc.getString(TAG_DIFFICULTY)); 
        map.put("terrain", loc.getString(TAG_TERRAIN)); 
        map.put("ratings", loc.getString(TAG_RATINGS)); 
        map.put("long", loc.getString(TAG_LONG)); 
        map.put("lat", loc.getString(TAG_LAT)); 
        map.put("uid", loc.getString(TAG_UID)); 
        map.put("comid", loc.getString(TAG_COMID)); 

        //Add map to list 
        list.add(map); 
       } 
       System.out.println(list); 

      } 
      catch (Exception e) 
      { 
       e.printStackTrace();    
      } 
      Log.d("ListLocations", "Successfully finished HTTP Request"); 
      return(null); 
     } 

      /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(Void result) { 
      super.onPostExecute(result); 
      pDialog.dismiss(); 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 
        Log.d("ListLocations", "inside run"); 
         ListAdapter adapter = new SimpleAdapter(
           ListLocations.this, list, 
           R.layout.list_item, new String[] { TAG_ID, 
             TAG_NAME}, 
           new int[] { R.id.id, R.id.name }); 



         Log.d("ListLocations", "after"); 
         // updating listview 
         setListAdapter(adapter); 


       } 
      }); 

     } 

    } 
} 

這是list_item.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" > 

    <!-- location id (pid) - will be HIDDEN - used to pass to other activity --> 
    <TextView 
     android:id="@+id/id" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:visibility="gone" /> 

    <!-- Name Label --> 
    <TextView 
     android:id="@+id/name" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:paddingTop="6dip" 
     android:paddingLeft="6dip" 
     android:textSize="17dip" 
     android:textStyle="bold" /> 

</LinearLayout> 

這是activity_list_locations.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <ListView 
     android:id="@android:id/list" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" /> 

</LinearLayout> 
+1

'onPostExecute()'在UI線程中運行,因此'runOnUIThread()'是多餘的。 – 2013-03-03 20:09:22

+0

我應該在哪裏調用runOnUIThread,如果有的話? – 2013-03-03 20:12:45

回答

2

你永遠不會把數據在list領域從您與SimpleAdapter使用,因爲在doInBackground方法list變量是一個局部變量,而不是該領域的Activity。相反的:

//... 
ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 
for(int i = 0; i < length; i++) { 
//... 

寫:

//... 
list = new ArrayList<HashMap<String, String>>(); 
for(int i = 0; i < length; i++) { 
//... 

,所以你不要讓一個局部變量,把要素在右側的列表。

+0

我不敢相信。 – 2013-03-03 20:13:46

+1

@JohnathanAu現在你是信徒嗎?:) – Luksprog 2013-03-03 20:16:10

+1

現在,我覺得世界是我的牡蠣,這已經結束了。 – 2013-03-03 20:18:43