为什么每次上滑分页后只显示最后一页呢?上一页就不显示了,应该是追加的

本文介绍了一个使用Android平台的DropDownListView组件实现下拉刷新和底部加载更多功能的应用案例。该案例展示了如何通过自定义适配器来展示数据,并利用异步任务进行数据加载,同时实现了下拉刷新和底部加载更多的监听器。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class DropDownListViewDemo extends Activity {

private List 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 imagesCache = new HashMap();// 图片缓存

@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> {

private boolean isDropDown;

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

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

@Override
protected void onPostExecute(List 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 mData;

public MyAdapter(List 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 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();

}

}
}       
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值