Reputation: 154
I have been trying to get my Cursor to display correctly in listview. it displays correctly when using a toast so the cursor is retrieving the data correctly but i'm having trouble making it display in list format.
Should be 6 columns in each row.
It only displays one row with no data. I'm sure it is a very basic problem. Maybe somebody could tell me where I'm going wrong, I would be grateful.
DisplayCursor.Java
public class DisplayCursor extends ListActivity
{
MyDBManager db = new MyDBManager(this);
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_cursor);
db.open();
Cursor cursor = db.getAllRows();
startManagingCursor(cursor);
String[] columns = new String[] { MyDBManager.KEY_DESCRIPTION, MyDBManager.KEY_PERCENTAGE, MyDBManager.KEY_PRICE,
MyDBManager.KEY_VOLUME, MyDBManager.KEY_VFM, MyDBManager.KEY_QUANTITY };
int[] to = new int[] { R.id.description,R.id.perc,R.id.price,R.id.units,R.id.vol, R.id.vfm };
SimpleCursorAdapter mAdapter = new SimpleCursorAdapter(this, R.layout.row, cursor, columns, to);
this.setListAdapter(mAdapter);
db.close();
}
}
row.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/description"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="27dp" />
<TextView
android:id="@+id/vol"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/description"
android:layout_alignBottom="@+id/description"
android:layout_toRightOf="@+id/description" />
<TextView
android:id="@+id/perc"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/vol"
android:layout_alignBottom="@+id/vol"
android:layout_toRightOf="@+id/vol" />
<TextView
android:id="@+id/price"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/perc"
android:layout_alignBottom="@+id/perc"
android:layout_toRightOf="@+id/perc" />
<TextView
android:id="@+id/units"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/price"
android:layout_alignBottom="@+id/price"
android:layout_toRightOf="@+id/price" />
<TextView
android:id="@+id/vfm"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/units"
android:layout_alignBottom="@+id/units"
android:layout_toRightOf="@+id/units" />
</RelativeLayout>
activity_display_cursor.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView android:id="@android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/nodata"/>
</LinearLayout>
Upvotes: 4
Views: 2834
Reputation: 8641
Ah, ListView with CursorAdapter strikes again! It's not easy to grok the first time round, so:
You have most of it correct, however, your row.xml needs to contain a View for each column in the cursor you want to display. The "layout" argument in the CursorAdapter constructor specifies the resID of a layout file; this file describes one row. In your case, you probably want a RelativeLayout that has 6 TextViews in it, one for each of the R.id values in the "to" argument to the constructor. The CursorAdapter takes care of mapping the cursor columns to the views in the "to" argument, which is why the size of those arrays has to be the same.
The activity_display_cursor.xml layout defines the ListView.
As a note, I don't use ListActivity much. It's easy enough to set up a ListView with an adapter in a regular Activity. Also, startManagingCursor was deprecated in API 11. A better choice is to use android.support.v4.content.CursorLoader and android.support.v4.widget.SimpleCursorAdapter.
Upvotes: 4