I am working on my first Android application and cannot figure out how to get my SimpleCursorAdpaterfill in the view. The cursor I pass has results in it, so the problem should be somewhere in creating an adapter instance or binding it to the view. I’m sort of at my end, since no exceptions are thrown, and I can’t take a step in setListAdapter.
This is how I take my cursor first:
Searches searches = new Searches(this);
SQLiteDatabase db = searches.getReadableDatabase();
Cursor c = db.query(
SearchConstants.TABLE_NAME,
FROM, null, null, null,
null, null);
startManagingCursor(c);
And this is my db schema:
CREATE TABLE Searches (_id INTEGER PRIMARY KEY, Name Text, Search TEXT)
Here are two lines where things begin to fall apart:
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.search, cursor, FROM, TO);
setListAdapter(adapter);
My main layout is as follows:
<ListView
android:id="@android:id/android:list"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@android:id/android:empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/empty" />
Here is a view to fill out with each result:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10sp">
<TextView
android:id="@+id/_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/colon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=": "
android:layout_toRightOf="@id/name" />
<TextView
android:id="@+id/search"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:singleLine="true"
android:textStyle="italic"
android:layout_toRightOf="@id/colon" />
</RelativeLayout>
Finally, here are the static variables that I used:
private static String[] FROM = {SearchConstants._ID, SearchConstants.NAME_COLUMN, SearchConstants.SEARCH_COLUMN};
private static int[] TO = {R.id._id, R.id.name, R.id.search};
public static final String TABLE_NAME = "Searches";
public static final String NAME_COLUMN = "Name";
public static final String SEARCH_COLUMN = "Search";
, . , , .
,
PS: , - , . ! - , .