Android ListView CursorAdapter

In this Android tutorial, I am going to provide you an example on using CursorAdapter with ListView . The CursorAdapter is a class derived from the BaseAdapter. You can use the CursorAdapter to expose data from a Cursor object to a ListView. Typically, to use the Cursor Adapter, we have to create its derived class. In the derived that extends the CursorAdapter, you can inflate layout file of the ListView and take data from the Cursor object into the ListView. When you construct an object of the derived class, you need to pass the current activity and the Cursor object to its constructor. In the example application below, in the onCreate() method of MainActivity class, null is used for the Cursor object because there is no Cursor object that has data to show. However, after the query() method of the ContentResolver is called to retrieve data from the rowser.BOOKMARKS_URI table. The Cursor object that stores the data about bookmarks and history of the browser is passed to the BrowserAdapter (the derived class of CursorAdapter) using the changeCursor() method.
Now to have example application on using CursorAdapter, you create a new Android project. After that, in the activity_main.xml file, you need a ListView as shown below.


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="com.example.andexample.MainActivity"

>
<ListView
   android:id="@+id/brlist"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   >
  
</ListView>
</LinearLayout>



In the ListView, we are going to show the title and url of the bookmarks and history. So, you need another layout file called list_item.xml that is used as a row template of the ListView.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
  
   <TextView
   android:id="@+id/txttitle"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10sp"
   android:textStyle="bold" >
</TextView>
<TextView
   android:id="@+id/txturl"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:padding="10sp"
       >
</TextView>

</LinearLayout>


Then, you create a class called BrowserAdapter that extends the CursorAdapter class. In this class, you inflate the list_item.xml file in the newView() method and expose data from the Cursor object to the ListView in the bindView() method.

package com.example.andexample;

import android.content.Context;
import android.database.Cursor;
import android.text.util.Linkify;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class BrowserAdapter extends CursorAdapter {
private final LayoutInflater mInflater;
public BrowserAdapter(Context context, Cursor cursor) {
     super(context, cursor, false);
     // Get inflater
     mInflater = LayoutInflater.from(context);
   }

   @Override
   public View newView(Context context, Cursor cursor, ViewGroup parent) {
   // Use the inflater to inflate the listview layout and return it
      return mInflater.inflate(R.layout.list_item, parent, false);
   }
   @Override
   public void bindView(View view, Context context, Cursor cursor) {
   // Set title TextView
   String title=cursor.getString(cursor.getColumnIndex(MainActivity.projection[1]));
     TextView txttitle=(TextView)view.findViewById(R.id.txttitle);
     txttitle.setText(title);
     // Set url to TextView
     String url=cursor.getString(cursor.getColumnIndex(MainActivity.projection[2]));
     TextView txturl=(TextView)view.findViewById(R.id.txturl);
     txturl.setText(url);
     // Make url linkable
     Linkify.addLinks(txturl, Linkify.ALL);
     
   }
}


Here is the content of the MainActivity class. The code is written to get data from the Browser.BOOKMARKS_URI table using the query() method of ContentResolver, and pass the Cursor object returned from the query() method to the BrowserAdapter object.

package com.example.andexample;
import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.Browser;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;

public class MainActivity extends ActionBarActivity{

Activity context;
BrowserAdapter adapter;
public static String[] projection={Browser.BookmarkColumns._ID,Browser.BookmarkColumns.TITLE,Browser.BookmarkColumns.URL};


protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
ListView listview=(ListView)findViewById(R.id.brlist);

// Set the adapter for the view. Now there is no Cursor object that contains data to show
// so the Cursor should be null.
adapter=new BrowserAdapter(this,null);
listview.setAdapter(adapter);

}
public void onStart(){
  super.onStart();
  // Get ContentResolver
  ContentResolver cr=getContentResolver();
  // Specify field to sort the records
  String order=Browser.BookmarkColumns.DATE+" DESC";
  // Read all rows from table of bookmarks and history
  Cursor cursor=cr.query(Browser.BOOKMARKS_URI,projection, null,null,order);
  // Update cursor in the adapter
  adapter.changeCursor(cursor);
}

}


Android-ListView-CusorAdapter

Posted by: Dara | post date: 10-12-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.