2014-01-28 3 views
0

간단한 연락처 목록 앱을 만들려고합니다. 새 연락처를 만들고 연락처 필드를 입력하고 저장 버튼을 누르면 "잘못된 열 ID가 없습니다."라는 오류가 발생합니다.간단한 어댑터를 사용하여 '_id'열이 없습니다.

데이터베이스에서 목록 위젯으로 데이터를 표시하는 데 간단한 커서 어댑터를 사용하고 있습니다. 여기

내 소스 코드 : 여기
public class WebpreneurActivity extends ListActivity { 

    private static final int CONTACT_CREATE = 0; 
    private static final int CONTACT_EDIT = 1; 

    //select the second one, Android view menu 
    private static final int INSERT_ID = Menu.FIRST; 
    private static final int DELETE_ID = Menu.FIRST + 1; 

    private DBHandler dbHelper; 
    private Cursor c; 
    ImageButton imageButton; 
    long rowid; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     Log.d("database1" ,"0"); 
     Log.d("Your Location4", "ok4:"); 
     super.onCreate(savedInstanceState); 
     Log.d("database1" ,"1"); 
     setContentView(R.layout.activity_webpreneur); 
     Log.d("database1" ,"2"); 
     dbHelper = new DBHandler(this); 
     Log.d("database1" ,"3"); 
     dbHelper.open(); 
     //addListenerOnButton(); 
     //dbHelper.addContact(); 

     imageButton = (ImageButton) findViewById(R.id.imageButton1); 
     Log.d("database1" ,"button"); 
     imageButton.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 
       Log.d("database1" ,"b4"); 
       Intent i = new Intent(getApplicationContext(), ContactEdit.class); 
       startActivityForResult(i, CONTACT_CREATE); 

       Log.d("database1" ,"button3"); 
       //fillData(); 

      } 

     }); 

     //dbHelper.close(); 
    } 

    @SuppressWarnings("deprecation") 
    public void fillData() { 
     Log.d("Your Location4", "ok6:"); 
     c = dbHelper.fetchAllRows(); 
     Log.d("Your Location4", "ok8:"); 
     startManagingCursor(c); 
     Log.d("Your Location4", "ok2:"); 
     // point of error !!!! 
     //////  ListAdapter adapter = new SimpleCursorAdapter(this, 
     R.layout.contact_row, c, new String[] { DBHandler.Key_Name, 
         DBHandler.Key_Phone }, new int[] { R.id.name, 
         R.id.phonenumber }); 
     setListAdapter(adapter); 
    } 

    protected void onListItemClick(ListView l, View v, int position, long id) { 
     super.onListItemClick(l, v, position, id); 

     Intent i = new Intent(this, ContactEdit.class); 
     i.putExtra(DBHandler.Key_ID, c.getLong(c.getColumnIndex(DBHandler.Key_ID))); 
     i.putExtra(DBHandler.Key_Name, c.getString(c.getColumnIndex(DBHandler.Key_Name))); 
     i.putExtra(DBHandler.Key_Address, c.getString(c.getColumnIndex(DBHandler.Key_Address))); 
     i.putExtra(DBHandler.Key_Phone, c.getString(c.getColumnIndex(DBHandler.Key_Phone))); 
     i.putExtra(DBHandler.Key_Website, c.getString(c.getColumnIndex(DBHandler.Key_Home))); 
     startActivityForResult(i, CONTACT_EDIT); 
    } 

    protected void onActivityResult(int requestCode, int resultCode, 
      Intent data) { 
     super.onActivityResult(requestCode, resultCode, data); 

     if (resultCode == RESULT_OK) { 
      String name = data.getStringExtra(DBHandler.Key_Name); 
      String address = data.getStringExtra(DBHandler.Key_Address); 
      String mobile = data.getStringExtra(DBHandler.Key_Phone); 
      String home = data.getStringExtra(DBHandler.Key_Home); 
      switch (requestCode) { 
       case CONTACT_CREATE: 
        Log.d("Your Location4", "jj:"); 
        dbHelper.createRow(name, address, mobile, home); 
        Log.d("Your Location4", "ok90:"); 
        fillData(); 
        break; 
       case CONTACT_EDIT: 

        String id = data.getStringExtra(DBHandler.Key_ID); 


        long rowId=Long.parseLong(id); 

        if (rowId != (Long)null){ 
         dbHelper.updateRow(rowId, name, address, mobile, home); 
        } 
        fillData(); 
        break; 

      } 
     } 
    } 
} 

나의 LOG 고양이

01-28 13:34:34.841: D/AndroidRuntime(24324): Shutting down VM 
     01-28 13:34:34.841: W/dalvikvm(24324): threadid=1: thread exiting with uncaught exception (group=0x4001d578) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): FATAL EXCEPTION: main 
     01-28 13:34:34.871: E/AndroidRuntime(24324): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=0, result=-1, data=Intent { (has extras) }} to activity {com.webpreneur_contactlist/com.webpreneur_contactlist.WebpreneurActivity}: java.lang.IllegalArgumentException: column '_id' does not exist 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.ActivityThread.deliverResults(ActivityThread.java:2553) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.ActivityThread.handleSendResult(ActivityThread.java:2595) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.ActivityThread.access$2000(ActivityThread.java:121) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:973) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.os.Handler.dispatchMessage(Handler.java:99) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.os.Looper.loop(Looper.java:138) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.ActivityThread.main(ActivityThread.java:3701) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at java.lang.reflect.Method.invokeNative(Native Method) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at java.lang.reflect.Method.invoke(Method.java:507) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:878) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:636) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at dalvik.system.NativeStart.main(Native Method) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.support.v4.widget.CursorAdapter.init(CursorAdapter.java:174) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.support.v4.widget.CursorAdapter.<init>(CursorAdapter.java:122) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.support.v4.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:54) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.support.v4.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:63) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at com.webpreneur_contactlist.WebpreneurActivity.fillData(WebpreneurActivity.java:82) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at com.webpreneur_contactlist.WebpreneurActivity.onActivityResult(WebpreneurActivity.java:117) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.Activity.dispatchActivityResult(Activity.java:3908) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): at android.app.ActivityThread.deliverResults(ActivityThread.java:2549) 
     01-28 13:34:34.871: E/AndroidRuntime(24324): ... 11 more 

내 Dbhandler 클래스 입니다 ///////////////// 공용 클래스가 DBHandler는 SQLiteOpenHelper를 확장 {

private static final int DB_Version = 1; 

    private static final String DB_Name = "Places"; 

    protected static final String Places_Table = "Places"; 
    private String DB_PATH=""; 

    String name, Address, Website,ID; 
    //WebpreneurActivity Contact; 
public static final String Key_ID = "ID"; 
public static final String Key_Name = "Name"; 
public static final String Key_Phone = "Phone"; 
public static final String Key_Address = "Address"; 
public static final String Key_Website = "Website"; 
public static final String Key_Home = "HOME"; 
public static final String PROJECTION[] = { 
    Key_ID, 
    Key_Name, 
    Key_Address, 
    Key_Phone, 
    Key_Home 
}; 

    String CREATE_PLACES_TABLE = "create table if not exists Places_Table (id integer primary key ,"+ 
      "name VARCHAR not null, phone VARCHAR not null, address VARCAHR not null, website VARCHAR not null,Home VARCHAR not null)"; 

    private final Context context; 
    private static SQLiteDatabase db; 

    public DBHandler(Context context) { 

     super(context, DB_Name, null, DB_Version); 
     this.context = context; 
     DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/"; 
     Log.d("database1" ,"4"); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 
     try{ 
      Log.d("DB", "DB creationnewwwww"); 
       db.execSQL("CREATE TABLE " + Places_Table + " (" + 
         Key_ID + " INTEGER PRIMARY KEY autoincrement," + 
         Key_Name + " TEXT NOT NULL," + 
         Key_Phone + " TEXT NOT NULL," + 
         Key_Address + " TEXT NOT NULL," + 
         Key_Website + " TEXT," + 
         Key_Home + " TEXT)"); 
       Log.d("DB", "DB creationnewwwwwwwwwwwwwwwwwwwwwwwwwww"); 
     } 
     catch(SQLiteException e){ 
      Log.d("DB", "DB creation excptionhhhhhhhhhhhhhhh"); 
      e.printStackTrace(); 
     } 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
     // TODO Auto-generated method stub 

     db.execSQL("Drop Table If Exists" + Places_Table); 
     onCreate(db); 
    } 

    /* void addContact() { 
      SQLiteDatabase db = this.getWritableDatabase(); 

      ContentValues values = new ContentValues(); 
      values.put(Key_Name,Name); // Contact Name 
      values.put(KEY_COST,cost); // Contact Phone 
      values.put(Key_Address, contact.Address); //Contact phone no 

      // Inserting Row 
      db.insert(Places_Table, null, values); 
      db.close(); // Closing database connection 
     }*/ 

    public void open() 
    {Log.d("DB", "DB creation 9"); 
    db = this.getWritableDatabase(); 
     //onCreate(db); 
    Log.d("DB", "DB creation 9"); 
    String myPath = DB_PATH + DB_Name; 
    db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 
    } 
    @Override 
    public synchronized void close() { 
     if(db != null) 
      db.close(); 
     super.close(); 
    } 

    //Adding Places 
    void addPlaces(int id, String name,String phone, String address,String url){ 

     Log.d("DB", "DB creation 1"); 
     //SinglePlaceActivity single = new SinglePlaceActivity(); Log.d("DB", "DB creation 2"); 
     ContentValues contentValues = new ContentValues(); 
     Log.d("DB", "DB creation 3"); 
     contentValues.put(Key_ID, id); 
     Log.d("DB", "DB creation 4"); 
     contentValues.put(Key_Name, name); 
     contentValues.put(Key_Phone, phone); 
     contentValues.put(Key_Address, address); 
     contentValues.put(Key_Website, url); 
     Log.d("DB", "DB creation 4"); 
     db.insert(Places_Table, null, contentValues); 
     Log.d("DB", "DB creation 5555"); 
     //db.close(); 
    } 

    public String getdata() { 
     // TODO Auto-generated method stub 
     String [] columns =new String[]{Key_ID ,Key_Name,Key_Address,Key_Website}; 
     Cursor c =db.query(DB_Name, columns, null, null, null, null, null); 
     String Result=""; 
     int iRow=c.getColumnIndex(Key_ID); 
     int iName=c.getColumnIndex(Key_Name); 
     int iAddress=c.getColumnIndex(Key_Address); 
     int iWebsite=c.getColumnIndex(Key_Website); 
     for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){ 
      Result=Result+c.getString(iRow)+" "+c.getString(iName)+ " "+c.getString(iAddress)+ " "+c.getString(iWebsite)+ "\n"; 
     } 

     return Result; 
    } 


    public void createRow(String name, String address, String Phone, String home) 
    {SQLiteDatabase db = this.getWritableDatabase(); 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put(Key_Name, name); 
     initialValues.put(Key_Address, address); 
     initialValues.put(Key_Phone, Phone); 
     initialValues.put(Key_Home, home); 
       //pass the initialValues to the database to insert the row 
     Log.d(Key_Home, home); 
     db.insert(Places_Table, null, initialValues); 
     Log.d("Your Location4", "okna:"); 
    } 

     public void deleteRow(long rowId){ 
     db.delete(Places_Table, Key_ID+"="+rowId,null); 
    } 

     public boolean updateRow (long rowId, String name, String address, String Phone, String home){ 
     ContentValues args = new ContentValues(); 
     args.put(Key_Name, name); 
     args.put(Key_Address, address); 
     args.put(Key_Phone, Phone); 
     args.put(Key_Home, home); 
     return db.update(Places_Table, args, Key_ID +"="+ rowId, null)>0; 
    } 
     public Cursor fetchRow(long rowId) throws SQLException{ 
      Cursor result = db.query(Places_Table, null, 
        Key_ID + "=" + rowId, null, null, null,null); 
      if ((result.equals(rowId)) || !result.isFirst()) { 
       throw new SQLException("No note matching ID: " + rowId); 
      } 
      return result; 
     } 

     public Cursor fetchAllRows(){ 
      Log.d("Your Location4", "ok99:"); 
      return db.query(Places_Table, PROJECTION, 
        null, null, null, null, null); 

     } 

} 
+0

열 _id가 있는지 확인 하시겠습니까? – Raghunandan

+0

나는 key_id autoincremented를 만들었다. 그렇지 않다면 나는 염려하는만큼 이형제를 갖고 있지 않지만 피곤함에 따라이 문제를 해결하도록 도와 준다. – Shazar

+0

데이터베이스 구조를 변경 했습니까? 그렇다면 앱을 제거한 다음 앱을 테스트중인 기기/에뮬레이터에 다시 설치해야합니다. –

답변

2

커서 어댑터는,843 필요커서가 작동하는 필드. http://developer.android.com/reference/android/widget/CursorAdapter.html

당신이 원하는 필드에 _id을 설정할 수있는 다음의 CursorWrapper를 사용할 수 있습니다. 구글 사용자 JC에서 언급 한 바와 같이 https://groups.google.com/d/msg/ormlite-user/SbRoHIov5pI/-8Ir3pEycw0J

: 당신의 fillData

@SuppressWarnings("deprecation") 
public void fillData() { 
    Log.d("Your Location4", "ok6:"); 
    // Load the Cursor in the normal way 
    c = dbHelper.fetchAllRows(); 
    // wrap the cursor in the NoIdCursorWrapper to handle the missing _id field 
    // (int this case the DBHandler.Key_ID field will act as the _id) 
    NoIdCursorWrapper nc = new NoIdCursorWrapper(c, DBHandler.Key_ID); 
    Log.d("Your Location4", "ok8:"); 
    startManagingCursor(c); 
    Log.d("Your Location4", "ok2:"); 

    // Add the NoIdCursorWrapper instead of the Cursor itself 
    ListAdapter adapter = new SimpleCursorAdapter(this, 
        R.layout.contact_row, nc, new String[] { DBHandler.Key_Name, 
        DBHandler.Key_Phone }, new int[] { R.id.name, 
        R.id.phonenumber }); 
    setListAdapter(adapter); 
} 
+0

볼 수 있습니다 지금이 문제를 해결하는 데 도움주세요 제발이 문제를 해결하려면 완전히 비어 있습니다? – Shazar

+0

하지만이 코드를 내 코드와 어떻게 통합합니까? – Shazar

+0

커서에서 어댑터 대신 CursorWrapper를 사용합니다. 정상적인 방법으로 Cursor를 읽은 다음 Cursor를 매개 변수로 사용하여 NoIdCursorWrapper를 만들고 Cursor 대신 Adapter에 Wrapper를 전달합니다. –

-1

/** 
* A {@link CursorWrapper} implementation that allows a {@link Cursor} 
* without a field named "_id" to be used with various Android {@link ListView} 
* classes that expect a column named "_id". This is done by specifying an 
* alias field name to be used in place of "_id". 
* 
* @author [email protected] 
*/ 
public class NoIdCursorWrapper extends CursorWrapper { 

    private int idColumnIndex; 

    /** 
    * Create a NoIdCursorWrapper using the alias column index. 
    * @param c the cursor to wrap 
    * @param idColumnIndex the column index to use as the _id column 
     alias 
    */ 
    public NoIdCursorWrapper(Cursor c, int idColumnIndex) { 
      super(c); 
      this.idColumnIndex = idColumnIndex; 
    } 

    /** 
    * Create a NoIdCursorWrapper using the alias column name. 
    * @param c the cursor to wrap 
    * @param idColumnName the column name to use as the _id column alias 
    */ 
    public NoIdCursorWrapper(Cursor c, String idColumnName) { 
      super(c); 
      idColumnIndex = c.getColumnIndex(idColumnName); 
    } 

    @Override 
    public int getColumnIndex(String columnName) { 
      int index = super.getColumnIndex(columnName); 
      if (index < 0 && "_id".equals(columnName)) { 
        index = idColumnIndex; 
      } 
      return index; 
    }; 


    @Override 
    public int getColumnIndexOrThrow(String columnName) throws IllegalArgumentException { 
      int index = getColumnIndex(columnName); 
      if (index>=0) { 
        return index; 
      } 
      // let the AbstractCursor generate the exception 
      return super.getColumnIndexOrThrow(columnName); 
    }; 

} 그런 다음

당신은 당신의 경우 어댑터에있는 _ID 열을 포함해야 그것을 표시하지 마십시오.

ListAdapter adapter = new SimpleCursorAdapter(this, 
    R.layout.contact_row, c, new String[] { "_id", DBHandler.Key_Name, 
        DBHandler.Key_Phone }, new int[] { R.id.name, 
        R.id.phonenumber }); 
+0

커서에서 '예'열이 아니라 예. – laalto

0

귀하의 PROJECTION"ID"하는 Key_ID 있습니다. 모두 CursorAdapters"_id" 이름의 열이 있어야합니다. id 열의 이름을 _id으로 변경해야합니다. 빠른 해결 방법으로 열 별칭을 만들 수도 있습니다.

public static final String PROJECTION[] = { 
    Key_ID + " AS _id", 
관련 문제