我有一个主活动和一个ListFragment,它包含一个自定义ArrayAdapter,它是该主要活动的一个标签。主要活动有一个AsyncTask,它会下拉一个xml文件,然后在应用程序启动时调用另一个类来解析它。 ListFragment数据来自AsyncTask的解析数据。我希望能够调用ListFragment的自定义适配器,以便我可以在AsyncTask的onPostExecute方法中notifyDataSetChanged。我怎么能做到这一点?
现在,当我打开应用程序时,数据会被拉出并解析,但ListView是空的,因为在AyncTask完成后没有调用notifyDataSetChanged。
代码发布如下:
MainActivity
public class MainActivity extends ActionBarActivity {
private static final String TAG = MainActivity.class.getSimpleName();
private ActionBar.Tab tab1, tab2;
private Fragment fragmentChannels = new ChannelListFragment();
private Fragment fragmentTab2 = new ChannelListFragment();
private ArrayList<Channel> mChannels;
private ArrayList<Programme> mProgrammes;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(R.string.app_name);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
actionBar.addTab(actionBar.newTab().setText(R.string.channels_title).setTabListener(new TabListener(fragmentChannels)));
actionBar.addTab(actionBar.newTab().setText("tab 2").setTabListener(new TabListener(fragmentTab2)));
// Get singleton
mChannels = ChannelStore.get(getApplicationContext()).getChannels();
// Populate singleton
populateChannels();
}
private void populateChannels() {
if (CheckNetwork.isInternetAvailable(getApplication())) {
ChannelSetup cs = new ChannelSetup();
cs.execute();
} else {
Toast.makeText(getApplication(), "No internet connection!", Toast.LENGTH_LONG).show();
}
}
private class ChannelSetup extends AsyncTask<Void, String, String> {
@Override
protected String doInBackground(Void... params) {
// download xml file
}
@Override
protected void onPostExecute(String result) {
try {
// parse the xml file and create objects from it.
// need to update the listadapter of the listfragment somehow
}
}
}
ChannelListFragment
public class ChannelListFragment extends ListFragment {
private static final String TAG = "ChannelListFragment";
private ArrayList<Channel> mChannels;
private ChannelAdapter mAdapter;
private Date mNow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.channels_title);
mNow = new Date();
mChannels = ChannelStore.get(getActivity()).getChannels();
Log.i(TAG, "" + mChannels.size() + " " + "");
mAdapter = new ChannelAdapter(mChannels);
setListAdapter(mAdapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
Channel c = (Channel) (getListAdapter()).getItem(position);
Log.i(TAG, "CH" + c.getId() + " clicked");
Intent i = new Intent(getActivity(), ChannelDetailsActivity.class);
i.putExtra(ChannelDetailsFragment.EXTRA_CHANNEL_ID, c.getId());
startActivity(i);
}
public class ChannelAdapter extends ArrayAdapter<Channel> {
public ChannelAdapter(ArrayList<Channel> channels) {
super(getActivity(), 0, channels);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// If we weren't given a view, inflate one
if (convertView == null) {
convertView = getActivity().getLayoutInflater().inflate(
R.layout.list_item_channel_table, null);
}
// do stuff with various text views, etc
return convertView;
}
public void updateListData() {
this.clear();
this.addAll(mChannels);
this.notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
我建议您在refreshChannels()
中创建公共方法ChannelListFragment
,以更新ChannelAdapter
内容(从ChannelStore
获取频道)并在{{1}之后调用}}。您可以在AsyncTask notifyDataSetChanged()
方法上调用此方法。