Note that there are some explanatory texts on larger screens.

plurals
  1. POforce close in creating simplecursor adapter
    primarykey
    data
    text
    <p>I have the following code which should display in a list the content from a database (a certain table and a certain column) but I get a force close dialog and I don't know why.</p> <p>This is my code:</p> <pre><code>public class Server8 extends Activity { DBAdapter db; @Override public void onCreate (Bundle savedInstanceState) { setContentView(R.layout.main); db=new DBAdapter(this); db.openDataBase(); Cursor cursor=db.getAllData2(); startManagingCursor(cursor); String[] from=new String[] {db.KEY_ROUTE}; int[] to = new int[] { R.id.name_entry}; ContactListCursorAdapter items = new ContactListCursorAdapter(this, R.layout.list_example_entry, cursor, from, to); } public class ContactListCursorAdapter extends SimpleCursorAdapter{ private Context context; private int layout; public ContactListCursorAdapter (Context context, int layout, Cursor c, String[] from, int[] to) { super(context, layout, c, from, to); this.context = context; this.layout = layout; } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { Cursor c = getCursor(); final LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(layout, parent, false); int nameCol = c.getColumnIndex(db.KEY_ROUTE); String name = c.getString(nameCol); TextView name_text = (TextView) v.findViewById(R.id.name_entry); if (name_text != null) { name_text.setText(name); } return v; } @Override public void bindView(View v, Context context, Cursor c) { int nameCol = c.getColumnIndex(db.KEY_ROUTE); String name = c.getString(nameCol); TextView name_text = (TextView) v.findViewById(R.id.name_entry); if (name_text != null) { name_text.setText(name); } } </code></pre> <hr> <p>In my DBAdapter I have this:</p> <pre><code>public static final String TABLE_2= "route"; public static final String KEY_ROWID_2="_id"; public static final String KEY_ROUTE= "route"; public static final String KEY_USER_ID= "user_id"; </code></pre> <p>This is how I query for the cursor:</p> <pre><code>public Cursor getAllData2() { return db.query(TABLE_2, new String[] {KEY_ROUTE},null,null,null,null,null); } </code></pre> <hr> <p>and this is what my logcat gives me:</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.CursorAdapter.init(CursorAdapter.java:111)</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.CursorAdapter.(CursorAdapter.java:90)</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.ResourceCursorAdapter.(ResourceCursorAdapter.java:47)</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at android.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:88)</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at com.server.Server8$ContactListCursorAdapter.(Server8.java:103)</p> <p>04-30 09:26:24.323: ERROR/AndroidRuntime(2676): at com.server.Server8.onCreate(Server8.java:91)</p> <hr> <p>Caused by: java.lang.IllegalArgumentException: column '_id' does not exist</p> <p>I'm not even asking for something like this!!</p> <p>and this is what my <code>TABLE_2</code> looks like:</p> <pre><code>_id route user_id 1 Sebes-Alba 1 ....only one record </code></pre> <p>What I'm doing wrong? Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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