[Android] Phone defender blacklist function (ListView combined with sqlite additions and deletions)

Source: Internet
Author: User
Tags phoneview

Modify the interface, add a button to the top bar, click to open a custom dialog, enter phone number and interception mode to save to the database

Customize dialog box See this article http://www.cnblogs.com/taoshihan/p/5370378.html

Add a record

Call the show () method of the Builder object to get the Alertdialog object

Call the View.inflate () method to turn the layout file into a View object

Call the Findviewbyid () method of the View object to get the confirmation and cancellation of the button object

Call the button object's Setonclicklistener () method, set the Click event, the anonymous inner class implements the Onclicklistener interface, and override the OnClick () method

Call the Dismiss () method of the Alertdialog object to close the dialog box

Call the Findviewbyid () method of the View object to obtain the values of the individual controls for processing

Call the Add () method of the previously defined DAO class Blacknumberado object, add a record to the database, parameters: String phone number, string intercept mode

At this point the ListView does not appear to have just added the record, need to exit this activity rewrite into, we notify the adapter data update

Call the Add () method of the Collection list object, adding a piece of data, parameters: 0 (first), data

Call the Notifydatasetchanged () method of the ListAdapter object to notify the data update

Deleting records

In the entry layout file, place a small icon on the right side of the trash bin, centered up and down, to the right of the parent control

The Android Click event is similar to the JS click event,

Refer to this article: http://www.cnblogs.com/taoshihan/p/5438729.html

Get to delete button object

Invoke the button object, set the Click event

Gets the Alertdialog.builder object that shows whether to confirm the deleted dialog box

Call the Alertdialog.builder object, set the confirmation button and the Cancel button, and note that when you set the Click event Onclicklistener, the package it is in is Dialoginterface.onclicklistener

Call the Delete () method of the previously defined DAO class Blacknumberado object, Parameter: string phone number

Call the Remove () method of the Collection list object to delete a data parameter: Int index

Call the Notifydatasetchanged () method of the ListAdapter object to notify the data update

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;/** * Communication Defender * * @author Taoshihan * */public cl    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);        Add 100 test Data//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 VI            ew            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 ("Create a new View object" +position);                }else{View=convertview;                Holder= (Viewholder) View.gettag ();            System.out.println ("Use Historical View Object" +position);            } holder.phoneView.setText (Infos.get (position). Get ("phone"));                Switch (infos.get (position). Get ("mode")) {case "1": Holder.modeView.setText ("phone intercept");            Break Case "2": Holder.modeView.setText ("SMSInterception ");            Break                Case "3": Holder.modeView.setText ("intercept All");            Break            Default:break;                                }//Delete entry Holder.iv_delete.setOnClickListener (new Onclicklistener () { @Override public void OnClick (View v) {alertdialog.builder builder=new Alertdialog.buil                    Der (Callsmssafeactivity.this);                    Builder.settitle ("warning"); Builder.setmessage ("Do you want to confirm the deletion?"                    "); Builder.setpositivebutton ("Confirm", new Dialoginterface.onclicklistener () {@Ov Erride public void OnClick (Dialoginterface dialog, int which) {Infos.re                            Move (position);                            Myadapter.notifydatasetchanged ();                        Dao.delete (Infos.get (position). Get ("phone")); }                    });                    Builder.setnegativebutton ("Cancel", null);                Builder.show ();            }            });        return view; } @Override public Object getItem (int position) {//TODO auto-generated method stub R        Eturn null; } @Override public long getitemid (int position) {//TODO auto-generated method stub R        Eturn 0;    }} private EditText blacknumber;    Private CheckBox Blockcall;    Private CheckBox blocksms; /** * Add blacklist * @param v */public void Addcallsmssafe (View v) {alertdialog.builder builder=new Alert        Dialog.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, "Please enter mobile number", 0). Show ();                    } if (blockcall.ischecked () &&blocksms.ischecked ()) {//Intercept all                Mode= "3";                }else if (blockcall.ischecked ()) {//Phone intercept mode= "1";                }else if (blocksms.ischecked ()) {//SMS intercept mode= "2";                }else{Toast.maketext (callsmssafeactivity.this, "Please tick block mode", 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 ();    }        }); }}

[Android] Phone defender blacklist function (ListView combined with sqlite additions and deletions)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.