Sunday 22 July 2012

Creating a Custom cursor Adapter and Binding it to a List View in Android

This Post is about creating a Custom Cursor Adapter,which comes handy when fetching the data from the SQLite DB/Content Provider and Binding the same to a View,a ListView to be precise.

I am taking this as my first post as i spent almost half-a-day to get it right.

The following example demonstrates on how to Fetch the contacts and display it in your own way using a List View.Each row in the custom list has an Image,The name fetched from contacts DB and chat availabilty of the contact(hardcoded offcourse.. :P).You can consider this a stub code and make improvisations on it.



/**
 * Activity that displays Custom List View
 * @author rajeev ranganathan
 *
 */
public class MainActivity extends ListActivity implements
LoaderManager.LoaderCallbacks<Cursor>{

private listBuilder mListBuilder;
private LayoutInflater mInflator;
private int LOADER_ID=1000;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
               //Using Cursor Loader for better Performance.
              //Refer Cursor Loader to get more understanding.

getLoaderManager().initLoader(LOADER_ID, null,this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
//Create the Cursor that will take care of the data being displayed
Log.d("TAG","onCreateLoader...");
return new CursorLoader(getApplicationContext(),
ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
//Now Bind the data to the View
Log.d("TAG","onLoadFinished...ARG1= "+cursor);
mListBuilder=new listBuilder(getApplicationContext(), cursor);
setListAdapter(mListBuilder);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
//NO OP

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

class listBuilder extends CursorAdapter{
public listBuilder(Context context, Cursor c) {
super(context, c);
Log.d("TAG","CursorAdapter Constr..");
mInflator=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public void bindView(View arg0, Context arg1, Cursor cursor) {
String name = null;
Log.d("TAG","CursorAdapter BindView");
if(null!=cursor){
Log.d("TAG","CursorAdapter BindView:Cursor not null");
name=cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY   _NAME));
}
//Set the Menu Image
ImageView menuImage=(ImageView)arg0.findViewById(R.id.iv_ContactImg);
menuImage.setImageResource(R.drawable.ic_launcher);

//Set the Name
TextView heading=(TextView)arg0.findViewById(R.id.tv_ContactName);
heading.setText(name);
//Set Availability
TextView randomText=(TextView)arg0.findViewById(R.id.tv_ContactNumber);
randomText.setText("Not Available");

}

@Override
public View newView(Context arg0, Cursor arg1, ViewGroup arg2) {
Log.d("TAG","CursorAdapter newView");
final View customListView=mInflator.inflate(R.layout.activity_listrow,null);
return customListView;
}

}
}
The XML design for the layout is as below,


<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/iv_ContactImg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />


    <TextView
        android:id="@+id/tv_ContactName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/iv_ContactImg"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000" />


    <TextView
        android:id="@+id/tv_ContactNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv_ContactName"
        android:layout_below="@+id/tv_ContactName"
        android:layout_marginLeft="18dp"
        android:text="TextView"
        android:textColor="#000000" />

</RelativeLayout>


The final result would look something like this,

Happy Coding,
-Rajeev

No comments:

Post a Comment

Note: only a member of this blog may post a comment.