Search code examples
androidsqlitelistviewcursor

LIstView shows no data while populating through cursor


I am new to Android development. I am using ListView to list some data from SQLite database. my Main.xml file looks like:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/inspections"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
            <!-- Header -->
    <LinearLayout android:id="@+id/header"
        android:background="#ff347c12"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        >
        <TextView android:id="@+id/item1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
            android:height="30dip"
            android:text="Id"
        />
        <TextView android:id="@+id/item2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Customer"
            android:width="100dip"
            android:height="30dip"
        />
        <TextView android:id="@+id/item3"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Contract"
            android:width="100dip"
            android:height="30dip"
        />
        <TextView android:id="@+id/item4"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:text="Inspector"
            android:width="100dip"
            android:height="30dip"
        />
    </LinearLayout>

    <!-- List Divider -->
    <View android:layout_width="fill_parent"
        android:layout_height="1dip"
        android:background="?android:attr/listDivider" />

    <!-- ListView (grid_items) -->
    <LinearLayout android:id="@+id/layout"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent">
        <ListView android:id="@+id/listview"
            android:layout_height="fill_parent"
            android:layout_width="fill_parent">
        </ListView>
    </LinearLayout>
</LinearLayout>

I am using grid_item.xml which is used to hold data of each row, it looks like:

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
        <TextView android:id="@+id/item1"
            android:text="row_id"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="20dip"
        />
        <TextView android:id="@+id/item2"
            android:text="col_1"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
        />
        <TextView android:id="@+id/item3"
            android:text="col_2"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
        />
        <TextView android:id="@+id/item4"
            android:text="col_3"
            android:layout_height="fill_parent"
            android:layout_width="wrap_content"
            android:width="100dip"
        /> </LinearLayout>

Code:

public class Inspections extends Activity{
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        try
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.inspections);

            LoadInspections();
        }
        catch (Exception e) {
            CommonMethods.ShowMessageBox(this, e.toString());
        }
    }

    private void LoadInspections()
    {
        try
        {
            DBAdapter db = new DBAdapter(this);
            db.open();
            Cursor cur = db.GetInspections();
            if(cur.moveToFirst())
            {
                startManagingCursor(cur);
                ListView lv = (ListView)findViewById(R.id.listview);
                // create the grid item mapping
                String[] from = new String[] {"_id", "customer", "contract", "inspector"};
                int[] to = new int[] {R.id.item1, R.id.item2, R.id.item3, R.id.item4 };

                // fill in the grid_item layout
                SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.grid_item, cur, from, to);

                lv.setAdapter(adapter);  
                Log.d("Inspection", "Records found " + cur.getString(1) + "/ "+ cur.getString(2)+ "/ "+ cur.getString(3));
            }
            else
            {
                Log.d("Inspection", "No reords");
            }

            db.close();
        }
        catch(Exception ex)
        {
            Log.d("Inspection", "Error: "+ ex.toString());
        }
     }
}

Problem: There is something wrong with LoadInspections(), the list-view show no data but only the HEADER-ROW. The cursor is retrieving data, check this line:

Log.d("Inspection", "Records found " + cur.getString(1) + "/ "+ cur.getString(2)+ "/ "+ cur.getString(3));

The log window is displaying the data.

I tried my best to find the answer on stackoverflow but could not succeeded. Thanks in advance for your valuable time & help.


Solution

  • I was trying different things & when i removed the "Header" & "List Divider" sections from my main.xml it worked. Very Strange??

    So my main.xml now looks like:

    <?xml version="1.0" encoding="utf-8"?>
     <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/inspections"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
    
        <!-- ListView (grid_items) -->
        <LinearLayout android:id="@+id/layout"
            android:layout_width="wrap_content"
            android:layout_height="fill_parent">
            <ListView android:id="@+id/mylistview"
                android:layout_height="fill_parent"
                android:layout_width="fill_parent">
            </ListView>
        </LinearLayout>
    </LinearLayout>
    

    Can somebody comment on this strange behavior of listview?