0

in datbase i have three rows which i have checked that they are not null..

SimpleCursorAdapter adapter; //class bvariable
public void showlist()
    {
        Log.i("BookShelf", "11111111111");
        String[] col={DbHelper.BOOK_NAME ,DbHelper.FLAG};
        Cursor cursor=db.query(DbHelper.TABLE,col,null,null,null,null,null);
        if((cursor !=null)&&(cursor.getCount()>0))
        {
            Log.i("BookShelf", "**list view adapter 5**  " + getApplicationContext() );
        adapter = new ListViewAdapter(getApplicationContext(), cursor);
        Log.i("BookShelf", "show list ke andar 5");
            listview.setAdapter(adapter); 
            Log.i("BookShelf", "show list ke andar 6");
        }
                cursor.close();
              }

package com.himanshu;

import android.content.Context;
import android.database.Cursor;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.View;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class ListViewAdapter extends SimpleCursorAdapter 
{ 
    static String[] FROM={DbHelper.BOOK_NAME };
    static int[] TO ={R.id.book_name};

public ListViewAdapter(Context context, Cursor c)
{ 
super(context, R.layout.row, c, FROM, TO);
Log.i("BookShelf", "list view adapter");
}
// This is where the actual binding of a cursor to view happens
@Override
public void bindView(View row, Context context, Cursor cursor) 
{ 
super.bindView(row, context, cursor);
Log.i("BookShelf", " bind view ");
int flag = cursor.getInt(cursor.getColumnIndex(DbHelper.FLAG));
if(flag==1)
{
TextView tick = (TextView) row.findViewById(R.id.tick); 
tick.setText("ADDED"); 
}
}
}

and the log looks like ::::

02-29 16:01:20.862: I/BookShelf(2437):  **list view adapter 5**  android.app.Application@44f3f8c8
02-29 16:01:20.881: D/AndroidRuntime(2437): Shutting down VM
02-29 16:01:20.881: W/dalvikvm(2437): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
02-29 16:01:20.921: E/AndroidRuntime(2437): FATAL EXCEPTION: main
02-29 16:01:20.921: E/AndroidRuntime(2437): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.himanshu/com.himanshu.AddActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.os.Looper.loop(Looper.java:123)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.ActivityThread.main(ActivityThread.java:4627)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at java.lang.reflect.Method.invokeNative(Native Method)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at java.lang.reflect.Method.invoke(Method.java:521)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at dalvik.system.NativeStart.main(Native Method)
02-29 16:01:20.921: E/AndroidRuntime(2437): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.widget.CursorAdapter.init(CursorAdapter.java:111)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at com.himanshu.ListViewAdapter.<init>(ListViewAdapter.java:18)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at com.himanshu.AddActivity.showlist(AddActivity.java:200)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at com.himanshu.AddActivity.onCreate(AddActivity.java:91)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-29 16:01:20.921: E/AndroidRuntime(2437):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-29 16:01:20.921: E/AndroidRuntime(2437):     ... 11 more

so on seeing the log its clear that there is some problem with the instantiation of the ListViewAdapter... please help me out...

Himanshu Kohli
  • 135
  • 2
  • 9
  • Possible duplicate of http://stackoverflow.com/questions/5812030/java-lang-illegalargumentexception-column-id-does-not-exist – Jim Blackler Feb 29 '12 at 10:53
  • 2
    make sure,your table and cursor which you pass to adapter class,contains _id column – Hiral Vadodaria Feb 29 '12 at 10:54
  • @Hiral:: thanx for ur advice.. i have included the _id in the cursor and now its working.. the constructor of ListViewAdapter is being called but its not calling the bindView function and its not setting the adapter also... the log looks like:: -----------SHOW LIST KE ANDAR 5 android.app.Application@44f3f8c8 --------- list view adapter ---------show list ke andar 5 -----------show list ke andar 6 – Himanshu Kohli Feb 29 '12 at 11:05
  • @HimanshuKohli: why don't you try overriding getView() method of adapter? – Hiral Vadodaria Feb 29 '12 at 11:08

1 Answers1

0

Check that your query gets the _id column (and all columns you display) and replace cursor.close(); with startManagingCursor(cursor);

Mohamed_AbdAllah
  • 5,311
  • 3
  • 28
  • 47