Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android分頁查詢垃圾短信數據庫信息

android分頁查詢垃圾短信數據庫信息

編輯:關於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 findInfosbyPage(int number,int offset){
		
		SQLiteDatabase database=null;
		Cursor cursor=null;
		List infos=new ArrayList();
		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 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(){
			
			@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;//短信內容顯示
		
	}
	

}


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved