1

我有一个水平视图寻呼机来翻阅 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;
    }
}

这里可能是什么问题。

4

1 回答 1

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;
}
于 2012-07-16T10:48:51.380 回答