I am testing a basic SQLite query and using a SimpleCursorAdapter to populate a ListView containing LinearLayout's that contain TextViews of the column data, but when I change the order of the TextView's, the data in the TextView's move with the order, not with the R.id.
In the example below, the order of the TextView's is @+id/rowid and @+id/name, and the data from the cursor correctly populates.
However if I modify the row.xml to change the order of the EditText's (I cut the entire TextView element and paste it on the opposite side of the other TextView), the data from the cursor stays in the same order, ie. the DatabaseHelper.NAME data from the cursor goes into the @+id/rowid EditText, and the _id data from the cursor goes into @+id/name
Is this normal behaviour? I expected the SimpleCursorAdapter 'from' columns to populate the 'to' views in their respective order in the constructor.
Example code:
Cursor usersCursor;
String query = String.format("SELECT _id, %s FROM %s",
DatabaseHelper.NAME, DatabaseHelper.TABLE);
cursor = db.getReadableDatabase().rawQuery(query, null);
usersCursorAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.row, usersCursor,
new String[] { "_id", DatabaseHelper.NAME },
new int[] { R.id.rowid, R.id.name },
0);
userList.setAdapter(usersCursorAdapter);
and the R.layout.row.xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/rowid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:textSize="36sp" />
</LinearLayout>
The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.
Try cleaning your project and running it again.
My guess is that the R.id
values in your R.java
file don't match the ones in the compiled Java code. This is entirely possible if you only changed the XML files. R.java
will be regenerated, but the classes which use R.id
are not recompiled with the new values. Since R.id
contains final
constants, the compiler can optimize the byte code by using the constant value directly rather than loading it from a variable. This directly causes the problem you are seeing when R.java
changes but the rest of your code is not recompiled.