Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8994727
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T23:29:42+00:00 2026-06-15T23:29:42+00:00

I write a program that can show data that stored in database in listview

  • 0

I write a program that can show data that stored in database in listview by cursor.but in runtime it gives me force close.I read logcat but i don t understand what is this error.I give my code and logcat.please help me

current_cart code:

public class current_cart extends ListActivity {

    private ListView mainListView = null;
    CustomSqlCursorAdapter adapter = null;
    private SqlHelper dbHelper = null;
    private Cursor currentCursor = null;

    private ListView listView = null;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.current_cart);

        if (this.dbHelper == null) {
            this.dbHelper = new SqlHelper(this);

        }

        listView = getListView();
        listView.setItemsCanFocus(false);
        listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        //listView.setClickable(true);

        Button btnClear = (Button) findViewById(R.id.btnClear);
        btnClear.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                Toast.makeText(getApplicationContext(),
                        " You clicked Clear button", Toast.LENGTH_SHORT).show();
              //  ClearDBSelections();
            }
        });

        new SelectDataTask().execute();

        this.mainListView = getListView();

        mainListView.setCacheColorHint(0);

    }

    @Override
    protected void onRestart() {
        super.onRestart();
        new SelectDataTask().execute();
    }

    @Override
    protected void onPause() {

        super.onPause();
        this.dbHelper.close();
    }

  //  protected void ClearDBSelections() {

      //  this.adapter.ClearSelections();

  //  }

    private class SelectDataTask extends AsyncTask<Void, Void, String> {

        protected String doInBackground(Void... params) {

            try {

                current_cart.this.dbHelper.createDatabase(dbHelper.dbSqlite);
                current_cart.this.dbHelper.openDataBase();

                current_cart.this.currentCursor = current_cart.this.dbHelper
                        .getCursor();

            } 
            catch (SQLException sqle) 
            {

                throw sqle;

            }
            return null;
        }

        // can use UI thread here
        protected void onPostExecute(final String result) {

            startManagingCursor(current_cart.this.currentCursor);
            int[] listFields = new int[] { R.id.txtTitle,R.id.txtprice };
            String[] dbColumns = new String[] { SqlHelper.COLUMN_TITLE,SqlHelper.COLUMN_NAME_DESC };

            current_cart.this.adapter = new CustomSqlCursorAdapter(
                    current_cart.this, R.layout.single_item,
                    current_cart.this.currentCursor, dbColumns, listFields,
                    current_cart.this.dbHelper);
            setListAdapter(current_cart.this.adapter);

        }
    }

}

CustomSqlCursorAdapter code:

public class CustomSqlCursorAdapter extends SimpleCursorAdapter {
    private Context mContext;

    private SqlHelper mDbHelper;
    private Cursor mCurrentCursor;

    public CustomSqlCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, SqlHelper dbHelper) {
        super(context, layout, c, from, to);
        this.mCurrentCursor = c;
        this.mContext = context;
        this.mDbHelper = dbHelper;

    }

    public View getView(int pos, View inView, ViewGroup parent) {
        View v = inView;
        if (v == null) {
            LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = inflater.inflate(R.layout.single_item, null);
        }

        if (!this.mCurrentCursor.moveToPosition(pos)) {
            throw new SQLException("CustomSqlCursorAdapter.getView: Unable to move to position: "+pos);
        }

        CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);


        cBox.setTag(Integer.valueOf(this.mCurrentCursor.getInt(0)));


        cBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {


            }
        });

        TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
        txtTitle.setText(this.mCurrentCursor.getString(this.mCurrentCursor
                .getColumnIndex(SqlHelper.COLUMN_TITLE)));
        TextView txtprice = (TextView) v.findViewById(R.id.txtprice);
        txtprice.setText(this.mCurrentCursor.getString(this.mCurrentCursor
                .getColumnIndex(SqlHelper.COLUMN_NAME_DESC)));

        return (v);
    }


}

SqlHelper code:

    public class SqlHelper extends SQLiteOpenHelper {
        private static final String DATABASE_PATH = "/data/data/com.example.bb/databases/";

        public static final String DATABASE_NAME = "ll";

        public static final String TABLE_NAME = "w";
        public static final int ToDoItems_VERSION = 1;

       // public static final String COLUMN_ID = "_id";             
        public static final String COLUMN_TITLE = "Good_Name";         
       public static final String COLUMN_NAME_DESC = "Good_UnitPrice";


        public SQLiteDatabase dbSqlite;
        private Context mContext;

        public SqlHelper(Context context) {
            super(context, DATABASE_NAME, null, 1);
            mContext = context;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            createDB(db);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w("SqlHelper", "Upgrading database from version " + oldVersion
                    + " to " + newVersion + ", which will destroy all old data");

            db.execSQL("DROP TABLE IF EXISTS w;");

            createDB(db);
        }

        public void createDatabase(SQLiteDatabase db) {
            createDB(db);
        }

        private void createDB(SQLiteDatabase db) {
            if (db == null) {
                db = mContext.openOrCreateDatabase(DATABASE_NAME, 0, null);
            }

            db.execSQL("CREATE TABLE IF NOT EXISTS w (Cart_ID INTEGER, Good_Name VARCHAR(50),Good_UnitPrice INTEGER (10),Quantity INTEGER);");
            db.setVersion(ToDoItems_VERSION);


        }

        public void openDataBase() throws SQLException {
            String myPath = DATABASE_PATH + DATABASE_NAME;

            dbSqlite = SQLiteDatabase.openDatabase(myPath, null,
                    SQLiteDatabase.OPEN_READWRITE);
        }

        @Override
        public synchronized void close() {
            if (dbSqlite != null)
                dbSqlite.close();

            super.close();
        }

        public Cursor getCursor() {
            SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();

            queryBuilder.setTables(TABLE_NAME);

            String[] asColumnsToReturn = new String[] {COLUMN_TITLE,COLUMN_NAME_DESC};

            Cursor mCursor = queryBuilder.query(dbSqlite, asColumnsToReturn, null,
                    null, null, null, null);

            return mCursor;
        }

       // public void clearSelections() {
         //   ContentValues values = new ContentValues();
          //  values.put(COLUMN_SELECTED, 0);
          //  this.dbSqlite.update(SqlHelper.TABLE_NAME, values, null, null);
       // }
    }

my logcat:

12-19 14:36:02.999: E/AndroidRuntime(436): FATAL EXCEPTION: main
12-19 14:36:02.999: E/AndroidRuntime(436): java.lang.IllegalArgumentException: column '_id' does not exist
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:297)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.widget.CursorAdapter.init(CursorAdapter.java:169)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.widget.CursorAdapter.<init>(CursorAdapter.java:117)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:52)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:78)
12-19 14:36:02.999: E/AndroidRuntime(436):  at com.example.nfc.CustomSqlCursorAdapter.<init>(CustomSqlCursorAdapter.java:28)
12-19 14:36:02.999: E/AndroidRuntime(436):  at com.example.nfc.current_cart$SelectDataTask.onPostExecute(current_cart.java:110)
12-19 14:36:02.999: E/AndroidRuntime(436):  at com.example.nfc.current_cart$SelectDataTask.onPostExecute(current_cart.java:1)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.os.AsyncTask.finish(AsyncTask.java:590)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.os.AsyncTask.access$600(AsyncTask.java:149)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:603)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.os.Looper.loop(Looper.java:126)
12-19 14:36:02.999: E/AndroidRuntime(436):  at android.app.ActivityThread.main(ActivityThread.java:3997)
12-19 14:36:02.999: E/AndroidRuntime(436):  at java.lang.reflect.Method.invokeNative(Native Method)
12-19 14:36:02.999: E/AndroidRuntime(436):  at java.lang.reflect.Method.invoke(Method.java:491)
12-19 14:36:02.999: E/AndroidRuntime(436):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
12-19 14:36:02.999: E/AndroidRuntime(436):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
12-19 14:36:02.999: E/AndroidRuntime(436):  at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T23:29:43+00:00Added an answer on June 15, 2026 at 11:29 pm

    A cursor adapter requires that the cursor you give it contains an _id column, as stated in the documentation :

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

    You need to add such a column to your table. It is the id that is used in all methods that mention an id (onItemSelected, getItemId, …)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to write a program that can stop and start services using SilverLight
I am trying to write a program that can do math with English words.
I'm teaching myself programming and today's challenge is to write a program that can
I am trying to write a prolog program that can find the tiles that
I'm trying to follow Java's JDBC tutorials to write a Java program that can
Hi I suppose to write a program that show the run time complixity I
I cannot use threads thus I want to write a server program that can
I am attempting to write a program that can play a list of flashes
So I've to write a simple program(that loops) where you can enter an int
I am trying to write a program that can replace text and replace also

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.