0

我需要从 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) {
        }
    }
}   
}
4

1 回答 1

0

您必须保留对 listItem 和 maListViewPerso 的引用才能在 onPostExecute 中使用它们,您可以通过将它们声明为类的成员来实现。

之后,您必须创建适配器。(适配器)对于简单的数据结构,您可以使用 Android 的预定义之一,例如ArrayAdapterSimpleCursorAdapter但我想您要做的是创建一个列表,该列表遍历 ArrayList 然后进入地图的项目. 为此,您必须实现自己的适配器,扩展 BaseAdapter。实现getView(int pos)您必须返回位置pos中的项目,并且使用getCount() ListView 将知道您的列表有多少项目。

于 2013-01-22T19:37:13.273 回答