我需要从 loadListTask 填充列表视图。您将在脚本的末尾看到它应该发生的位置。怎么做 ?我尝试了几种方法,但结果总是一样,崩溃了。
这里的主要活动和最后的异步任务:
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadListTask myLoadListTask = new LoadListTask();
myLoadListTask.execute();
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
Fragment fragment = new SectionFragment();
Bundle args = new Bundle();
args.putInt(SectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase();
case 1:
return getString(R.string.title_section2).toUpperCase();
case 2:
return getString(R.string.title_section3).toUpperCase();
}
return null;
}
}
public static class SectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public SectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ArrayList<HashMap<String, String>> listItem;
HashMap<String, String> map;
SimpleAdapter mSchedule;
listItem = new ArrayList<HashMap<String, String>>();
map = new HashMap<String, String>();
ListView maListViewPerso = new ListView(getActivity());
mSchedule = new SimpleAdapter (getActivity(), listItem, R.layout.affichageitem,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
maListViewPerso.setAdapter(mSchedule);
switch (getArguments().getInt(
ARG_SECTION_NUMBER)) {
case 1:
map = new HashMap<String, String>();
map.put("titre", "Excel");
map.put("description", Integer.toString(getArguments().getInt(
ARG_SECTION_NUMBER)));
map.put("img", String.valueOf(R.drawable.devoir));
listItem.add(map);
case 2:
//To do
case 3:
//To do
}
return maListViewPerso;
}
}
// I NEED TO POPULATE FROM HERE
private class LoadListTask extends AsyncTask<String, Void, Integer> {
protected void onPreExecute() {
}
protected Integer doInBackground(String... params) {
return 0;
}
protected void onPostExecute(Integer result) {
if (result == 0) {
}
}
}
}