Implementing ContentProvider

0
1241

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. 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.
  6. Now in the MainActivity.java file extend the class with Activity.
  7. Declare the ArrayList , index and TextView as private.
  8. 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;

  9. 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.

  10. Now write the code for the connect button.
  11. 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();
    }

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

  14. Similarly write code for next and the previous buttons as given below.
  15. 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));
    	}
    }
    
    

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

  20. Now Run your MainActivity .java file and you will see the following screen :

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

  22. Thus we have successfully implemented ContentProvider.