注册 登录
编程论坛 Android开发

java.lang.IllegalArgumentException: column '_id' does not exist 求助!

菜鸟要努力 发布于 2015-04-08 23:17, 4596 次点击
数据库的表有三列分别为_id,todo_text1,todo_text2
想要在listview的item中只显示todo_text1,todo_text2
然后出现了java.lang.IllegalArgumentException: column '_id' does not exist的错误
提示错误位置是 SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.b_item,myCursor,
下面是代码
程序代码:
package st.st.st;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteCursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class St3 extends Activity {
//体重记录
    private ToDoDB myToDoDB;
      private Cursor myCursor;
      private ListView myListView;
      private EditText myEditText1;//输入日期
      private EditText myEditText2;//输入体重
      private int _id;
      protected final static int MENU_ADD = Menu.FIRST;
      protected final static int MENU_EDIT = Menu.FIRST + 1;
      protected final static int MENU_DELETE = Menu.FIRST + 2;
      protected final static int MENU_BACK=Menu.FIRST+3;//菜单按钮“返回”功能

    @Override
     public boolean onOptionsItemSelected(MenuItem item)
      {
        super.onOptionsItemSelected(item);
        switch (item.getItemId())
        {
          case MENU_ADD:
            this.addTodo();
            break;
          case MENU_EDIT:
            this.editTodo();
            break;
          case MENU_DELETE:
            this.deleteTodo();
            break;
          case MENU_BACK:
              this.ReBack();
                break;
        }
        return true;
      }
   
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        super.onCreateOptionsMenu(menu);
        /*添加四个个MENU*/
        menu.add(Menu.NONE, MENU_ADD, 0, "添加");
        menu.add(Menu.NONE, MENU_EDIT, 0, "修改");
        menu.add(Menu.NONE, MENU_DELETE, 0,"删除");
        menu.add(Menu.NONE,MENU_BACK,0,"返回");
        return true;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.b);
         myListView = (ListView) this.findViewById(R.id.b_lv);
         myEditText1 = (EditText) this.findViewById(R.id.b_rq);//输入日期的edittext
         myEditText2 = (EditText) this.findViewById(R.id.b_tz);//输入体重的edittext
         myToDoDB = new ToDoDB(this);
         myCursor = myToDoDB.select();
         SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.b_item,myCursor,
                 new String[]{ToDoDB.FIELD_TEXT1,ToDoDB.FIELD_TEXT2},
                 new int[]{R.id.listtext_riqi,R.id.listtext_tizhong});
         myListView.setAdapter(adapter);
            
        myListView.setOnItemClickListener(new OnItemClickListener()
        {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                    long arg3) {
                // TODO Auto-generated method stub
                myCursor.moveToPosition(arg2);
                _id=myCursor.getInt(0);
                myEditText1.setText(myCursor.getString(1));
                myEditText2.setText(myCursor.getString(2));
            }
        });
        myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> arg0, View arg1,
                    int arg2, long arg3) {
                // TODO Auto-generated method stub
                SQLiteCursor sc=(SQLiteCursor)arg0.getSelectedItem();
                _id=sc.getInt(0);
                myEditText1.setText(sc.getString(1));
                myEditText2.setText(sc.getString(2));
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
               
            }
        });
        }
   
      /*添加数据*/
        private void addTodo() {
            // TODO Auto-generated method stub
            if(myEditText1.getText().toString().equals("")
                    && myEditText2.getText().toString().equals(""))
                return;
            myToDoDB.insert(myEditText1.getText().toString(),myEditText2.getText().toString());
            myCursor.requery();
            myListView.invalidateViews();
            myEditText1.setText("");
            myEditText2.setText("");
            _id=0;
        }
        
          /*编辑更改数据*/
        private void editTodo()
          {
            if (myEditText1.getText().toString().equals("")
                    && myEditText2.getText().toString().equals(""))
              return;
            myToDoDB.update(_id, myEditText1.getText().toString(),myEditText2.getText().toString());
            myCursor.requery();
            myListView.invalidateViews();
            myEditText1.setText("");
            myEditText2.setText("");
            _id = 0;
          }
        
          /*删除数据*/
        private void deleteTodo() {
            // TODO Auto-generated method stub
            if (_id == 0)
                  return;
                /* ?埃戈? */
                myToDoDB.delete(_id);
                myCursor.requery();
                myListView.invalidateViews();
                myEditText1.setText("");
                myEditText2.setText("");
                _id = 0;
            }
        
        private void ReBack() {
            // TODO Auto-generated method stub
            Intent intent = new Intent();  
            //设置Intent对象要启动的Activity  
            myToDoDB.close();
            intent.setClass(St3.this,St1Activity.class);  
            //通过Intent对象启动另外一个Activity  
            startActivity(intent);  
            St3.this.finish();
        }      
}

package st.st.st;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class ToDoDB extends SQLiteOpenHelper {
   
    private final static String DATABASE_NAME="todo_db";
    private final static int DATABASE_VERSION=1;
    private final static String TABLE_NAME="todo_table";
    public final static String FIELD_id="_id";
    public final static String FIELD_TEXT1="todo_text1";//日期
    public final static String FIELD_TEXT2="todo_text2";//体重

    public ToDoDB(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
         String sql = "CREATE TABLE " + TABLE_NAME + " (" + FIELD_id
                    + " INTEGER primary key autoincrement, " + FIELD_TEXT1 +" text,"+ FIELD_TEXT2 +" text)";
                db.execSQL(sql);
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
         String sql = "DROP TABLE IF EXISTS " + TABLE_NAME;
            db.execSQL(sql);
            onCreate(db);
    }
    public Cursor select(){
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null, null);
        return cursor;
    }
    public long insert(String text1,String text2){
        SQLiteDatabase db=this.getWritableDatabase();
        ContentValues cv=new ContentValues();
        cv.put(FIELD_TEXT1, text1);
        cv.put(FIELD_TEXT2, text2);
        long row=db.insert(TABLE_NAME, null, cv);
        return row;
    }
    public void delete(int id){
         SQLiteDatabase db = this.getWritableDatabase();
            String where = FIELD_id + " = ?";
            String[] whereValue =
            { Integer.toString(id) };
            db.delete(TABLE_NAME, where, whereValue);
    }
     public void update(int id, String text1,String text2)
      {
        SQLiteDatabase db = this.getWritableDatabase();
        String where = FIELD_id + " = ?";
        String[] whereValue =
        { Integer.toString(id) };
        ContentValues cv = new ContentValues();
        cv.put(FIELD_TEXT1, text1);
        cv.put(FIELD_TEXT2, text2);
        db.update(TABLE_NAME, cv, where, whereValue);
      }
}
   


1 回复
#2
菜鸟要努力2015-04-09 08:48
问题已解决 原来是忘记修改select()了...
1