0

我試圖顯示一組提取結果作爲列表。每當我嘗試使用虛擬數據時,它都會正確顯示,但是當我將其更改爲從服務器檢索到的數據時,什麼都不會發生。我沒有得到任何錯誤,所以這是非常混亂setListAdapter not displayed

import java.util.ArrayList; 

import org.json.JSONArray; 
import org.json.JSONException; 

import android.app.ListFragment; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 

public class BlogFragment extends ListFragment { 

    String[] countries = new String[] { 
     "India", 
     "Pakistan", 
     "Sri Lanka", 
     "China", 
     "Bangladesh", 
     "Nepal", 
     "Afghanistan", 
     "North Korea", 
     "South Korea", 
     "Japan" 
    }; 

    String [] titles ={}; 
    ArrayAdapter<String> adapter; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) { 
     /** Creating an array adapter to store the list of countries **/ 
     new AsyncFetchData().execute(); 
     adapter = new ArrayAdapter<String>(inflater.getContext(), android.R.layout.simple_list_item_1,titles); 

     return super.onCreateView(inflater, container, savedInstanceState); 
    } 

    private class AsyncFetchData extends AsyncTask 
    { 

     @Override 
     protected Object doInBackground(Object... arg0) { 


      JSONArray a = new JSONArray(); 
      ArrayList<String> aList = new ArrayList<String>(); 
      a = ServerAPI.getData(); 
      for (int i = 0; i < a.length(); i++) { 
       String title = null; 
       String content = null; 
       try { 
        title = a.getJSONObject(i).getString("title"); 
        content = a.getJSONObject(i).getString("content"); 
        aList.add(title); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       System.out.println(title); 
//    System.out.println(content); 
      } 
      titles = aList.toArray(new String[aList.size()]); 
      /** Setting the list adapter for the ListFragment */ 


      return null; 
     } 

     protected void onPostExecute(Object result) { 
     // TODO Auto-generated method stub 
     ArrayAdapter aa = new ArrayAdapter(getActivity().getBaseContext(), android.R.layout.simple_list_item_1, titles); 
     aa.notifyDataSetChanged(); 
     setListAdapter(aa); 
    } 
    } 
} 

正確地顯示在runOnUiThread法陣countries而標題沒有的字符串。我檢查放置在標題字符串數組中的數據,它是有效的。 任何想法爲什麼發生這種情況?

+0

你在'setListAdapter'調用之前嘗試'adapter.notifyDatasetChanged()'嗎? –

+0

@MysticMagic沒有任何反應。 – omarsafwany

+0

然後調用listView.invalidateViews();只是在notifyDatasetChanged後 –

回答

0

的問題是:

適配器已經用舊數據集的初始化。因此,您正在更新基礎數據,但適配器仍保留舊數據。

您需要使用新數據更新適配器。

爲此,

呼叫

adapter.notifyDatasetChanged(); 

之前

setListAdapter(adapter); 

notifyDataSetChanged: 通知所附的觀察者的基礎數據已 改變任何視圖反映數據設置應該刷新自己。

+0

我照你說的做了,但沒有任何反應。 – omarsafwany

0

首先爲新標題創建一個新適配器,然後設置該適配器。

0

有一些奇怪的事情我在你的代碼中看到:

  1. onCreateView你開始新的AsyncFetchData()執行()。如果想下載的數據爲當前列表項可能是有用的,對整個名單是在其他方式(不是內部onCreateView)製成

  2. 我希望onCreateView創造的,而不是調用super.onCreateView一個新的觀點,以訪問視圖元素(與下載的數據填充它們)

ListFragment使用的標準模式是:

public class MyListFragment extends ListFragment { 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
    super.onActivityCreated(savedInstanceState); 
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile", 
     "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X", 
     "Linux", "OS/2" }; 
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), 
     android.R.layout.simple_list_item_1, values); 
    setListAdapter(adapter); 
    } 

} 

如果要更新列表從下載的數據AsyncTask,那麼你應該更新適配器引用的數組中的數據,而無需重新創建適配器本身。並調用adapter.notifyDataChanged()一旦新的數據準備就緒,在UI線程(就像你正在嘗試做的,但通常它是在AsyncTask.postExecute()這是專門爲這種情況下。

我想你在下載之前沒有任何數據顯示,那麼你不需要顯示空的列表,可能是某種進度條。它可能是TextView「Please wait,downloaded ...」當下載完成時,您將用ListView替換TextView(或片段),可能會帶有動畫。通過這種方式,ListView將被立即創建,您不需要關心其異步下載。

新增:關於進度條 - 這將是更好的使用標準之一:

ProgressDialog progress = new ProgressDialog(this); 
progress.setMessage("Please wait, downloading..."); 
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); 
progress.setIndeterminate(true); 

AsyncTask.onPreExecute()是它的一個好地方。不要忘記在onPostExecute()