Implementing ContentProvider

0
1016

Hello Friends !!! In this tutorial we are going to retrieve the contacts from the android mobile using ContentProvider.

  • Here is an example of ContentProvider ,so to implement it just follow the steps given below .
  • STEPS
  1. Create a new project for example name it “ContactsDemo”.
  2. Now in your activity_main.xml file in the graphical layout take three buttons and name it as CONNECT , PREV , NEXT .
  3. Also take a Large text where we would display the text or the contact.
  4. Hence the layout will look like as shown below:
  5. 1

  6. Now on the buttons (i.e button property)in the activity_main.xml file set the android:onClick=”prevClicked” for previous button,do the same for the Next and the Connect button.
  7. Now in the MainActivity.java file extend the class with Activity.
  8. Declare the ArrayList , index and TextView as private.
  9. Therefore the code for declaring it private is given :
    1. private ArrayList names=new ArrayList();
    2. private static int index = 0;
    3. Private static TextView tvName = null;

  10. Now in onCreate ( ) method add the following line of code which will find the id of the textView.
    1. tvName = (TextView)find ViewById(R.id.name);
    2. name is the id of the TextView which is generated not to be typed manually.

  11. Now write the code for the connect button.
  12. [code language=”java”]public void connectClicked(View v)
    {
    Uri allContacts = ContactsContract.Contacts.CONTENT_URI;
    Cursor cur = this.getContentResolver().query(
    allContacts, //the uri
    null, //rows to get back,null for all
    null, //where clause
    null, //where clause arguments
    null //order by clause
    );
    cur.moveToFirst();

    /* for each column in the cursor , fetch the name of the contact into the ArrayList names :*/
    do
    {
    int nameCol = cur.getColumnIndex(PhoneLookup.DISPLAY_NAME);
    String contact = cur.getString(nameCol);
    names.add(contact);
    }while(cur.moveToNext());

    //display the first name

    tvName.setText(names.get(index));

    //always close the cursor when done

    cur.close();
    }[/code]

  13. The CONTENT_URI gives the authority to access or to map the contact .
  14. So now your window will look as shown below :
  15. CD1

  16. Similarly write code for next and the previous buttons as given below.
  17. [code language=”java”]public void nextClicked(View v)
    {
    //if we are not at the end of the list :
    if(index<names.size()-1)
    {
    //display the next name :
    tvName.setText(names.get(++index));
    }
    }

    public void prevClicked(View v)
    {
    //If we are not at the beginning of the list :
    if(index > 0)
    {
    //display the previous name :
    tvName.setText(names.get(–index));
    }
    }

    [/code]

  18. The most important thing is the android manifest :
  19. If you are going to look at contacts or any other information or any other system provider you need to provide a permission.
  20. In this case we are providing a permission as shown below in AndroidManifest.xml file:
  21. [code language=”xml”]
    <uses-permission android:name=” android.permission.READ_CONTACTS”/>
    [/code]

  22. Now Run your MainActivity .java file and you will see the following screen :
  23. CD2

  24. Now click on the connect button it will display the first name from the contact database :
  25. CD3

  26. Thus we have successfully implemented ContentProvider.