listview分页

lxz_jxmz1125 2011-12-12 04:05:21
package test.test;

import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class TestActivity extends Activity {
ListView lv;
Button btnLeft, btnRight;

View.OnClickListener cl;

MoreAdapter ma;

String[] data = {
"0","1","2","3","4","5","6","7","8","9","10",
"11","12","13","14","15","16","17","18","19","20",
"21","22","23","24","25","26","27","28","29","30",
"31","32","33","34","35","36","37","38","39","40",
"41","42","43","44","45","46","47","48","49","50",
"51","52","53","54","55","56","57","58","59","60",
"61","62","64","64","65","66","67","68","69","70",
"71","72","73","74","75","76","77","78","79","80",
"81","82","83","84","85","86","87","88","89","90",
"91","92","93","94","95","96","97","98","99","100"
};

//用于显示每列5个Item项。
int VIEW_COUNT = 5;

//用于显示页号的索引
int index = 0;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

//加载Listview和2个Button
initView();

//设置ListView的Adapter
ma = new MoreAdapter(this);
lv.setAdapter(ma);


cl = new Button.OnClickListener(){
public void onClick(View v) {

switch(v.getId()){
case R.id.btnLeft:
leftView();
break;

case R.id.btnRight:
rightView();
break;
}
}

};

//添加2个Button的监听事件。
btnLeft.setOnClickListener(cl);
btnRight.setOnClickListener(cl);

//检查2个Button是否是可用的
checkButton();

}

public void initView(){
lv = (ListView)findViewById(R.id.list);

btnLeft = (Button)findViewById(R.id.btnLeft);
btnRight = (Button)findViewById(R.id.btnRight);

}

//点击左边的Button,表示向前翻页,索引值要减1.
public void leftView(){
index--;

//刷新ListView里面的数值。
ma.notifyDataSetChanged();

//检查Button是否可用。
checkButton();
}

//点击右边的Button,表示向后翻页,索引值要加1.
public void rightView(){
index++;

//刷新ListView里面的数值。
ma.notifyDataSetChanged();

//检查Button是否可用。
checkButton();
}

public void checkButton(){
//索引值小于等于0,表示不能向前翻页了,以经到了第一页了。
//将向前翻页的按钮设为不可用。
if(index <=0){
btnLeft.setEnabled(false);
}
//值的长度减去前几页的长度,剩下的就是这一页的长度,如果这一页的长度比View_Count小,表示这是最后的一页了,后面在没有了。
//将向后翻页的按钮设为不可用。
else if(data.length - index*VIEW_COUNT <= VIEW_COUNT){
btnRight.setEnabled(false);
}

//否则将2个按钮都设为可用的。
else {
btnLeft.setEnabled(true);
btnRight.setEnabled(true);
}

}

//ListView的Adapter,这个是关键的导致可以分页的根本原因。
public class MoreAdapter extends BaseAdapter {
Activity activity;

public MoreAdapter(Activity a){
activity = a;
}


//设置每一页的长度,默认的是View_Count的值。
public int getCount() {
// TODO Auto-generated method stub
//return data.length;

//ori表示到目前为止的前几页的总共的个数。
int ori = VIEW_COUNT * index;

//值的总个数-前几页的个数就是这一页要显示的个数,如果比默认的值小,说明这是最后一页,只需显示这么多就可以了
if(data.length - ori < VIEW_COUNT ){
return data.length - ori;
}
//如果比默认的值还要大,说明一页显示不完,还要用换一页显示,这一页用默认的值显示满就可以了。
else {
return VIEW_COUNT;
}

}

public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}

public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
//return addTestView(position);

TextView tv = new TextView(activity);
tv.setGravity(Gravity.CENTER);
//TextView要显示的是当前的位置+前几页已经显示的位置个数的对应的位置上的值。
tv.setText(data[position+index*VIEW_COUNT]);
return tv;

}
}
}
================================================
上述代码我在网上找到的我想问一下,如果listview里德数据是从数据库里取出来的,String[] data ={}里的内容应该怎样写呢
...全文
102 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
King_at_csdn 2011-12-12
  • 打赏
  • 举报
回复
sqlite有limit关键字做分页查询功能。每次点击按钮就去查数据库再传你查出要显示页的内容呀~

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧