Android 向listview中追加数据的问题

来源:互联网 发布:淘宝积分怎么获得 编辑:程序博客网 时间:2024/06/17 21:38
我写了一个上滑分页的例子,当上滑的时候会显示下一页内容,但是,前一页内容看不见了。只能看见一页的内容,各位帮忙分析一下。代码如下:

public class DropDownListViewDemo extends Activity {

private List<Story_entryC> story_entrys = null;
private DropDownListView articles_list = null;
public static final int MORE_DATA_MAX_COUNT = 30;
public int moreDataCount = 0;
private static final int success = 1;
private static final int fail = 0;
public int itemsize = 0;
MyUtil myUtil = new MyUtil();
private Bitmap bitmap;
private DataHandler dataHandler = new DataHandler();
MyAdapter adapter = new MyAdapter(story_entrys);
ImageLoader imageLoader = new ImageLoader(this, false);
public HashMap<String, Bitmap> imagesCache = new HashMap<String, Bitmap>();// 图片缓存

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drop_down_listview_demo);
articles_list = (DropDownListView) findViewById(R.id.list_view);
// set drop down listener
articles_list.setOnDropDownListener(new OnDropDownListener() {

@Override
public void onDropDown() {
new GetDataTask(true).execute();
}
});
new DataThread().start();
// set on bottom listener
articles_list.setOnBottomListener(new OnClickListener() {

@Override
public void onClick(View v) {
new GetDataTask(false).execute();
}
});
articles_list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ToastUtils.show(DropDownListViewDemo.this,
story_entrys.get(position - 1).getId());
Intent intent = new Intent();
intent.setClass(DropDownListViewDemo.this,
DropDownListViewDemo.class);
intent.putExtra("id", story_entrys.get(position - 1).getId());
startActivity(intent);

}
});
articles_list.setShowFooterWhenNoMore(true);
}

private class GetDataTask extends AsyncTask<Void, Void, List<Story_entryC>> {

private boolean isDropDown;

public GetDataTask(boolean isDropDown) {
this.isDropDown = isDropDown;
}

@Override
protected List<Story_entryC> doInBackground(Void... params) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
;
}
return story_entrys;
}

@Override
protected void onPostExecute(List<Story_entryC> result) {

if (isDropDown) {
moreDataCount = 0;
new DataThread().start();
story_entrys.addAll(story_entrys);
//adapter.notifyDataSetChanged();
// should call onDropDownComplete function of DropDownListView
// at end of drop down complete.
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd HH:mm:ss");
articles_list.onDropDownComplete(getString(R.string.update_at)
+ dateFormat.format(new Date()));
} else {
moreDataCount++;
new DataThread().start();
story_entrys.addAll(story_entrys);
//adapter.addNewData(story_entrys);
System.out.println("页数:" + moreDataCount + "####"
+ story_entrys.size());
if (moreDataCount >= MORE_DATA_MAX_COUNT) {
articles_list.setHasMore(false);
}
// should call onBottomComplete function of DropDownListView at
// end of on bottom complete.
articles_list.onBottomComplete();
}

super.onPostExecute(result);
}
}

private class DataHandler extends Handler {

@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case success:
MyAdapter mAdapter =new MyAdapter(story_entrys);
articles_list.setAdapter(mAdapter);
mAdapter.addNewData(story_entrys);
break;
case fail:
Toast.makeText(DropDownListViewDemo.this, "获取数据失败",
Toast.LENGTH_SHORT).show();
default:
break;
}
}

}

// 定义自己的适配器
private class MyAdapter extends BaseAdapter {
private List<Story_entryC> mData;

public MyAdapter(List<Story_entryC> data){
mData = data;
}

@Override
public int getCount() {
return mData.size();
}

@Override
public Object getItem(int position) {
return mData.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Story_entryC sc = mData.get(position);
View view = View.inflate(DropDownListViewDemo.this,
R.layout.article_item, null);
if (position % 2 == 0) {
view.setBackgroundColor(Color.rgb(251, 248, 226)); // 颜色设置
} else {
view.setBackgroundColor(Color.rgb(230, 246, 247)); // 颜色设置
}
TextView title = (TextView) view.findViewById(R.id.article_title);
title.setText(sc.getTitle());
TextView brief = (TextView) view.findViewById(R.id.article_brief);
brief.setText(sc.getBrief());
TextView cname = (TextView) view.findViewById(R.id.cname);
cname.setText(sc.getCname());
TextView view_num = (TextView) view.findViewById(R.id.view_num);
view_num.setText("阅读  " + sc.getView_num());
TextView keep_num = (TextView) view.findViewById(R.id.keep_num);
keep_num.setText("收藏  " + sc.getKeep_num());
ImageView iv = (ImageView) view.findViewById(R.id.article_pics);
bitmap = imageLoader.getBitmap(sc.getThumb());
iv.setImageBitmap(bitmap);
return view;
}
public void addNewData(List<Story_entryC> data){
mData.addAll(data);
notifyDataSetChanged();
}
}

String id = "";
String name = "";

private class DataThread extends Thread {
final int mypage = moreDataCount;

@Override
public void run() {
String address = DropDownListViewDemo.this.getResources()
.getString(R.string.article_entry_url);
try {
System.out.println("需要使用的页数是:" + mypage);
story_entrys = myUtil.getJsonStory_entry(address + "&cid=1"
+ "&page=" + mypage);
String urls = address + "&cid=1" + "&page=" + mypage;
System.out.println(urls);
Message mes = new Message();
mes.what = success;
dataHandler.sendMessage(mes);
} catch (Exception e) {
e.printStackTrace();
Message mes = new Message();
mes.what = fail;
dataHandler.sendMessage(mes);
}
super.run();

}

}
}

0 0
原创粉丝点击