从异步任务填充片段列表视图

时间:2013-01-22 19:12:24

标签: android android-asynctask

我需要从loadListTask填充listview。您将在脚本结束时看到它应该发生的位置。怎么做 ? 我尝试了几种方法,但总是一样的结果,一次崩溃。

此处的主要活动和最后的异步任务:

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) {
        }
    }
}   
}

1 个答案:

答案 0 :(得分:0)

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

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