user1880779
user1880779

Reputation: 2078

Android ListView Database Exception

I have been a naughty boy and I've copied a method from official Notepad application from android developer site, this is my class :

package com.example.prva;

import android.app.ListActivity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;


public class ListView extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        fillData();
        }

    private void fillData() {
        // Get all of the notes from the database and create the item list
        Cursor c = DatabaseManager.getAllData();
        startManagingCursor(c);

        String[] from = new String[] { DatabaseManager.TABLE_COLUMN_ONE };
        int[] to = new int[] { R.id.text1 };

        // Now create an array adapter and set it to display using our row
        SimpleCursorAdapter notes =
            new SimpleCursorAdapter(this, R.layout.notes_row, c, from, to);
        setListAdapter(notes);
    }
    }

When I try to run this ListActivity I get this error :

01-31 02:39:14.259: E/AndroidRuntime(1845): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prva/com.example.prva.ListView}: java.lang.IllegalArgumentException: column '_id' does not exist

Now I understand this because its true, I do not have the _id column in my database (the notepad application database has it and used it but I have my own database), I just don't understand where is that column mentioned in my ListActivity class? Where is it being called from so it gives the error?

Upvotes: 0

Views: 171

Answers (1)

StarPinkER
StarPinkER

Reputation: 14271

See the documentation for CursorAdapter:

The Cursor must include a column named _id or this class will not work.

In your code, you use the SimpleCursorAdapter, which is a derived class, so it appears this statement applies.

Cursors are like iterators or pointers, they contain nothing but a mechanism for transversing the data, they contain no columns themselves.

From another documentation, you can understand the statement above better:

Handling content URI IDs

By convention, providers offer access to a single row in a table by accepting a content URI with an ID value for the row at the end of the URI. Also by convention, providers match the ID value to the table's _ID column, and perform the requested access against the row that matches.

This convention facilitates a common design pattern for apps accessing a provider. The app does a query against the provider and displays the resulting Cursor in a ListView using a CursorAdapter. The definition of CursorAdapter requires one of the columns in the Cursor to be _ID.

Several ways for you to fix the problem:

  1. Add a column "_id" in your table.

  2. Using alias to get the curosr. For example:

    SELECT someid as _id, name, number FROM TABLE1;

Upvotes: 1

Related Questions