0

Hi am trying to create a android app with a sql database and i want to list my results in a listview but cant seem to get it to work please help I have this so far and am getting the error

java.lang.IllegalArgumentException: column '_id' does not exist

SimpleCursorAdapter adp;

    public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);

         db.open();


         // from array: keeps the names of the Cursor columns.
         String[] from = {"CURSOR_FIELD1","CURSOR_FIELD2"};
         // to array: keeps the ids of the fields in your list item layout
         int[] to = {R.id.itemLayoutfield1, R.id.itemLayoutfield2};
         // sets the basic layout of the ListActivity (the layout must contain a ListView element with the id called 'list').
         setContentView(R.layout.view_data_layout);

         // gets de cursor with the rows        
         c = db.getAllCourses(); 

         adp = new SimpleCursorAdapter(this, R.layout.row, c, from, to);             
         setListAdapter(adp);

        ListView listView = getListView();
        listView.setTextFilterEnabled(true);


        listView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                Toast.makeText(getApplicationContext(),
                ((TextView) view).getText(), Toast.LENGTH_SHORT).show(); 

                displayAllCourses(c);

                db.close();


            }               

        });

and my dbadapter i have

public Cursor getAllCourses() {

    return db.query(DATABASE_TABLE, new String[] { ID, Time_Amount,
            Seed_Acre, MM_Settings, Acre_amount, Speed_Type, Acre_String, Seed_Type,  Hill_Name }, null, null,
            null, null, null, null);
}

public Cursor getCourse(long rowId) throws SQLException {

    Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] { ID,
            Time_Amount, Seed_Acre, MM_Settings, Acre_amount, Speed_Type, Acre_String, Seed_Type, Hill_Name },
            ID + "=" + rowId, null, null, null, null, null);
    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;


}
2
  • what is the error message? please post the log file Commented Jan 8, 2013 at 14:58
  • Sorry all stressed, There's no errors when running it just displays how many entry's are in the database. and i need it to list all the Hill_Name entry's in a list Commented Jan 8, 2013 at 15:22

1 Answer 1

1

Your 'hill' String array just contains one single item (which is not even the first row of the table).

To show the results of a query from a Cursor you'd better use the SimpleCursorAdapter object, intead of the ArrayAdapter, this object helps you to map the results of the query (cursor) to the fields of your View (user interface). It would be something like that:

public class YourListActivity extends ListActiviy{

    private SimpleCursorAdapter adp;
    private Cursor c;

    public void onCreate(Bundle savedInstanceState) {
             super.onCreate(savedInstanceState);

             ...
             // from array: keeps the names of the Cursor columns.
             String[] from = {"CURSOR_FIELD1","CURSOR_FIELD2", ...};
             // to array: keeps the ids of the fields in your list item layout
             int[] to = {R.id.itemLayoutfield1, R.id.itemLayoutfield2, ...};
             // sets the basic layout of the ListActivity (the layout must contain a ListView element with the id called 'list').
             setContentView(R.layout.listLayout);

             // gets de cursor with the rows        
             c = db.getAllCourses(); 

             adp = new SimpleCursorAdapter(this, R.layout.itemLayout, c, from, to);             
             setListAdapter(adp);

             ...
     }
}

Important note: Keep in mind that the CursorAdapter class and it's subclasses (i.e. SimpleCursorAdapter) needs that the primary key of the tables are named _id.

The listLayout.xml should be similar to (notice that it has a ListView with id 'list'):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content">
     <ListView android:id="@android:id/list"
               android:layout_width="fill_parent"
               android:layout_height="wrap_content"/>
 </LinearLayout>

And the itemlayout.xml could be something like:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
            <TextView
                android:id="@+id/itemLayoutfield1"
                android:text="TextView" />
        </LinearLayout>
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >
            <TextView
                android:id="@+id/itemLayoutfield2"
                android:text="TextView" />          
        </LinearLayout>
        <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >
            <TextView
                android:id="@+id/itemLayoutfield3"
                android:text="TextView" />          
        </LinearLayout>
</LinearLayout>
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you For replying :) Am new to Android. So the from array is things such as my ID, Time_Amount, Seed_Acre, MM_Settings, and so on then my to array the R.id.itemLayoutfield1 is that where the entrys from the database are placed after getting them from the cursor?
Also what is the adp part?
Am trying it out am getting red under adp
I added the code but am now getting 01-08 17:11:54.703: E/AndroidRuntime(15275): FATAL EXCEPTION: main 01-08 17:11:54.703: E/AndroidRuntime(15275): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.uk.t.app/co.uk.t.app.ViewDataActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
All sorted now :) Thank you very much
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.