vinidog
vinidog

Reputation: 372

SimpleCursorAdapter is not populating ListView

The SimpleCursorAdapter is not populating my ListView, no error, just nothing happens.

Could someone help me find the error?

1) Layout with ListView component (tab_frag1_layout.xml):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FF0000"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/lvVehicle"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />

</LinearLayout>

2) Layout to represent row (vehicle_row.xml):

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dip" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1dip" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_arrow_right"
            android:contentDescription="@null"
            android:adjustViewBounds="true"
            android:layout_marginRight="3dip"
            android:layout_gravity="center_vertical" />

        <TextView
            android:id="@+id/plate"
            style="@style/vehicleDefaultFont.plate"
            android:text="@string/label_text_view" />

        <TextView
            android:id="@+id/hyphen"
            android:text="@string/label_hyphen" />

        <TextView
            android:id="@+id/model"
            android:text="@string/label_text_view" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="1dip" >

        <TextView
            android:id="@+id/driver"
            style="@style/vehicleDefaultFont.driver"
            android:layout_span="4"
            android:text="@string/label_text_view" />
    </TableRow>

</TableLayout>

3) Fragment Class:

public class Tab1Fragment extends Fragment {

    String TAG = getClass().getName();
    private VehicleDbAdapter dbHelper;
    private SimpleCursorAdapter dataAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.tab_frag1_layout, container, false);

        dbHelper = new VehicleDbAdapter(getActivity());
        dbHelper.open();

        dbHelper.deleteAllVehicles();

        dbHelper.insertVehicles();

        Cursor cursor = dbHelper.fetchAllVehicles();

        if(cursor != null && cursor.getCount() > 0) {

            String[] columns = new String[] {
                    VehicleDbAdapter.KEY_MODEL,
                    VehicleDbAdapter.KEY_PLATE,
                    VehicleDbAdapter.KEY_DRIVER
            };

            int[] to = new int[] { 
                    R.id.model,
                    R.id.plate,
                    R.id.driver,
            };

            dataAdapter = new SimpleCursorAdapter(
                    view.getContext(),
                    R.layout.vehicle_row, 
                    cursor, 
                    columns, 
                    to,
                    0); 

            ListView listView = (ListView) view.findViewById(R.id.lvVehicle);
            listView.setAdapter(dataAdapter);

            Log.i(TAG, "Cursor = " + cursor.getCount());



        } else {

            Log.i(TAG, "Cursor = " + cursor.getCount());
        }

        return view;
    }   
}

Curiously:

My cursor contains an _id field.

I doublechecked my cursor and it has 7 rows.

07-21 01:30:22.215: I/ Cursor = 7

I tried using the layout to the generic android list layout, but nothing doing :(

dataAdapter = new SimpleCursorAdapter(
                    view.getContext(),
                    android.R.layout.simple_list_item_1, 
                    cursor, 
                    columns, 
                    to,
                    0); 

Any help is welcome.

Upvotes: 0

Views: 1124

Answers (2)

user7482740
user7482740

Reputation:

I think you have problem at XMl. Check id of your listview.

android:id="@+android:id/mainListView">

Upvotes: 0

Vikram
Vikram

Reputation: 51571

The TextViews that you have defined in vehical_row.xml: @+id/plate, @+id/hyphen, @+id/model and @+id/driver are all missing the layout_width and the layout_height attributes. These are required.

For now, set these attributes as "wrap_content". For example:

<TextView
        android:id="@+id/plate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        style="@style/vehicleDefaultFont.plate"
        android:text="@string/label_text_view" />

Do the above for the other three as well. This should work for you.

Upvotes: 1

Related Questions