Note that there are some explanatory texts on larger screens.

plurals
  1. PODebugging SimpleCursorAdapter
    text
    copied!<p>I'm working on my first Android app and can't figure out how to get my <code>SimpleCursorAdpater</code> to populate the view. The cursor that I'm passing in has results in it, so the problem must be somewhere in instantiating the adapter or in binding it to the view. I'm sort of at my wits end since no exceptions are thrown and I can't really step into setListAdapter.</p> <p>Here is how i get my cursor in the first place:</p> <pre><code> Searches searches = new Searches(this); SQLiteDatabase db = searches.getReadableDatabase(); //select _id, Name, Search FROM Searches; Cursor c = db.query( SearchConstants.TABLE_NAME, FROM, null, null, null, null, null); startManagingCursor(c); </code></pre> <p>And this is the schema do my db:</p> <pre><code>CREATE TABLE Searches (_id INTEGER PRIMARY KEY, Name Text, Search TEXT) </code></pre> <p>Here are the two lines where things start to fall apart:</p> <pre><code>SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.search, cursor, FROM, TO); setListAdapter(adapter); </code></pre> <p>My main layout looks like this:</p> <pre><code>&lt;ListView android:id="@android:id/android:list" android:layout_width="wrap_content" android:layout_height="wrap_content" /&gt; &lt;TextView android:id="@android:id/android:empty" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/empty" /&gt; </code></pre> <p>Here is the view to fill with each result:</p> <pre><code>&lt;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"&gt; &lt;TextView android:id="@+id/_id" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content"/&gt; &lt;TextView android:id="@+id/colon" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text=": " android:layout_toRightOf="@id/name" /&gt; &lt;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" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Finally here are the static variables I used:</p> <pre><code>//search query stuff private static String[] FROM = {SearchConstants._ID, SearchConstants.NAME_COLUMN, SearchConstants.SEARCH_COLUMN}; //where to paste search results private static int[] TO = {R.id._id, R.id.name, R.id.search}; /** * Table name */ public static final String TABLE_NAME = "Searches"; /** * Name Column */ public static final String NAME_COLUMN = "Name"; /** * Search Column */ public static final String SEARCH_COLUMN = "Search"; </code></pre> <p>I think this is all of the relevant code. I have no idea how to proceed at this point, so any suggestions at all would be helpful.</p> <p>Thanks, brian</p> <p>PS: Looks like theres a lot of great suggestions here - i'm not ignoring them i just havent had the chance yet. Thanks for the advice! At some point i'll go thru them all and try to give some feedback as to which things worked well for me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload