我通过PHP获取数据并设置为listview .. 首先我打开应用程序,列表视图将显示出来,但在调用完并重新打开应用程序后,列表视图将不会出现,需要调用刷新才能重新显示..
FeedAdapter:
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;
}
带有listview的Tab1片段
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 :(得分:1)
您应该实现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()
。
您可以将asyncTask与此问题How to get the result of OnPostExecute() to main activity because AsyncTask is a separate class?
的已接受答案分开