标签:数据库分页查询 android 数据库 界面 异步加载
1、数据库
package com.example.yqqmobilesafe.db; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class RubishSmsDbHelper extends SQLiteOpenHelper { private static String DATABASE_NAME="rubishSms.db"; private static int VERSION=2; public RubishSmsDbHelper(Context context) { super(context, DATABASE_NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table rubish_sms(_id integer primary key autoincrement ,phone_number text not null,sms_content text not null)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("drop table if exists rubish_sms"); onCreate( db); } }
package com.example.yqqmobilesafe.db.dao; import java.util.ArrayList; import java.util.List; import org.apache.http.auth.NTCredentials; import android.R.integer; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.util.Log; import com.example.yqqmobilesafe.db.RubishSmsDbHelper; import com.example.yqqmobilesafe.domain.RubishSmsInfo; public class RubishSmsInfoDao { private boolean D=true; private RubishSmsDbHelper mDbHelper; public RubishSmsInfoDao(Context context) { mDbHelper=new RubishSmsDbHelper(context); } /** * 添加垃圾短信信息 * @param info */ public void addSmsInfos(RubishSmsInfo info ){ SQLiteDatabase database=null; try { database=mDbHelper.getWritableDatabase(); database.execSQL("insert into rubish_sms(phone_number,sms_content) values(?,?)",new String[]{info.getPhoneNumber(),info.getSmsContent()}); database.close(); } catch (Exception e) { if(D){ Log.i("RubishSmsInfoDao","数据库插入失败!!!"); } } } /** * 删除垃圾短信 * @param info */ public void deleteSmsInfos(RubishSmsInfo info ){ SQLiteDatabase database=null; try { database=mDbHelper.getWritableDatabase(); database.execSQL("delete from rubish_sms where phone_number=?",new String[]{info.getPhoneNumber()}); database.close(); } catch (Exception e) { if(D){ Log.i("RubishSmsInfoDao","数据库垃圾短信信息删除失败!!!"); } } } /** * 分页查询 * @param number * @param offset * @return */ public List<RubishSmsInfo> findInfosbyPage(int number,int offset){ SQLiteDatabase database=null; Cursor cursor=null; List<RubishSmsInfo> infos=new ArrayList<RubishSmsInfo>(); try { database=mDbHelper.getWritableDatabase(); cursor=database.rawQuery("select phone_number,sms_content from rubish_sms limit ? offset ?",new String[]{number+"",offset+""}); while(cursor.moveToNext()){ RubishSmsInfo info=new RubishSmsInfo(); info.setPhoneNumber(cursor.getString(0)); info.setSmsContent(cursor.getString(1)); infos.add(info); info=null; } cursor.close(); database.close(); } catch (Exception e) { if(D){ Log.i("RubishSmsInfoDao","数据库垃圾短信信息分页查询失败!!!"); } } return infos; } /** * 获得记录的最大数目 * @return */ public int getMaxCount(){ SQLiteDatabase database=null; Cursor cursor=null; int num=0; try { database=mDbHelper.getReadableDatabase(); cursor=database.rawQuery("select phone_number,sms_content from rubish_sms",null); while(cursor.moveToNext()){ num++; } } catch (Exception e) { if(D){ Log.i("RubishSmsInfoDao","数据库垃圾短信信息数量统计失败!!!"); } } return num; } }
2、界面
package com.example.yqqmobilesafe; import java.util.List; import android.app.Activity; import android.app.Dialog; import android.os.AsyncTask; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.OnScrollListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.example.yqqmobilesafe.db.dao.RubishSmsInfoDao; import com.example.yqqmobilesafe.domain.RubishSmsInfo; public class RubishSmsActivity extends Activity { private ListView mRubishSms;//显示垃圾短信的列表 private List<RubishSmsInfo> mInfos;//垃圾短信信息 private RubishSmsInfosAdapter mAdapter;//垃圾短信信息适配器 private int maxNum=5;//分页加载每页的最大数目 private int offset=0;//偏移 private int totalNums;//垃圾短信的总数目 private RubishSmsInfoDao mRubishSmsInfoDao;//垃圾短信信息的数据库表操作对象 private View loading;//数据加载提示 public RubishSmsActivity() { } @Override protected void onCreate(Bundle savedInstanceState) { init(); setListener(); super.onCreate(savedInstanceState); } private void init() { setContentView(R.layout.activity_rubish_sms); mRubishSms=(ListView) findViewById(R.id.lv_rubish_sms_content); mRubishSmsInfoDao=new RubishSmsInfoDao(getApplicationContext()); totalNums=mRubishSmsInfoDao.getMaxCount(); loading=findViewById(R.id.ll_rublish_sms_info_loading); } /** * 设置监听事件 */ private void setListener(){ //下拉列表的时候分页加载数据 mRubishSms.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { switch (scrollState) { //在下拉列表空闲的时候显示数据 case OnScrollListener.SCROLL_STATE_IDLE: //获得分页加载的每页最大值 int position=mRubishSms.getLastVisiblePosition(); int total=0; if(mInfos!=null){ // total=maxNum; total=mInfos.size(); } if(position==total-1){ //到达该分页加载的末尾位置 offset+=maxNum; if(offset>totalNums){ Toast.makeText(RubishSmsActivity.this,"数据已经加载完,没有更多的数据了...", 300).show(); return; } //异步任务加载数据 fillData(); } break; } } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub } }); fillData(); //每个条目绑定监听 mRubishSms.setOnItemClickListener(new OnItemClickListener() { View m_view=View.inflate(RubishSmsActivity.this,R.layout.show_rubish_sms_operation,null); @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //获得条目对应的每个对象 Object object=mRubishSms.getItemAtPosition(position); if(object instanceof RubishSmsInfo){ final RubishSmsInfo info=(RubishSmsInfo) object; //对对象的操作1、删除2、恢复到收件箱3、加入黑名单 Dialog builder=new Dialog(RubishSmsActivity.this); builder.setTitle("提示"); builder.setContentView(m_view); //删除 ((TextView)m_view.findViewById(R.id.tv_delete)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mInfos.remove(info); if(mRubishSmsInfoDao==null){ mRubishSmsInfoDao=new RubishSmsInfoDao(getApplicationContext()); } mRubishSmsInfoDao.deleteSmsInfos(info); if(mAdapter==null){ mAdapter=new RubishSmsInfosAdapter(); mRubishSms.setAdapter(mAdapter); }else{ mAdapter.notifyDataSetChanged(); } } }); //恢复到收件箱 ((TextView)m_view.findViewById(R.id.tv_recovery_sms)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); //加入黑名单 ((TextView)m_view.findViewById(R.id.tv_add_black_number)).setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { } }); builder.show(); } } }); } /** * 异步任务给列表加载数据 */ private void fillData(){ new AsyncTask<Void,Void,Void>(){ @Override protected void onPreExecute() { loading.setVisibility(View.VISIBLE); super.onPreExecute(); } @Override protected void onPostExecute(Void result) { loading.setVisibility(View.INVISIBLE); //刷新界面列表数据 if(mAdapter==null){ mAdapter=new RubishSmsInfosAdapter(); mRubishSms.setAdapter(mAdapter); }else{ mAdapter.notifyDataSetChanged(); } super.onPostExecute(result); } @Override protected Void doInBackground(Void... params) { if(mInfos==null){ mInfos=mRubishSmsInfoDao.findInfosbyPage(maxNum, offset); }else{ mInfos.addAll(mRubishSmsInfoDao.findInfosbyPage(maxNum, offset)); } try { Thread.sleep(2000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } }.execute(); } /** * 垃圾短信显示的数据适配器 * @author yqq * */ private class RubishSmsInfosAdapter extends BaseAdapter{ @Override public int getCount() { return mInfos.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return mInfos.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) { View view=null; ViewHolder holder=null; if(convertView!=null&& convertView instanceof LinearLayout){ view=convertView; holder=(ViewHolder) view.getTag(); }else{ holder=new ViewHolder(); view=View.inflate(RubishSmsActivity.this,R.layout.rubish_sms_info_item,null); holder.tvPhoneNumber=(TextView) view.findViewById(R.id.tv_phone_number); //holder.tvDate=(TextView) view.findViewById(R.id.tv_received_date); holder.tvContent=(TextView) view.findViewById(R.id.tv_rubish_sms_content); view.setTag(holder); } holder.tvPhoneNumber.setText("电话号码:"+mInfos.get(position).getPhoneNumber()); //holder.tvDate.setText(mInfos.get(position).getReceivedDate()); holder.tvContent.setText("短信内容:"+mInfos.get(position).getSmsContent()); return view; } } static class ViewHolder{ TextView tvPhoneNumber;//电话号码显示 //TextView tvDate;//日期显示 TextView tvContent;//短信内容显示 } }
标签:数据库分页查询 android 数据库 界面 异步加载
原文地址:http://blog.csdn.net/u014600432/article/details/41440535