Call custom ArrayAdapter method from another Activity
I have a main activity and a ListFragment containing a custom ArrayAdapter that is a tab of that main activity. The main activity has an AsyncTask that pulls down an xml file then calls another class to parse it when the app starts. The ListFragment data comes from the parsed data that the AsyncTask. I want to be able to call the custom adapter of the ListFragment so that I can notifyDataSetChanged in the onPostExecute method of the AsyncTask. How can I accomplish this?
Right now, when I open the app, the data gets pulled and parsed but the ListView is empty because notifyDataSetChanged hasn't been called after the AyncTask is done.
Code is posted below:
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();
}
}
}
I suggest you to create a public method refreshChannels()
in your ChannelListFragment
that updates the ChannelAdapter
content (getting the channels from the ChannelStore
) and calling after notifyDataSetChanged()
. You could call this method on your AsyncTask onPostExecute()
method.
上一篇: C ++