从另一个Activity调用自定义ArrayAdapter方法
我有一个主要活动和一个包含自定义ArrayAdapter的ListFragment,它是该主要活动的选项卡。 主要活动有一个AsyncTask,它可以下拉一个xml文件,然后调用另一个类来在应用程序启动时解析它。 ListFragment数据来自AsyncTask的解析数据。 我希望能够调用ListFragment的自定义适配器,以便我可以在AsyncTask的onPostExecute方法中通知DataSetChanged。 我怎样才能做到这一点?
现在,当我打开应用程序时,数据被拉取并解析,但ListView是空的,因为在完成AyncTask之后,尚未调用notifyDataSetChanged。
代码如下所示:
主要活动
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();
}
}
}
我建议你在你的ChannelListFragment
中创建一个公共方法refreshChannels()
,它更新ChannelAdapter
内容(从ChannelStore
获取频道)并在notifyDataSetChanged()
之后调用。 你可以在你的AsyncTask onPostExecute()
方法上调用这个方法。