Cursor Adapters require the table to have a _id field. Else, you would get
this exception.
Kumar Bibek
http://techdroid.kbeanie.com
http://www.kbeanie.com



On Fri, Dec 3, 2010 at 11:15 AM, pramod.deore <[email protected]>wrote:

> Hi Everyone,
>
> I have a table and I want to retrieve one column (RoomName) from this
> table and display it as a list.  had tried it but it gives me
> exception as " java.lang.IllegalArgumentException: column '_id' does
> not exist"
>
> My .java class is
>
> public class RemoveRoom extends Activity
> {
>         SQLiteDatabase sampleDB = null;
>         String SAMPLE_DBNAME = "HomeAutoDataBase";
>         String ROOM_TABLE_NAME = "RoomTable";
>         String SWITCH_TABLE_NAME = "SwitchTable";
>         protected ListAdapter adapter;
>         protected ListView roomList;
>
>
>
>         @Override
>         public void onCreate(Bundle savedInstanceState)
>         {
>                 System.out.println ("First statement in RemoveRoom");
>                super.onCreate(savedInstanceState);
>                setContentView(R.layout.roomlist);
>                roomList = (ListView) findViewById (R.id.list);
>                displayRoomList();
>
>         }
>
>         public void displayRoomList()
>         {
>                 try
>                {
>                        sampleDB =  this.openOrCreateDatabase(SAMPLE_DBNAME,
> MODE_PRIVATE, null);
>                        Cursor c = sampleDB.rawQuery("SELECT RoomName FROM "
> +ROOM_TABLE_NAME, null);
>                        System.out.println ("Total number of rooms
> are"+c.getCount());
>
>
>                          adapter = new SimpleCursorAdapter(
>                          this,
>                          R.layout.roomlist,
>                          c,
>                          new String[] {"RoomName"},
>                          new int[] {R.id.roomname}); //here I got
> exception
>          roomList.setAdapter(adapter);
>
>                }
>                 catch (Exception e)
>                 {
>                         e.printStackTrace();
>                 }
>         }
>
> }
>
>
> my .xml file is
>
> <?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="fill_parent">
>
>        <ListView
>           android:id="@+id/list"
>           android:layout_width="fill_parent"
>           android:layout_height="fill_parent"/>
>
>    <TextView
>            android:id="@+id/roomname"
>            android:layout_width="wrap_content"
>            android:layout_height="wrap_content"/>
>
>
> </LinearLayout>
>
>
> And my logcat gives me
>
> 12-03 10:48:03.540: WARN/System.err(573):
> java.lang.IllegalArgumentException: column '_id' does not exist
> 12-03 10:48:03.560: WARN/System.err(573):     at
> android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:
> 314)
> 12-03 10:48:03.570: WARN/System.err(573):     at
> android.widget.CursorAdapter.init(CursorAdapter.java:111)
> 12-03 10:48:03.580: WARN/System.err(573):     at
> android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
> 12-03 10:48:03.589: WARN/System.err(573):     at
> android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:
> 47)
> 12-03 10:48:03.600: WARN/System.err(573):     at
> android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:88)
> 12-03 10:48:03.611: WARN/System.err(573):     at
> com.monarch.home.RemoveRoom.displayRoomList(RemoveRoom.java:47)
> 12-03 10:48:03.640: WARN/System.err(573):     at
> com.monarch.home.RemoveRoom.onCreate(RemoveRoom.java:29)
> 12-03 10:48:03.651: WARN/System.err(573):     at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:
> 1047)
> 12-03 10:48:03.660: WARN/System.err(573):     at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:
> 2459)
> 12-03 10:48:03.669: WARN/System.err(573):     at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:
> 2512)
> 12-03 10:48:03.679: WARN/System.err(573):     at
> android.app.ActivityThread.access$2200(ActivityThread.java:119)
> 12-03 10:48:03.700: WARN/System.err(573):     at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
> 12-03 10:48:03.710: WARN/System.err(573):     at
> android.os.Handler.dispatchMessage(Handler.java:99)
> 12-03 10:48:03.710: WARN/System.err(573):     at
> android.os.Looper.loop(Looper.java:123)
> 12-03 10:48:03.720: WARN/System.err(573):     at
> android.app.ActivityThread.main(ActivityThread.java:4363)
> 12-03 10:48:03.730: WARN/System.err(573):     at
> java.lang.reflect.Method.invokeNative(Native Method)
> 12-03 10:48:03.750: WARN/System.err(573):     at
> java.lang.reflect.Method.invoke(Method.java:521)
> 12-03 10:48:03.770: WARN/System.err(573):     at
> com.android.internal.os.ZygoteInit
> $MethodAndArgsCaller.run(ZygoteInit.java:860)
> 12-03 10:48:03.781: WARN/System.err(573):     at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
> 12-03 10:48:03.790: WARN/System.err(573):     at
> dalvik.system.NativeStart.main(Native Method)
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to