修改界面,在顶部横条上增加一个添加按钮,点击打开一个自定义对话框,输入电话号码和拦截模式保存到数据库
自定义对话框看这篇http://www.cnblogs.com/taoshihan/p/5370378.html
添加记录
调用Builder对象的show()方法,获取AlertDialog对象
调用View.inflate()方法,将布局文件转成View对象
调用View对象的findViewById()方法,获取确认和取消的Button对象
调用Button对象的setOnClickListener()方法,设置点击事件,匿名内部类实现OnClickListener接口,重写onClick()方法
调用AlertDialog对象的dismiss()方法,可以关闭对话框
调用View对象的findViewById()方法,获取到各个控件的值,进行判断处理
调用之前定义好的Dao类BlackNumberAdo对象的add()方法,数据库增加一条记录,参数:String电话号码,String拦截模式
此时ListView并不会显出出来刚添加的记录,需要退出这个Activity重写进入,我们通知适配器数据更新
调用集合List对象的add()方法,添加一条数据,参数:0(第一个),数据
调用ListAdapter对象的notifyDataSetChanged()方法,通知数据更新
删除记录
条目布局文件中,在右侧放置一个垃圾桶的小图标,上下居中,父控件右边
安卓系统的点击事件和js的点击事件很相似,
参考这篇:http://www.cnblogs.com/taoshihan/p/5438729.html
获取到删除按钮Button对象
调用Button对象,设置点击事件
获取AlertDialog.Builder对象,展示是否确认删除的对话框
调用AlertDialog.Builder对象,设置确认按钮和取消按钮,注意设置点击事件OnClickListener时,它所在的包是DialogInterface.OnClickListener
调用之前定义好的Dao类BlackNumberAdo对象的delete()方法,参数:String电话号码
调用集合List对象的remove()方法,删除一条数据,参数:int索引
调用ListAdapter对象的notifyDataSetChanged()方法,通知数据更新
CallSmsSafeActivity.java
package com.qingguow.mobilesafe; import java.util.HashMap; import java.util.List; import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.ImageView; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import com.qingguow.mobilesafe.db.ado.BlackNumberAdo; /** * 通讯卫士 * * @author taoshihan * */ public class CallSmsSafeActivity extends Activity { private ListView listview; private List<Map<String, String>> infos; private BlackNumberAdo dao; private MyAdapter myAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_call_sms_safe); listview = (ListView) findViewById(R.id.lv_blacknumber); myAdapter=new MyAdapter(); dao = new BlackNumberAdo(this); infos = dao.findAll(); listview.setAdapter(myAdapter); // //添加100条测试数据 // Random random=new Random(); // for(int i=1;i<=100;i++){ // ado.add("18805419000"+i, String.valueOf(random.nextInt(3)+1)); // } } class ViewHolder{ public TextView phoneView; public TextView modeView; public ImageView iv_delete; } private class MyAdapter extends BaseAdapter { @Override public int getCount() { // TODO Auto-generated method stub return infos.size(); } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view; ViewHolder holder=new ViewHolder(); if(convertView==null){ view = View.inflate(CallSmsSafeActivity.this, R.layout.list_call_sms_safe_item, null); holder.phoneView=(TextView) view .findViewById(R.id.tv_main_phone); holder.modeView=(TextView) view .findViewById(R.id.tv_block_mode); holder.iv_delete=(ImageView) view.findViewById(R.id.iv_delete); view.setTag(holder); System.out.println("创建新的View对象"+position); }else{ view=convertView; holder=(ViewHolder) view.getTag(); System.out.println("使用历史View对象"+position); } holder.phoneView.setText(infos.get(position).get("phone")); switch (infos.get(position).get("mode")) { case "1": holder.modeView.setText("电话拦截"); break; case "2": holder.modeView.setText("短信拦截"); break; case "3": holder.modeView.setText("全部拦截"); break; default: break; } //删除条目 holder.iv_delete.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(CallSmsSafeActivity.this); builder.setTitle("警告"); builder.setMessage("是否确认删除?"); builder.setPositiveButton("确认", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { infos.remove(position); myAdapter.notifyDataSetChanged(); dao.delete(infos.get(position).get("phone")); } }); builder.setNegativeButton("取消", null); builder.show(); } }); return view; } @Override public Object getItem(int position) { // TODO Auto-generated method stub return null; } @Override public long getItemId(int position) { // TODO Auto-generated method stub return 0; } } private EditText blackNumber; private CheckBox blockCall; private CheckBox blockSms; /** * 添加黑名单 * @param v */ public void addCallSmsSafe(View v) { AlertDialog.Builder builder=new AlertDialog.Builder(this); View view=View.inflate(this, R.layout.dialog_add_safe_sms, null); builder.setView(view); final AlertDialog alertDialog=builder.show(); Button submit=(Button) view.findViewById(R.id.bt_submit); Button cancel=(Button) view.findViewById(R.id.bt_cancel); blackNumber=(EditText) view.findViewById(R.id.et_black_number); blockCall=(CheckBox) view.findViewById(R.id.cb_block_call); blockSms=(CheckBox) view.findViewById(R.id.cb_block_sms); submit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { String phone=blackNumber.getText().toString().trim(); String mode = "3"; if(TextUtils.isEmpty(phone)){ Toast.makeText(CallSmsSafeActivity.this, "请输入手机号码", 0).show(); } if(blockCall.isChecked()&&blockSms.isChecked()){ //全部拦截 mode="3"; }else if(blockCall.isChecked()){ //电话拦截 mode="1"; }else if(blockSms.isChecked()){ //短信拦截 mode="2"; }else{ Toast.makeText(CallSmsSafeActivity.this, "请勾选拦截模式", 0).show(); } dao.add(phone, mode); Map<String, String> info=new HashMap<String, String>(); info.put("phone", phone); info.put("mode", mode); infos.add(0,info); myAdapter.notifyDataSetChanged(); alertDialog.dismiss(); } }); cancel.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { alertDialog.dismiss(); } }); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/5395.html