Vamsi Challa Vamsi Challa - 6 days ago 5
Android Question

ListView - Custom Adapter - Database

Android 2.3.3

I have a table in database with two columns. I wish to retrieve this data and show them in a ListView having two textviews(first column in first textview and second column in second textview) and repeat this till for all the rows in the database. I have read a few examples on(custom adapter and listview) how to do, but the more I read, the more I get confused.

Can someone give me a head start on how to do this? I can use a dynamic listview, but I wish to do it using the static one.

Here is the Layout of the row in 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="horizontal"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="5dp" >

<TextView
android:id="@+id/txtView_History_Expression"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Expression"
android:textColor="#FFFFFF"
/>

<TextView
android:id="@+id/txtView_History_Result"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="Result"
android:textColor="#316DA2" />

</LinearLayout>


Here is the XML with 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/txtView_History_Header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HISTORY"
android:gravity="center"
android:textSize="24dp"
android:textColor="#316DA2"
android:paddingTop="10dp"
/>

<ListView
android:id="@+id/lvHistory"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

</LinearLayout>


I am retrieving the data from database and storing it in a Cursor. I want to read data from Cursor and give those values to the textviews in the listview.

Answer

I have a table in database with two columns. I wish to retrieve this data and show them in a ListView having two textviews(first column in first textview and second column in second textview) and repeat this till for all the rows in the database. I have read a few examples on(custom adapter and listview) how to do, but the more I read, the more I get confused.

Hello. This algorithm is not too tricky. You need to these steps:

  1. Create some method for getting rows from db. You can return either List<TableDataType> or directly Cursor.
  2. Create subclass of some ListAdapter to get better control over it.
  3. Set ListAdapter to ListView.

Example:

Method for getting data from db:

public List<DataType> getAll() {
   List<DataType> objects = new ArrayList<DataType>();
   DataType child = null;
   Cursor c = null;    
   try {
      String query = "select * from TableName";
      c = db.rawQuery(query, null);
      if (c.moveToFirst()) {
         child = new DataType();
         child.setId(c.getInt(c.getColumnIndex("id")));
         child.setName(c.getString(c.getColumnIndex("name")));
         objects.add(child);  
      }
      return objects;
   }
   finally {
      if (c != null) {
         c.close();
      }
      if (db != null) {
         db.close();
      }
   }
}

Then you can create own Adapter extending from BaseAdapter and set data to TextViews in getView() method and create RowHolder that will be arbitrary Object that holds childs(widgets) of each row

public View getView(int position, View convertView, ViewGroup parent) {
   if (convertView == null) {
      // inflate convertView and add row to RowHolder
      // set RowHolder as tag of row .. convertView.setTag(rowHolder);
   }
   else {
      // just recycle views so get existing RowHolder
      rowHolder = (RowHolder) convertView.getTag();
   }
   // set data to widget(s)
   rowHolder.getFirstTextView().setText(dataSource.get(position).getId());
   ...

}

RowHolder can looks like

public class RowHolder {

   private View row;
   private TextView idColumn;

   public RowHolder(View v) {
      this.row = v;
   }

   public TextView getIdColumn() {
      if (idColumn == null) {
         idColumn = (TextView) row.findViewById(R.id.idColumnId);
      }
      return idColumn;


   }
}

And finally set Adapter to ListView and work is done.

Note:

DataType is own defined Object. It presents Table in your db where properties of Object are identical with columns in Table.

There is other approach and return from db Cursor and a usage of SimpleCursorAdapter or CursorAdapter. Here are nice examples:

Comments