0
朋友,在方向更改後,在ListView中加載更多記錄不起作用?
我使用下面的代碼來顯示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);
}
我已經使用onSaveInstanceState方法是否需要更改此方法?我猜onConfigurationChanged是替代這種方法。 – UMAR 2011-04-22 07:04:56