listview和cursoradapter相关的错误

时间:2017-05-02 16:37:12

标签: android listview android-cursoradapter

我正在尝试使用游标适配器使用数据库数据填充listview。

问题: 它抛出一个运行时错误,应用程序崩溃。我无法理解错误。

用于游标适配器

        public bookmarkcursoradapter(Context context, Cursor c) {
            super(context, c, false);
        }

        @Override
        public void bindView(View view, Context context, Cursor cursor) {

            final TextView title=(TextView) view.findViewById(R.id.booktitle);
            TextView url=(TextView) view.findViewById(R.id.bookurl);
            TextView time=(TextView) view.findViewById(R.id.time);
            title.setText(cursor.getString(1));
            url.setText(cursor.getString(2));
            time.setText(cursor.getString(3));
        }
        @Override
        public View newView(Context context, Cursor cursor, ViewGroup parent) {
            LayoutInflater inflater3=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            return  inflater3.inflate(R.layout.rowbookmark,parent,false);


        }


    }

主要活动中游标适配器的使用代码

Cursor bookmarkcursor;
    bookmarkcursoradapter adapter;
    ListView bookmark,history;


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

            setContentView(R.layout.activity_browser);
      bookmarkcursor = database.getallbookmark();
       if (bookmarkcursor!=null) {
           adapter = new bookmarkcursoradapter(browser.this, bookmarkcursor);
           bookmark.setAdapter(adapter);
       }
       else{
           adapter = new bookmarkcursoradapter(browser.this,null);
           bookmark.setAdapter(adapter);
       }  


    //bookmark adding 
                            bookmarkbutton.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {


                                    database.addContact(new databasemodel(titile,url2));
                                    bookmarkcursor=database.getallbookmark();
                                    adapter.changeCursor(bookmarkcursor);


                                }
                            });
 bookmark.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                   @Override
                   public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                      bookmarkcursor.moveToPosition(position);

                       String bookurl= database.geturlbookmark(bookmarkcursor.getInt(0));
                       mTabHost.setCurrentTab(mTabHost.getCurrentTab());
                       wb.loadUrl(bookurl);
                       drawer.closeDrawer(Gravity.END,TRUE);

                   }
               });
               bookmark.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                   @Override
                   public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                       bookmarkcursor.moveToPosition(position);
                       database.deletebookmark(bookmarkcursor.getInt(0));
                        bookmarkcursor=database.getallbookmark();
                        adapter.changeCursor(bookmarkcursor);
                       adapter.notifyDataSetChanged();
                       Toast.makeText(browser.this,"bookmarkdeleted", LENGTH_LONG).show();
                       return true;
                   }
               });

**注意:**我只发布主要活动的cursoradapter相关代码

数据库代码

public class databasehelper extends SQLiteOpenHelper {
    private  static int DATABASE_VERSION =25;
    public   static final String DATABASE_NAME = "browse1.db";
    public static final String TABLE_BOOKMARK = "Bookmark";
    public static final String TABLE_HISTORY= "History";
    public  static final String COLUMN_ID = "ID";
    public static final String COLUMN_title = "Title";
    public  static final String COLUMN_url = "Url";
    public  static final String COLUMN_creat = "createdat";


    public databasehelper(Context context) {
        super(context, DATABASE_NAME,null,DATABASE_VERSION);
    }



    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_BOOKMARK + " ( " + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_title + " TEXT, " + COLUMN_url + " TEXT," + COLUMN_creat + " TEXT "+");");
        db.execSQL("create table " + TABLE_HISTORY + " ( " + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_title + " TEXT, " + COLUMN_url + " TEXT," + COLUMN_creat + " TEXT "+");");
    }

@Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_BOOKMARK);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_HISTORY);
        // Create tables again
        onCreate(db);
    }


   public void addContact(databasemodel d) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_title, d.gettitle()); // Contact Name
        values.put(COLUMN_url, d.getURL()); // Contact Phone
       values.put(COLUMN_creat,d.getDateTime());
        // Inserting Row
        db.insert(TABLE_BOOKMARK, null, values);
        db.close(); // Closing database connection
    }
    public void addHistory(databasemodel d) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(COLUMN_title, d.gettitle()); // Contact Name
        values.put(COLUMN_url, d.getURL()); // Contact Phone
        values.put(COLUMN_creat,d.getDateTime());
        // Inserting Row
        db.insert(TABLE_HISTORY, null, values);
        db.close(); // Closing database connection
    }

    /*public ArrayList<databasemodel> getAllContacts() {
        ArrayList<databasemodel> contactList = new ArrayList<databasemodel>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_BOOKMARK;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                databasemodel structure = new databasemodel();
                structure.setid(Integer.parseInt(cursor.getString(0)));
                structure.setTitle(cursor.getString(1));
                structure.setURL(cursor.getString(2));
                structure.setTIME(cursor.getString(3));
                // Adding contact to list
                contactList.add(structure);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }*/
    public  Cursor getallbookmark(){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor bookmarkcursordb;
        String selectQuery = "SELECT  * FROM " + TABLE_BOOKMARK;
        bookmarkcursordb=db.rawQuery(selectQuery,null);
        if (bookmarkcursordb != null) {
            bookmarkcursordb.moveToFirst();
        }
        return bookmarkcursordb;

    }
    public ArrayList<databasemodel> getAllhistory() {
        ArrayList<databasemodel> contactList = new ArrayList<databasemodel>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_HISTORY;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                databasemodel structure = new databasemodel();
                structure.setid(Integer.parseInt(cursor.getString(0)));
                structure.setTitle(cursor.getString(1));
                structure.setURL(cursor.getString(2));
                structure.setTIME(cursor.getString(3));
                // Adding contact to list
                contactList.add(structure);
            } while (cursor.moveToNext());
        }

        // return contact list
        return contactList;
    }
    public void deletebookmark(int id) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(" DELETE FROM " + TABLE_BOOKMARK + " WHERE " + COLUMN_ID+ " = " + " ' " + id + " ' ");
        db.close();

    }
    public String geturlbookmark( int id){
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor;
        String QUERY=" SELECT " + COLUMN_url + " FROM " + TABLE_BOOKMARK + " WHERE " + COLUMN_ID + " = " + " ' " + id + " ' ";
        cursor=db.rawQuery(QUERY,null);

           String url   = cursor.getString(0);

             cursor.close();
             return url;


    }
    public ArrayList<String> getbookmarkurl()
    {
        ArrayList<String> bookmarkurl= new ArrayList<>();
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor;
        String QUERY=" SELECT " + COLUMN_url + " FROM " + TABLE_BOOKMARK ;
        cursor=db.rawQuery(QUERY,null);
        if (cursor.moveToFirst()) {
            do {

                // Adding contact to list
                bookmarkurl.add(cursor.getString(0));
            } while (cursor.moveToNext());
        }

        // return contact list
        return bookmarkurl;

    }
    }

logcat错误

05-02 21:11:53.111 3901-3901/com.example.lenovo1.tabhostbrowser E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: com.example.lenovo1.tabhostbrowser, PID: 3901
                                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lenovo1.tabhostbrowser/com.example.lenovo1.tabhostbrowser.browser}: java.lang.IllegalArgumentException: column '_id' does not exist
                                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                   Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
                                                                                      at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:333)
                                                                                      at android.widget.CursorAdapter.init(CursorAdapter.java:180)
                                                                                      at android.widget.CursorAdapter.<init>(CursorAdapter.java:144)
                                                                                      at com.example.lenovo1.tabhostbrowser.bookmarkcursoradapter.<init>(bookmarkcursoradapter.java:0)
                                                                                      at com.example.lenovo1.tabhostbrowser.browser.onCreate(browser.java:222)
                                                                                      at android.app.Activity.performCreate(Activity.java:6679)
                                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)

plz帮助。提前谢谢。

0 个答案:

没有答案