Android - display cursor results from database in ListView

I am trying to correctly display the cursor in a list. it displays correctly when using a toast, so the cursor correctly extracts the data, but I am having problems displaying it in list format.

Each row should have 6 columns.

It displays only one row without data. I am sure this is a very simple problem. Maybe someone can tell me where I'm wrong, I would appreciate it.

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> 
+4
source share
1 answer

Ah, a ListView with a CursorAdapter will hit again! It’s not easy to reschedule the first time, so:

You have most of the fixes, however your row.xml should contain a view for each column in the cursor that you want to display. The "layout" argument in the CursorAdapter constructor re-indicates the ID of the layout file; this file describes a single line. In your case, you probably want a RelativeLayout, which has 6 TextViews, one for each of the R.id values ​​in the "to" argument to the constructor. The CursorAdapter takes care of matching the cursor columns with the views in the "to" argument, so the size of these arrays must be the same.

The activity_display_cursor.xml mask defines a ListView.

As a note, I do not use ListActivity. It’s easy enough to configure a ListView with an adapter in a regular Activity. In addition, startManagingCursor is deprecated in API 11. It is best to use android.support.v4.content.CursorLoader and android.support.v4.widget.SimpleCursorAdapter.

+4
source

Source: https://habr.com/ru/post/1445848/


All Articles