Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> [android] 手機衛士黑名單功能(ListView結合SQLite增刪改),androidsqlite

[android] 手機衛士黑名單功能(ListView結合SQLite增刪改),androidsqlite

編輯:關於android開發

[android] 手機衛士黑名單功能(ListView結合SQLite增刪改),androidsqlite


修改界面,在頂部橫條上增加一個添加按鈕,點擊打開一個自定義對話框,輸入電話號碼和攔截模式保存到數據庫

自定義對話框看這篇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();
            }
        });
    }
}

 

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