在方向更改后,在ListView中加载更多记录不起作用?

时间:2011-04-21 13:00:01

标签: android listview

朋友,

我使用下面的代码在listview中显示几条记录,然后按“Button”加载更多记录。

除非我改变方向,

活动按预期正常工作。 当我改变方向时,它会停止向listview加载更多记录。

任何人都指导我在这做什么错误?

ListView listView;
    MediaItem myData;
    MediaItemAdapter EfficientAdapter;
    List<MediaItem> myItemstoLoad;

@Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        if(myItemstoLoad == null)
        myItemstoLoad = new ArrayList<MediaItem>();

        if(savedInstanceState != null)
        {
            myData =(MediaItem)savedInstanceState.getSerializable("Obj");
        }else
        {

                myData = new MediaItem();
                MediaItem md1= new MediaItem();
                md1.setName("Media item 1");
                md1.setThumbnail("http://screenshots.en.softonic.com/en/scrn/36000/36098/2_icon1.jpg");
                myItemstoLoad.add(md1);
                md1= new MediaItem();
                md1.setName("Media item 2");
                md1.setThumbnail("http://www.jellymuffin.com/icons/love/images/icon105.jpg");
                myItemstoLoad.add(md1);
                md1= new MediaItem();
                md1.setName("Media item 3");
                md1.setThumbnail("http://www.mediaups.com/wp-content/uploads/2007/08/iconsetpreview.png");
                myItemstoLoad.add(md1);
                md1= new MediaItem();
                md1.setName("Media item 4");
                md1.setThumbnail("http://images.motorcycle-superstore.com/ProductImages/100/2009_Icon_Merc_Hat.jpg");
                myItemstoLoad.add(md1);
                myData.setList(myItemstoLoad);
        }

        EfficientAdapter = new MediaItemAdapter(this,myData.getList());
        getListView().setAdapter(EfficientAdapter);

       Button btn=(Button)findViewById(R.id.loadMore);
       btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            AddMoreRecordsToListView();

        }
    });

    }

public class MediaItem implements Serializable
    {
        String thumbnail,name;
        List<MediaItem> listMediaItem;


        public List<MediaItem> getList() {
            return listMediaItem;
        }

        public void setList(List<MediaItem> myItemstoLoad) {
            this.listMediaItem = myItemstoLoad;
        }

        public String getThumbnail() {
            return thumbnail;
        }

        public void setThumbnail(String thumbnail) {
            this.thumbnail = thumbnail;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }




    }


    public void AddMoreRecordsToListView()
    {

        MediaItem md1= new MediaItem();
        md1.setName("Media item 5");
        md1.setThumbnail("http://media.myspacepimper.com/content/image/icon/icon_221.gif");
        myItemstoLoad.add(md1);
        md1= new MediaItem();
        md1.setName("Media item 6");
        md1.setThumbnail("http://www.funmunch.com/icon/birthday/birthday_icon13.gif");
        myItemstoLoad.add(md1);
        md1= new MediaItem();
        md1.setName("Media item 7");
        md1.setThumbnail("http://eurec.files.wordpress.com/2008/10/100px-rss-feed-icon.png");
        myItemstoLoad.add(md1);
        md1= new MediaItem();
        md1.setName("Media item 8");
        md1.setThumbnail("http://simply-audio.net/iconcenter/categories/Misc/th_Icon3.png");
        myItemstoLoad.add(md1);
        myData.setList(myItemstoLoad);

        EfficientAdapter.notifyDataSetChanged();

    }


@Override
    protected void onSaveInstanceState(final Bundle outState) {
        outState.putSerializable("Obj", myData);
    }

1 个答案:

答案 0 :(得分:0)

旋转屏幕后onCreate()方法正在调用。如果您不想要此行为,则必须处理旋转操作。首先将android:configChanges="keyboardHidden|orientation" 添加到Activity的manifest节点。然后在Activity中覆盖onConfigurationChanged方法并调用setContentView以强制GUI以新方向重新完成。