protected Handler hand=new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
//totalpage.setText((currentpage+1)+"/"+pagetotal);
// totalpage.setText("12233");
lv=(ListView) findViewById(R.id.lvblack);
lv.setAdapter(new Myadapter()); //加载布局,匹配布局,构造输配器
//用handle处理数据
}
};
private TextView totalpage;
private EditText etpage;
private int count;
private int pagetotal;
private ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
dao=new BlackpeopleDao(this); //这是数据类,用于读数据的类,创建对象
totalpage=(TextView) findViewById(R.id.totalpage);
etpage=(EditText) findViewById(R.id.etpage);
setContentView(R.layout.activitysafecommand);
// for(int i=0;i<100;i++)
// {
// dao.Insert("13103701"+i, String.valueOf(new Random().nextInt(3)+1));
//
// }
InitDataBase();//初始化数据
}
private void InitDataBase() {
new Thread(){
@Override
public void run() {
list=dao.Getpagelist(pagesize, currentpage); //得到数据
pagetotal=(dao.GetPage()/pagesize)+1;
hand.sendEmptyMessage(0);
//用线程加载,防止主线程阻塞,更好显示UI
}
}.start();
}
class Myadapter extends BaseAdapter{
//适配器继承于 BaseAdapter
private ViewHolder holder;
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return list.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if(convertView==null)
{
convertView=View.inflate(Safewei_activity.this, R.layout.itemsafeactivity, null);
holder=new ViewHolder();
holder.number= (TextView) convertView.findViewById(R.id.tvphone);
holder.mode=(TextView) convertView.findViewById(R.id.tvmode);
holder.img=(ImageView) convertView.findViewById(R.id.deles);
convertView.setTag(holder);
}else{
holder=(ViewHolder) convertView.getTag();
}
holder.number.setText(list.get(position).getNumber());
// mode.setText(list.get(position).getMode());
if(list.get(position).getMode().equals("1"))
{
holder.mode.setText("电话拦截");
}else if(list.get(position).getMode().equals("2"))
{
holder.mode.setText("短信拦截");
}else if(list.get(position).getMode().equals("3"))
{
holder.mode.setText("电话拦截和短信拦截");
}
final BlackpeopleInfo infos=list.get(position);
holder.img.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number=infos.getNumber();
boolean rows=dao.Delete(number);
if(rows)
{
Toast.makeText(Safewei_activity.this, "删除成功", Toast.LENGTH_SHORT).show();
list.remove(infos);
notifyDataSetChanged();
}
else{
Toast.makeText(Safewei_activity.this, "删除失败", Toast.LENGTH_SHORT).show();
}
}
});
return convertView;
}
}
static class ViewHolder{
TextView number;//构造一个控件的类
TextView mode;
ImageView img;
}
public void previous(View view)
{
if(currentpage<=0)
{
Toast.makeText(this, "这是最上一页", Toast.LENGTH_SHORT).show();
}
else{
currentpage--;
InitDataBase();
}
}
public void next(View v){
if(currentpage>=(pagetotal-1))
{
Toast.makeText(this, "这是最后一页", Toast.LENGTH_SHORT).show();
}else{
currentpage++;
InitDataBase();
}
}
=========================================================================
数据库代码
//分页的代码 返回集合list
public ArrayList<BlackpeopleInfo> Getpagelist(int pagesize,int pagenumber)
{
ArrayList<BlackpeopleInfo> list=new ArrayList<BlackpeopleInfo>();
SQLiteDatabase db=helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select number,mode from blacknumber limit ? offset ?", new String[]{String.valueOf(pagesize),
String.valueOf(pagesize*pagenumber)});
while(cursor.moveToNext())
{
BlackpeopleInfo info=new BlackpeopleInfo();
info.setMode(cursor.getString(1));
info.setNumber(cursor.getString(0));
list.add(info);
}
cursor.close();
db.close();
return list;
}