Peter Fox
Peter Fox

Reputation: 1849

Android SQLite Data using SimpleCursorAdapter not populating more than one row in the listview?

Basically I have code that initialises the database in a DatabaseHelper extended class (see below) and I'm currently getting only one of the results to display?

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE IF NOT EXISTS host ("
            + BaseColumns._ID
            + " INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, mac_address VARCHAR)");

    ContentValues host_values = new ContentValues();
    host_values.put("name", "test host");
    host_values.put("mac_address", "ffffffffffff");

    db.insert("host", null, host_values);

    ContentValues host_values2 = new ContentValues();
    host_values.put("name", "test me host");
    host_values.put("mac_address", "eeeeeeeeeeee");

    db.insert("host", null, host_values2);

}

This is the code, I'm using a ListFragment over a ListActivity is that makes any difference?

public class TargetListFragment extends ListFragment {

private SQLiteDatabase database;

private CursorAdapter dataSource;

private static final String fields[] = { "name", "mac_address", 
    BaseColumns._ID };

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
    DatabaseHelper helper = new DatabaseHelper(this.getActivity().getApplicationContext());
    database = helper.getWritableDatabase();
    Cursor data = database.query("host", fields, 
        null, null, null, null, null);

    dataSource = new SimpleCursorAdapter(this.getActivity().getApplicationContext(), 
        R.layout.target_row, data, fields,  
        new int[] { R.id.target_name, R.id.mac_address });

    setListAdapter(dataSource);
}

This is really bothering me as it seems almost like the cursor is only dealing with the first row and then being closed but I can't think of anything that would cause that?

Again in case it's useful here's the XML of the row view.

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_height="wrap_content" android:id="@+id/target_row_layout"
            android:orientation="vertical" android:layout_width="wrap_content">

            <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:id="@+id/target_name"
                    style="@style/target_list.item_name" /> <!--style="@style/target_list.item_name"-->
            <TextView android:layout_width="fill_parent"
                    android:layout_height="wrap_content" android:id="@+id/mac_address"
                    style="@style/target_list.mac_address" /> <!--style="@style/target_list.mac_address"-->
    </LinearLayout>

Upvotes: 0

Views: 789

Answers (1)

Sam
Sam

Reputation: 86948

You are using host_values where you meant to use host_values2... insert() won't add a second row because all of the values inside host_values2 are null:

ContentValues host_values2 = new ContentValues();
host_values2.put("name", "test me host");  // Change variable
host_values2.put("mac_address", "eeeeeeeeeeee");  // Change variable

db.insert("host", null, host_values2);

(Alternatively you can simply remove host_values2 and write over the previous data host_values.)

Upvotes: 1

Related Questions