2010-12-20 3 views
-1

내가이가 나를 위해 열심히 너무 뭉크입니다 내가 테스트하고 구글을 많이 이해하려고 노력 오전 List<Friend>.SQLite DB에서 클래스로 커서 내용을 추출하는 방법은 무엇입니까?

을 반환 한 후 SQLite 데이터베이스에서 모든 사용자를 추출하고 MyDBAdapter에 기능을 할 필요가 예제 및 유래 게시물하지만 난 그것을 내가이 코드를 tryed

을 할 수 있지만 그렇지 않은 일을하지, C는 ursorIndexOutOfBoundsException: Index -1 requested, with a size of 3

public List<Friend> retrieveAllUsers() 
    { 
     List <Friend> friends=new ArrayList<Friend>(); 

     Cursor result=fetchAllUsers(); 

     for (int i=0; i<result.getCount();i++) 
     { 
      //note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); 
      friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), "","","","")); 
     } 


     return friends; 
    } 

은 누군가가 나 그 기능을 수행 할 수 있습니다하세요?

나는이 같은 사용자 (친구) 클래스가 있습니다

public class Friend { 
private String email; //id de usuario 
    private String password; 
    private String fullName; 
    private String movilephone; 
    private String movileOperatingSystem; 

또한 내가 이런 SQLite 데이터베이스가 있습니다

public class MyDbAdapter { 

    private static final String TAG = "NotesDbAdapter"; 
    private DatabaseHelper mDbHelper; 
    private SQLiteDatabase mDb; 
    private static final String DATABASE_NAME = "gpslocdb"; 
    private static final String PERMISSION_TABLE_CREATE = "CREATE TABLE permission (fk_email1 varchar, fk_email2 varchar, validated tinyint, hour1 time default '08:00:00', hour2 time default '20:00:00', date1 date, date2 date, weekend tinyint default '0', fk_type varchar, PRIMARY KEY (fk_email1,fk_email2))"; 
    private static final String USER_TABLE_CREATE = "CREATE TABLE user (email varchar, password varchar, fullName varchar, mobilePhone varchar, mobileOperatingSystem varchar, PRIMARY KEY (email))"; 

    private static final int DATABASE_VERSION = 2; 

    private final Context mCtx; 

    private static class DatabaseHelper extends SQLiteOpenHelper { 

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

     @Override 
     public void onCreate(SQLiteDatabase db) { 
      db.execSQL(PERMISSION_TABLE_CREATE); 
      db.execSQL(USER_TABLE_CREATE); 
     } 

     @Override 
     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
      db.execSQL("DROP TABLE IF EXISTS user"); 
      db.execSQL("DROP TABLE IF EXISTS permission"); 
      onCreate(db); 
     } 
    } 

    /** 
    * Constructor - takes the context to allow the database to be 
    * opened/created 
    * 
    * @param ctx the Context within which to work 
    */ 
    public MyDbAdapter(Context ctx) { 
     this.mCtx = ctx; 
    } 

    /** 
    * Open the database. If it cannot be opened, try to create a new 
    * instance of the database. If it cannot be created, throw an exception to 
    * signal the failure 
    * 
    * @return this (self reference, allowing this to be chained in an 
    *   initialization call) 
    * @throws SQLException if the database could be neither opened or created 
    */ 
    public MyDbAdapter open() throws SQLException { 
     mDbHelper = new DatabaseHelper(mCtx); 
     mDb = mDbHelper.getWritableDatabase(); 
     return this; 
    } 

    public void close() { 
     mDbHelper.close(); 
    } 

    public long createUser(String email, String password, String fullName, String mobilePhone, String mobileOperatingSystem) 
    { 
     ContentValues initialValues = new ContentValues(); 
     initialValues.put("email",email); 
     initialValues.put("password",password); 
     initialValues.put("fullName",fullName); 
     initialValues.put("mobilePhone",mobilePhone); 
     initialValues.put("mobileOperatingSystem",mobileOperatingSystem); 
     return mDb.insert("user", null, initialValues); 
    } 


    public Cursor fetchAllUsers() { 

     return mDb.query("user", new String[] {"email", "password", "fullName", "mobilePhone", "mobileOperatingSystem"}, null, null, null, null, null); 
    } 

    public Cursor fetchUser(String email) throws SQLException { 

     Cursor mCursor = mDb.query(true, "user", new String[] {"email", "password", "fullName", "mobilePhone", "mobileOperatingSystem"} 
      , "email" + "=" + email, null, null, null, null, null); 
     if (mCursor != null) { 
      mCursor.moveToFirst(); 
     } 
     return mCursor; 
    } 
} 

답변

2
Cursor result=fetchAllUsers(); 
    if(result.moveToFirst()){ 
     do{ 
      //note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE))); 
      friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), "","","","")); 
     }while(result.moveToNext()); 
    } 
1

첫째, 당신이 커서를 이동해야합니다을 첫 번째 행을 찾은 다음 movoToFirst가 성공한 경우 moveToNext를 사용하십시오.

Cursor result=fetchAllUsers(); 
    if (result.moveToFirst()) { 
    do { 
     friends.add(new Friend(result.getString(result.getColumnIndexOrThrow("email")), "","","","")); 
    } while (result.moveToNext()); 
} 
+0

Ups, 너무 늦은 것처럼 보입니다. – martinpelant

관련 문제