标签:who led while todo auto inf ret ddb static
CallSafeActivity .java
public class CallSafeActivity extends Activity { private ListView list_view; private List<BlackNumberInfo> blackNumberInfos; private LinearLayout ll_pb; private BlackNumberDao dao; private CallSafeAdapter adapter; /** * 当前页面 */ private int mCurrentPageNumber = 0; /** * 每页展示20条数据 */ private int mPageSize = 20; private TextView tv_page_numbeer; /** * 一共有多少页面 */ private int totalPage; private EditText et_page_number; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_callsafe); initUI(); initData(); } private Handler handler = new Handler() { @Override public void handleMessage(Message msg) { ll_pb.setVisibility(View.INVISIBLE); tv_page_numbeer.setText(mCurrentPageNumber +"/"+ totalPage ); adapter = new CallSafeAdapter(blackNumberInfos, CallSafeActivity.this); list_view.setAdapter(adapter); } }; private void initData() { new Thread() { @Override public void run() { dao = new BlackNumberDao(CallSafeActivity.this); //通过总的记录数 / 每页多少条数据 totalPage = dao.getTotalNumber() / mPageSize; System.out.println("总的页码=="+totalPage); blackNumberInfos = dao.findBar(mCurrentPageNumber, mPageSize); handler.sendEmptyMessage(0); } }.start(); } /** * 添加黑名单 * * @param view */ public void addBlackNumber(View view) { AlertDialog.Builder builder = new AlertDialog.Builder(this); final AlertDialog dialog = builder.create(); View dialog_view = View.inflate(this, R.layout.dialog_add_blacknumber, null); final EditText et_number = (EditText) dialog_view.findViewById(R.id.et_number); Button btn_ok = (Button) dialog_view.findViewById(R.id.btn_ok); Button btn_cancel = (Button) dialog_view.findViewById(R.id.btn_cancel); final CheckBox cb_phone = (CheckBox) dialog_view.findViewById(R.id.cb_phone); final CheckBox cb_sms = (CheckBox) dialog_view.findViewById(R.id.cb_message); btn_cancel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); btn_ok.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String str_number = et_number.getText().toString().trim(); if(TextUtils.isEmpty(str_number)){ Toast.makeText(CallSafeActivity.this,"请输入黑名单号码",Toast.LENGTH_SHORT).show(); return; } String mode = ""; if(cb_phone.isChecked()&& cb_sms.isChecked()){ mode = "1"; }else if(cb_phone.isChecked()){ mode = "2"; }else if(cb_sms.isChecked()){ mode = "3"; }else{ Toast.makeText(CallSafeActivity.this,"请勾选拦截模式",Toast.LENGTH_SHORT).show(); return; } BlackNumberInfo blackNumberInfo = new BlackNumberInfo(); blackNumberInfo.setNumber(str_number); blackNumberInfo.setMode(mode); blackNumberInfos.add(0,blackNumberInfo); //把电话号码和拦截模式添加到数据库。 dao.add(str_number,mode); if(adapter == null){ adapter = new CallSafeAdapter(blackNumberInfos, CallSafeActivity.this); list_view.setAdapter(adapter); }else{ adapter.notifyDataSetChanged(); } dialog.dismiss(); } }); dialog.setView(dialog_view); dialog.show(); } private void initUI() { ll_pb = (LinearLayout) findViewById(R.id.ll_pb); //展示加载的圆圈 ll_pb.setVisibility(View.VISIBLE); list_view = (ListView) findViewById(R.id.list_view); tv_page_numbeer = (TextView) findViewById(R.id.tv_page_numbeer); et_page_number = (EditText) findViewById(R.id.et_page_number); } private class CallSafeAdapter extends MyBaseAdapter<BlackNumberInfo> { private CallSafeAdapter(List lists, Context mContext) { super(lists, mContext); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder; if (convertView == null) { convertView = View.inflate(CallSafeActivity.this, R.layout.item_callsafe, null); holder = new ViewHolder(); holder.tv_number = (TextView) convertView.findViewById(R.id.tv_number); holder.tv_mode = (TextView) convertView.findViewById(R.id.tv_mode); holder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.tv_number.setText(lists.get(position).getNumber()); String mode = lists.get(position).getMode(); if (mode.equals("1")) { holder.tv_mode.setText("来电拦截+短信"); } else if (mode.equals("2")) { holder.tv_mode.setText("电话拦截"); } else if (mode.equals("3")) { holder.tv_mode.setText("短信拦截"); } final BlackNumberInfo info = lists.get(position); holder.iv_delete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String number = info.getNumber(); boolean result = dao.delete(number); if(result){ Toast.makeText(CallSafeActivity.this,"删除成功",Toast.LENGTH_SHORT).show(); lists.remove(info); //刷新界面 adapter.notifyDataSetChanged(); }else{ Toast.makeText(CallSafeActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); } } }); return convertView; } } static class ViewHolder { TextView tv_number; TextView tv_mode; ImageView iv_delete; } /** * 上一页 * * @param view */ public void prePage(View view) { if (mCurrentPageNumber <= 0) { Toast.makeText(this, "已经是第一页了", Toast.LENGTH_SHORT).show(); return; } mCurrentPageNumber--; initData(); } /** * 下一页 * * @param view */ public void nextPage(View view) { //判断当前的页码不能大于总的页数 if (mCurrentPageNumber >= (totalPage - 1)) { Toast.makeText(this, "已经是最后一页了", Toast.LENGTH_SHORT).show(); return; } mCurrentPageNumber++; initData(); } /** * 跳转 * * @param view */ public void jump(View view) { String str_page_number = et_page_number.getText().toString().trim(); if(TextUtils.isEmpty(str_page_number)){ Toast.makeText(this,"请输入正确的页码",Toast.LENGTH_SHORT).show(); }else{ int number = Integer.parseInt(str_page_number); if(number >=0 && number <= (totalPage - 1) ){ mCurrentPageNumber = number; initData(); }else{ Toast.makeText(this,"请输入正确的页码",Toast.LENGTH_SHORT).show(); } } } }
dialog_add_blacknumber.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center" android:text="添加黑名单" android:textSize="30sp" /> <EditText android:id="@+id/et_black" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="请输入黑名单" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <CheckBox android:id="@+id/cb_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="短信拦截" /> <CheckBox android:id="@+id/cb_phone" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="电话拦截" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <Button android:id="@+id/bt_ok" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="确定" /> <Button android:id="@+id/bt_cancel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
MyBaseAdapter.java
public abstract class MyBaseAdapter<T> extends BaseAdapter { public List<T> lists; public Context mContext; public MyBaseAdapter() { super(); // TODO Auto-generated constructor stub } public MyBaseAdapter(List<T> lists,Context mContext) { this.lists = lists; this.mContext = mContext; } @Override public int getCount() { // TODO Auto-generated method stub return lists.size(); } @Override public Object getItem(int position) { // TODO Auto-generated method stub return lists.get(position); } @Override public long getItemId(int position) { // TODO Auto-generated method stub return position; } }
activity_callsafe.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
style="@style/TitleStyle"
android:text="黑名单管理" />
<Button
android:onClick="addBlackNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="11"> <LinearLayout android:id="@+id/ll_pb" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" android:visibility="invisible" > <ProgressBar android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="玩命加载中..." /> </LinearLayout> <include//这样写list view是为了在一个项目中,为了美观整体性,list view基本上都要一致 android:id="@+id/list_view" layout="@layout/list_view"></include> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <Button android:onClick="prePage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="上一页" android:layout_weight="1"/> <Button android:onClick="nextPage" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="下一页" android:layout_weight="1"/> <Button android:onClick="jump" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="跳转" android:layout_weight="1"/> <EditText android:id="@+id/et_page_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_page_numbeer" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="0/10" android:textSize="18sp" android:layout_weight="1"/> </LinearLayout> </LinearLayout>
list_view.xml
<?xml version="1.0" encoding="utf-8"?> <ListView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > </ListView>
BlackNumberDao.java 对表进行增删改查
//建立黑名单的数据库 public class BlackNumberDao { private BlackNumberOpenHelper helper; public BlackNumberDao(Context context) { helper = new BlackNumberOpenHelper(context); } // 黑名单号码和拦截模式 public boolean add(String number, String mode) { SQLiteDatabase db = helper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("number", number); contentValues.put("mode", mode); long rowid = db.insert("blacknumber", null, contentValues); if (rowid == -1) { return false; } else { return true; } } // 通过电话号码删除 public boolean delete(String number) { SQLiteDatabase db = helper.getWritableDatabase(); // 所影响的行数 int rownumber = db.delete("blacknumber", "number", new String[] { number }); if (rownumber == 0) { return false; } else { return true; } } // 通过电话号码修改拦截的模式 public boolean changeNumberMode(String number, String mode) { SQLiteDatabase db = helper.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put("mode", mode); int rownumber = db.update("blacknumber", contentValues, number, new String[] { number }); if (rownumber == 0) { return false; } else { return true; } } // 返回一个黑名单号码拦截模式 public String findNumber(String number) { String mode = ""; SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.query("blacknumber", new String[] { mode }, "number=?", new String[] { number }, null, null, null); if (cursor.moveToNext()) { mode = cursor.getString(0); } cursor.close(); db.close(); return mode; } // 查找所有的黑名单 public List<BlackNumberInfo> findAll() { SQLiteDatabase db = helper.getReadableDatabase(); List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>(); Cursor cursor = db.query("blacknumber", new String[] { "number", "mode" }, null, null, null, null, null); while (cursor.moveToNext()) { BlackNumberInfo info = new BlackNumberInfo(); info.setNumber(cursor.getString(0)); info.setMode(cursor.getString(1)); blackNumberInfos.add(info); } cursor.close(); db.close(); return blackNumberInfos; } // 进行分页处理,pageNumber当前是哪一页,pageSize每页显示多少数据,offset表示跳过,从第几条开始 public List<BlackNumberInfo> findBar(int pageNumber, int pageSize) { SQLiteDatabase db = helper.getReadableDatabase(); Cursor cursor = db.rawQuery("select * from blacknumber limit ? offset ?", new String[] { String.valueOf(pageSize), String.valueOf(pageSize * pageNumber) }); List<BlackNumberInfo> blackNumberInfos = new ArrayList<BlackNumberInfo>(); while (cursor.moveToNext()) { BlackNumberInfo blackNumberInfo = new BlackNumberInfo(); blackNumberInfo.setMode(cursor.getString(1)); blackNumberInfo.setNumber(cursor.getString(0)); blackNumberInfos.add(blackNumberInfo); } cursor.close(); db.close(); return blackNumberInfos; }
/**
* 获取总的记录数
* @return
*/
public int getTotalNumber(){
SQLiteDatabase db = helper.getReadableDatabase();
Cursor cursor = db.rawQuery("select count(*) from blacknumber", null);
cursor.moveToNext();
int count = cursor.getInt(0);
cursor.close();
db.close();
return count;
}
}
BlackNumberOpenHelper.java //创建了库和表
public class BlackNumberOpenHelper extends SQLiteOpenHelper { public BlackNumberOpenHelper(Context context) { super(context, "safe.db", null, 1); //safe.db,创建的数据库的名字 } /** * 主键,电话号码,拦截模式:电话/短信拦截 */ @Override public void onCreate(SQLiteDatabase db) { // 创建表 db.execSQL("create table blacknumber (_id integer primary key autoincrement,number varchar(20),mode varchar(2))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // 更新表 } }
BlackNumberInfo .java
//JavaBean对象 public class BlackNumberInfo { private String number;//黑名单电话号码 private String mode;//黑名单拦截模式 //电话拦截+短信拦截 public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getMode() { return mode; } public void setMode(String mode) { this.mode = mode; } }
标签:who led while todo auto inf ret ddb static
原文地址:http://www.cnblogs.com/mengxiao/p/6375324.html