2015-10-20 23 views
0

我通過PHP獲取數據並設置爲列表視圖.. 首先我打開應用程序,listview將顯示出來,但在調用完成並重新打開應用程序之後,列表視圖不會出現,需要調用刷新,使其重新出現..ListView在調用finish()後沒有出現,並重新打開

FeedAdapter:以列表視圖

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.tab_1, container, false); 

    tweetListView = (ListView) v.findViewById(R.id.tweetList); 


     urlString = "http://idonno.net/G.php"; //json url 
     new ProcessJSON().execute(urlString); 
     tweetItemArrayAdapter = new FeedAdapter(getActivity(), localmovies); 
    tweetListView.setAdapter(tweetItemArrayAdapter); 

return v; 


} 
+0

你的後臺任務後通知適配器完成接受的答案也是分開的AsyncTask? –

+0

不...我剛剛添加,但無法運行...我應該通知? –

+0

我想你使用的是AsyncTask。你只需要通知JSON解析過程並添加對象到arraylist完成(OnPostExecute) –

回答

1
public View getView(int position, View convertView, ViewGroup parent){ 


    View row = inflater.inflate(R.layout.row_feed, parent, false); 
    TextView username = (TextView) row.findViewById(R.id.txtUsername); 

    MovieFeed moviefeed = localmovies.get(position); 

    username.setText(moviefeed.getUsername()); 


    return row; 
} 

TAB1片段您應該實現一個asyncTask,並確保在實例化listview之前確實擁有數據。就像這個例子中的匿名內部類一樣。

@Override 
protected void onPostExecute(String result){ 
    progressDialog.dismiss(); 
    runOnUiThread(new Runnable(){ 
     public void run(){ 
      final ListView lv1 = (ListView) findViewById(R.id.listings); 
      lv1.setAdapter(new CustomListAdapter(YourActivity.this, shares)); 
     } 
    }); 

} 

如果您隨時更新列表,請在適配器上撥打notifyDataSetChanged()

你可以像這個問題How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?

+1

謝謝,我剛剛移動了我的tweetItemArrayAdapter = new FeedAdapter(getActivity(),localmovies); tweetListView.setAdapter(tweetItemArrayAdapter);從onCreate到onPostexecute,問題已經解決了。TQ這麼多 –

相關問題