水平视图中的列表视图寻呼机未更新

时间:2012-07-16 10:34:03

标签: android android-listview android-viewpager

我有一个水平视图寻呼机来翻阅3个ListView。三个列表视图由数据库的三个不同表填充。问题是,当我在页面中滑动时,列表视图未加载,而是显示白屏。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setContentView(R.layout.viewp);
    MyPagerAdapter adapter = new MyPagerAdapter();
    ViewPager myPager = (ViewPager) findViewById(R.id.threepageviewer);
    myPager.setAdapter(adapter);
    myPager.setCurrentItem(1);
}

private class MyPagerAdapter extends PagerAdapter {

    public int getCount() {
        return 3;
    }

    public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        int resId = 0;
        switch (position) {
        case 0:
            resId = R.layout.left;
            ListView list = new ListView(Listing.this);
            helper = new DbHelper(Listing.this);
            dataset_cursor1 = helper.getAll1();
            startManagingCursor(dataset_cursor1);
            adapter1 = new NoteAdapter(dataset_cursor1);
            list.setAdapter(adapter1);
            adapter1.notifyDataSetChanged();

            break;
        case 1:
            resId = R.layout.middle;
            ListView list1 = new ListView(Listing.this);
            helper = new DbHelper(Listing.this);
            dataset_cursor2 = helper.getAll();
            startManagingCursor(dataset_cursor2);
            adapter2 = new NoteAdapter(dataset_cursor2);
            list1.setAdapter(adapter2);
            adapter2.notifyDataSetChanged();
            break;
        case 2:
            resId = R.layout.right;
            ListView list2 = new ListView(Listing.this);
            helper = new DbHelper(Listing.this);
            dataset_cursor3 = helper.getAll2();
            startManagingCursor(dataset_cursor3);
            adapter3 = new NoteAdapter(dataset_cursor3);
            list2.setAdapter(adapter3);
            adapter3.notifyDataSetChanged();

            break;
        }

        View view = inflater.inflate(resId, null);

        ((ViewPager) collection).addView(view, 0);

        return view;
    }

    @Override
    public void destroyItem(View arg0, int arg1, Object arg2) {
        ((ViewPager) arg0).removeView((View) arg2);

    }

    @Override
    public void finishUpdate(View arg0) {

    }

    @Override
    public boolean isViewFromObject(View arg0, Object arg1) {
        return arg0 == ((View) arg1);

    }

    @Override
    public void restoreState(Parcelable arg0, ClassLoader arg1) {

    }

    @Override
    public Parcelable saveState() {
        return null;
    }

    @Override
    public void startUpdate(View arg0) {

    }

    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }
}

这可能是什么问题。

1 个答案:

答案 0 :(得分:2)

您正在对布局进行充气,但您没有添加任何视图。你应该这样做:

public Object instantiateItem(View collection, int position) {

        LayoutInflater inflater = (LayoutInflater) collection.getContext()
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view;
        switch (position) {
        case 0:
            view =  = inflater.inflate(R.layout.left, null);
            ListView list = (ListView) view.findViewById(R.id.listviewleft); //id of your listview
            helper = new DbHelper(Listing.this);
            dataset_cursor1 = helper.getAll1();
            startManagingCursor(dataset_cursor1);
            adapter1 = new NoteAdapter(dataset_cursor1);
            list.setAdapter(adapter1);
            adapter1.notifyDataSetChanged();

            break;   
            (...)
        }


    ((ViewPager) collection).addView(view);

    return view;
}