我有一個主要活動和一個包含作爲該主要活動選項卡的自定義ArrayAdapter的ListFragment。主要活動有一個AsyncTask,它可以下拉一個xml文件,然後調用另一個類來在應用程序啓動時解析它。 ListFragment數據來自AsyncTask的解析數據。我希望能夠調用ListFragment的自定義適配器,以便我可以在AsyncTask的onPostExecute方法中通知DataSetChanged。我怎樣才能做到這一點?從另一個活動調用自定義ArrayAdapter方法
現在,當我打開應用程序時,數據被拉取並解析,但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();
}
}
}
你如何將你的片段附加到你的活動? – mmlooloo 2014-08-31 19:30:25
我在第一個代碼片段中設置了導航模式後,通過actionbar.addTab()附加了片段 – aaearon 2014-08-31 20:03:00