Debugging SimpleCursorAdapter

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();
    //select _id, Name, Search FROM Searches;
    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:

//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";

, . , , .

,

PS: , - , . ! - , .

+3
7

, . , Android . Eclipse, .

http://android.opensourceror.org/2010/01/18/android-source/

, , . , , , , .

cursor.moveToFirst()
+3

, - . , . : -, , , ,

System.out.println("cursor.getCount()="+cursor.getCount());

setAdapter(). , , , ;-) , .

, , , :

android:id="@+id/android:list"

:

android:id="@android:id/android:list"

main.xml. : android: id = "@+ id/android: empty".

, xml-layout ( simple_list_item_1) , :

ListAdapter adapter = new SimpleCursorAdapter(this, 
    // Use a template that displays a text view
    android.R.layout.simple_list_item_1, 
    // Give the cursor to the list adapter
    cursor, 
    // Map the NAME column in your database to...
    new String[] {SearchConstants.NAME_COLUMN} ,
    // ...the "text1" view defined in the R.layout.simple_list_item_1
    new int[] {android.R.id.text1}
);

, . , !

+2

, Simplecursoradapter .

, ! , ...

, !

+1

, , . , ( sqlite ). .

, , , , ( ) SimpleCursorAdapter, . ( ):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.dbManager = new DatabaseManager(this);

    setContentView(R.layout.main);
    registerForContextMenu(getListView()); // catch clicks

    if (!showList()) {
        TextView tv = new TextView(this);
        tv.setText(getString(R.string.txt_list_empty));
        setContentView(tv);
    }
}

private boolean showList() {
    final Cursor c = dbManager.fetchListData();
    startManagingCursor(c); // when the Activity finishes, the cursor is closed

    if (!c.moveToFirst())
        return false;

    final SimpleCursorAdapter myAdapter = new SimpleCursorAdapter(
            this, android.R.layout.simple_list_item_2, c,
            new String[]{"name"} , new int[]{android.R.id.text1} );

    setListAdapter(myAdapter);
    return true;
}

, , , , . , .

0

ViewBinder . , SimpleCursorAdaptor. , .

- :

adapter.setViewBinder(new SimpleCursorBinder());

public class SimpleCursorBinder implements SimpleCursorAdpater.ViewBinder {
    public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
        /* set breakpoints and examine incoming data. */
        // returning false, causes SimpleCursorAdapter to handing the binding
        boolean bound = false;

        String columnName = cursor.getColumnName(columnIndex);
        TextView bindingView = null;
        int viewId = view.getId();

        // could just use this opportunity to manually bind
        if (columnName.equals(SearchConstants._ID)) {
            bindingView = (TextView)(viewId == R.id._id ? view : null);
        } else if (columnName.equals(SearchConstants.NAME_COLUMN)) {
            bindingView = (TextView)(viewId == R.id.name ? view : null);
        } else if (columnName.equals(SearchConstants.SEARCH_COLUMN)) {
            bindingView = (TextView)(viewId == R.id.search ? view : null);
        }

        if (bindingView != null) {
            bindingView.setText(cursor.getString(columnIndex));
            bound = true;
        }

        return bound;
    }
}
0

, - . , - .

, startManagingCursor (c) , . , , .

(, , , .)

if (c != null) {
        c.moveToFirst();
    }

:

public Cursor getQueryCursor() 
    {
     Cursor c;
     c = null;
     try{
         c =  myDataBase.query(TABLE_MYTABLE, new String[] {
          COLUMN_ID,COLUMN_LABEL, COLUMN_TEXT}, 
                null, 
                null, 
                null, 
                null, 
                null);

         if (c != null) {
             c.moveToFirst();
         }

     }catch(Exception ec)
     {
      Log.w("MY_APP", ec.getMessage());

     }
     return c;
    }

Then, applying the result of the query, I often put try / catch arguments in it and add breakpoints at these points (sometimes getMessage () returns null, but other exception properties will highlight the problem. From LogCat. I could often solve the root of my problems in the following way.

try{
    SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.search, cursor, FROM, TO);
    setListAdapter(adapter);
}catch(IllegalStateException e)
{
    Log.w("MyApp", e.getMessage());
}catch(Exception es)
{
    Log.w("MyApp", es.getMessage());
}
0
source

Please check the SQLite database, it should contain the column "_id" as the primary key. I had the same problem, and finally I figured it out ...

0
source

Source: https://habr.com/ru/post/1771841/


All Articles