Android Project Call Butler (3)-----Add and remove blacklist

Source: Internet
Author: User

Implement specific functions now-----add blacklist

First look at the picture:

You can also see that the whole logic is:

1. Click the Add button at the bottom

2. Go to the Contact Selection screen to select a contact (this is called the system's contact interface, you can only select one contact at a time, of course, to achieve the choice of multiple contacts each time can also, can be customized to select the contact interface)

3. Return the selected contacts and insert them into the database.

4. Traverse the blacklist table in the database to display all blacklists in the ListView

5. Delete a contact

6. Refresh the blacklist display list

Main code (BLACKLISTACTIVITY.JAVA):

Private Button Btn_add;
Btn_add = (Button) Findviewbyid (r.id.btn_add);         Btn_add.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {Intent Intent = new Intent ( Intent.action_pick, ContactsContract.Contacts.CONTENT_URI); Startactivityforresult (Intent, 1);});

          Here's Intent.action_pick, ContactsContract.Contacts.CONTENT_URI is to jump to the system of the Select contact interface, because to return the selected contact information, so to Startactivityforresult, where 1, Refers to the Requestcode is the request code, naturally also to implement the Onactivityresult method, as follows:  

     protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivityresult (reques        Tcode, ResultCode, data);        if (ResultCode = = RESULT_OK) {if (Requestcode = = 1) {System.out.println ("-------------------");        Uri uri = Data.getdata ();        CREATE_DB ();        cursor cursor = managedquery (URI, NULL, NULL, NULL, NULL);        Cursor.movetofirst ();        String result[] = this.getcontactphone (cursor);        insert_db (Result[0], result[1]);//Here is the database, of course, you can use list to save the contact's name and telephone showblacklist ();                Db.close (); }}} private string[] Getcontactphone (cursor cursor) {int phonecolum = Cursor.getcolumnindex (Contact    SContract.Contacts.HAS_PHONE_NUMBER);    int phonenum = Cursor.getint (Phonecolum);    String result[]= New string[2];    if (Phonenum > 0) {//Get contact ID number int idcolum = Cursor.getcolumnindex (contactscontract.contacts._id); String contactId = cursor.getstring (idcolUM); Remove Contact phone Cursor phone = getcontentresolver (). query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, NULL, Cont        actscontract.commondatakinds.phone.contact_id+ "=" +contactid, null, NULL); if (Phone.movetofirst ()) {for (;! Phone.isafterlast ();p hone.movetonext ()) {int index = Phone.getcolumnindex (    ContactsContract.CommonDataKinds.Phone.NUMBER);    int typeindex = Phone.getcolumnindex (ContactsContract.CommonDataKinds.Phone.TYPE);        int name = Phone.getcolumnindex (phonelookup.display_name);    String PhoneNumber = phone.getstring (index);    String phonename = phone.getstring (name);    Result[0] = Phonename;     RESULT[1] = PhoneNumber;    }}} return result; }

The Getcontactphone (cursor cursor) method allows you to return the name and number of the selected contact, where the returned contact information is saved in the database, and the table in the database is traversed to display it in the interface.

Since the use of the database, of course, the creation and shutdown of the database, as well as database additions and deletions, the following database-related code

Private list<person> array = new arraylist<person> ();p rivate sqlitedatabase db;


   Create or Open database public    void create_db () {    //Create or Open database    db = Sqlitedatabase.openorcreatedatabase ( BlackListActivity.this.getFilesDir (). toString () + "/LIST.DB3", null);    Db.execsql ("DROP TABLE IF EXISTS blacklist");      if (db = = null) {    toast.maketext (blacklistactivity.this, "Database creation unsuccessful", Toast.length_long). Show ();    else{    //toast.maketext (mainactivity.this, "Database creation succeeded", Toast.length_long). Show ();    /*//CREATE TABLE    db.execsql ("CREATE table if not exists blacklist (_id Integer primary key autoincrement," +    "name varchar (+), "+    " number varchar (15)); */    }    }   


   Insert public     void insert_db (String name,string number) {Number    = Number.replaceall ("-", "");    cursor cursor = db.rawquery ("Select * from blacklist where number =" +number,null);    if (cursor.getcount () = = 0) {    db.execsql ("Insert INTO blacklist (name,number) VALUES ('" + name+ "', '" + number + "');");    }    else{    Toast.maketext (Blacklistactivity.this, "the number is already in the Blacklist", Toast.length_short). Show ()        ;    Cursor.close ();    } <BR abp= "823"/><br abp= "824"/>

   Delete public     void delete_db (int. item_id) {    db.execsql ("Delete from blacklist where _id= '" + item_id+ "'");    }


    Show blacklist public    void Showblacklist () {    cursor cursor = db.rawquery ("Select * from blacklist", null);    Array.clear ();    if (Cursor.getcount () > 0) {    cursor.movetofirst ();        for (int i = 0;i < Cursor.getcount (); i++) {    int idindex = Cursor.getcolumnindex ("_id");    int nameindex = Cursor.getcolumnindex ("name");    int numberindex = Cursor.getcolumnindex ("number");        int id = cursor.getint (idindex);    String name = cursor.getstring (nameindex);    String number = cursor.getstring (numberindex);    person person = new person (id,name,number);    Array.add (person);    Cursor.movetonext ();    }        }    Cursor.close ();    adapter = new Adapter (blacklistactivity.this,tv_count); Lv_show.setadapter (adapter);        }

Add the contact and display the function, has been implemented, then the rest is to delete the contact, when long press the item in the ListView, will pop up the multi-select control and the bottom of the Delete and Return button control, about how to eject these controls, in the previous section specifically written, This is just the implementation of deleting contacts based on the previous section.

Private list<person> Selectid = new arraylist<person> ();p rivate Boolean ismulchoice = false; Private Adapter adapter;private ListView lv_show;private relativelayout add_layout;private relativelayout delete_ Layout;private button Btn_delete;private button btn_cancel;

        Btn_delete = (Button) Findviewbyid (r.id.btn_delete);        Btn_delete.setonclicklistener (New Clickevent ());        Btn_cancel = (Button) Findviewbyid (r.id.btn_cancel);        Btn_cancel.setonclicklistener (New Clickevent ());
      Class Clickevent implements onclicklistener{@Overridepublic void OnClick (View v) {switch (V.getid ()) {C            ASE R.id.btn_cancel:ismulchoice = false;            Selectid.clear ();            adapter = new Adapter (blacklistactivity.this,tv_count);            Lv_show.setadapter (adapter);            Delete_layout.setvisibility (View.gone);            Add_layout.setvisibility (view.visible);        Break            Case R.id.btn_delete:ismulchoice =false;            CREATE_DB (); for (int i=0;i<selectid.size (), i++) {for (int j=0;j<array.size (); j + +) {if (SELECTID.G                        ET (i). Equals (Array.get (j))) {delete_db (Array.get (j). ID);                    Array.remove (j);                        }}} selectid.clear ();            Showblacklist ();                        Db.close (); adapter = new Adapter (blacklistactivity.this,tv_coUNT);            Lv_show.setadapter (adapter);            Delete_layout.setvisibility (View.gone);            Add_layout.setvisibility (view.visible);        Break        Default:break; }        }        }

The idea of deletion is to delete the selected contact from the database when you click the Delete button, and call Showblacklist to refresh the display blacklist list.

Finally, the use of the choice of contacts, then have to read the contact information permissions, in Androidmanifest.xml add:

<uses-permission android:name= "Android.permission.READ_CONTACTS"/> <br abp= "855"/>




Android Project Call Butler (3)-----Add and remove blacklist

Related Article

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.