Thursday, 4 August 2016

Content Provider Example in android studio | How to read contact from Mobile device

Content Provider:-
  • A content provider supply data from one application to another application on request.
  • all these request handle by the "ContentResolver" class.
  • it can use different ways to store its data and the data can be stored in a database i,e: file,over network,SQLite database.
Requirements:-
1. MainActivity.java | activity_main.xml
2. adapter_data_provider.xml
Example:-
Step 1:-activity_main.xml
package supriya.com.contentprovider;
import android.content.ContentResolver;
import android.database.Cursor;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class MainActivity extends AppCompatActivity {
    ListView listViewData;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listViewData=(ListView)findViewById(R.id.list1);
        ContentResolver contentResolver=getContentResolver();
        Cursor c=contentResolver.query(ContactsContract.CommonDataKinds.Phone.
CONTENT_URI,null,null,null,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
        String [] fromToCopy=new String[]{ContactsContract.CommonDataKinds.Phone.
DISPLAY_NAME,ContactsContract.CommonDataKinds.Phone.NUMBER};
        int[] to=new int[]{R.id.txtName,R.id.txtNumber};
        SimpleCursorAdapter scp=new SimpleCursorAdapter(getApplicationContext(),R.layout.adapter_data_provider,c,fromToCopy,to);
        listViewData.setAdapter(scp);

    }
}
2. main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#000000"
        android:orientation="vertical"
        android:gravity="center">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Contacts"
            android:textColor="#ffffff"
            android:gravity="center"
            android:textStyle="bold"
            android:textSize="20sp"/>
    </LinearLayout>

    <ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/list1"
        android:layout_marginLeft="10dp"></ListView>
</LinearLayout>
3.adapter_data_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Name"
        android:textSize="25sp"
        android:textColor="#000000"
        android:id="@+id/txtName"/>
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="+91-9191919191"
        android:textSize="20sp"
        android:textColor="#424242"
        android:id="@+id/txtNumber"/>

</LinearLayout>

No comments:

Post a Comment