每次滑动至底端,从数据库中获取10条数据,并加载于ListView中 数据库 package com.example.listviewbatchloading; impo
每次滑动至底端,从数据库中获取10条数据,并加载于ListView中
package com.example.listviewbatchloading;
import Android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class PersonDb extends SQLiteOpenHelper {
public PersonDb(Context context) {
super(context, "creature", null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table people(_id integer primary key autoincrement,name char,number char)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("drop table people");
onCreate(db);
}
}
数据库的业务封装,其中获取更多数据的是核心代码
package com.example.listviewbatchloading;
import java.util.ArrayList;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class PersonList {
PersonDb personDb;
public PersonList(Context context){
this.personDb = new PersonDb(context);
}
public List<Person> getMoreDatas(int startIndex,int num){
List<Person> list = new ArrayList<Person>();
SQLiteDatabase db = personDb.getWritableDatabase();
Cursor cursor = db.rawQuery("select name,number from people order by _id desc limit ?,?",
new String[]{startIndex + "",num + ""});
while(cursor.moveToNext()){
Person person = new Person();
person.setName(cursor.getString(cursor.getColumnIndex("name")));
person.setNumber(cursor.getString(cursor.getColumnIndex("number")));
list.add(person);
}
cursor.close();
db.close();
return list;
}
public void add(String name,String number){
SQLiteDatabase db = personDb.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put("name", name);
cv.put("number", number);
db.insert("people", null, cv);
db.close();
}
}
功能实现
package com.example.listviewbatchloading;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv ;
private List<Person> datas = new ArrayList<Person>();
private static int PERPAGE = 10; //每页加载数目
private static final int FINISH = 0;//数据加载完成
private List<Person> moreDatas;//每次加载的数据
private MyAdapter adapter;
private PersonList personList ;
//若数据较多,耗时较长,数据加载完成时,发送FINISH至handler,并通知ListView更新数据
private Handler handler = new Handler(){
public void handleMessage(android.os.Message msg) {
switch (msg.what) {
case FINISH:
if (moreDatas.size() != 0) {
System.out.println(moreDatas.toString());
adapter.notifyDataSetChanged();
}else {
Toast.makeText(MainActivity.this, "没有更多数据", Toast.LENGTH_SHORT).show();
}
break;
default:
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
initEvent();
}
private void initEvent() {
lv.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
if (scrollState == OnScrollListener.SCROLL_STATE_IDLE) {
int lastVisiblePosition = lv.getLastVisiblePosition();
if (lastVisiblePosition == datas.size() - 1) {
initData();
System.out.println("加载更多数据");
}
}
}
@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
// TODO Auto-generated method stub
}
});
}
private void initView() {
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
personList = new PersonList(getApplicationContext());
adapter = new MyAdapter();
lv.setAdapter(adapter);
}
private void initData() {
new Thread() {
public void run() {
// 加载更多数据
moreDatas = personList.getMoreDatas(datas.size(), PERPAGE);
datas.addAll(moreDatas);// 把一个容器的所有数据加进来
// 取数据完成,发消息通知取数据完成
handler.obtainMessage(FINISH).sendToTarget();
};
}.start();
}
private class ItemView{
private TextView tv_name;
private TextView tv_num;
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
// TODO Auto-generated method stub
return datas.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ItemView itemView = null;
if (convertView == null) {
itemView = new ItemView();
convertView = View.inflate(getApplicationContext(), R.layout.item_lv, null);
itemView.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
itemView.tv_num = (TextView) convertView.findViewById(R.id.tv_num);
convertView.setTag(itemView);
}else {
itemView = (ItemView) convertView.getTag();
}
Person person = datas.get(position);
itemView.tv_name.setText(person.getName());
itemView.tv_num.setText(person.getNumber());
return convertView;
}
}
}
其他Person的JavaBean,以及布局文件,不在赘述
您可能感兴趣的文章:Android实现Listview异步加载网络图片并动态更新的方法Android ListView异步加载图片方法详解Android实现ListView异步加载图片的方法Android中ListView异步加载图片错位、重复、闪烁问题分析及解决方案Android程序开发ListView+JSON+异步网络图片加载+滚动翻页的例子(图片能缓存,图片不错乱)Android实现上拉加载更多以及下拉刷新功能(ListView)基于Android ListView之加载使用技巧Android实现ListView分页自动加载数据的方法Android ListView实现上拉加载更多和下拉刷新功能Android实现ListView异步加载的方法(改进版)
--结束END--
本文标题: Android实现简单的分批加载ListView
本文链接: https://lsjlt.com/news/25423.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0