2012-08-08 35 views
0

我找到了解決辦法恢復我的價值觀,看得我populateFields()新版本 好了,所以我一直在讀通過所有的微調和這裏SQlite的帖子,似乎無法找到很好的答案,我正在尋找,所以我張貼這種情況。 我的應用程序有兩個屏幕,並使用SQLite數據庫我的設備上保存從editTexts的名稱和重量字段爲字符串,像這樣更新從SQLite數據庫我的微調

String eName = name.getText().toString(); 
String eWeight = weight.getText().toString();// where name and weight are EditTexts 

,我有兩個微調如下

String eReps = spinReps.getSelectedItem().toString(); 
String eSets = spinSets.getSelectedItem().toString(); 

然後我打電話這要添加到數據庫

long id = mDbHelper.createExercise(eName, eWeight, eReps, eSets); 

這是我的問題是,當有人選擇創建一個新的練習,我的應用程序崩潰,因爲它嘗試錯誤地填充微調器。這是我目前的。

private void populateFields(){ 
    if(mRowId != null){ // where mRowId is the selected row from the list 
    Cursor exercise = mDbHelper.fetchExercise(); 
    name.setText(exercise.getString(exercise. 
       getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME))); 
    weight.setText(exercise.getString(exercise. 
       getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT))); 
// this is the part that i need help with, I do not know how to restore 
// the current items spinner value for reps and sets from the database. 

    spinReps.setSelection(exercise.getString(exercise. 
       getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS))); 

    spinSets.setSelection(exercise.getString(exercise. 
       getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS))); 
} 

我想我需要使用某種適配器來恢復我的列表項以及數據庫中的當前值,我只是不知道如何。

有人可以幫助我嗎?

**下面是我的解決方案**

我不得不搬到我ArrayAdapters repsAdapter,spinAdapter了我的onCreate() ,然後實施這一新的populateFields()

private void populateFields(){ 
    if(mRowId != null){ 
     Cursor exercise = mDbHelper.fetchExercise(mRowId); 
     // same as before 
     name.setText(exercise.getString(exercise. 
         getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME))); 
     // get the string for sReps and sSets from the database 
     String sReps = exercise.getString(exercise. 
         getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)); 
     String sSets = exercise.getString(exercise. 
         getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS)); 
     // use the strings to get their position in my adapters 
     int r = repsAdapter.getPosition(sReps); 
     int s = setsAdapter.getPosition(sSets); 
     // set their returned values to the selected spinner Items 
     spinReps.setSelection(r); 
     spinSets.setSelection(s); 
     // same as before 
     weight.setText(exercise.getString(exercise. 
         getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT))); 
    } 
} 

回答

0

的方式將微調器設置爲值而不是位置取決於您使用的適配器。

  1. ArrayAdapter,這一個很簡單:

    int position = adapter.getPosition(exercise.getString(exercise. 
           getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS))); 
    spinReps.setSelection(position); 
    
  2. SimpleCursorAdapter,這個人是有點難度:

    String match = exercise.getString(exercise.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)); 
    
    Cursor cursor = adapter.getCursor(); 
    cursor.moveToPosition(-1); 
    int columnIndex = cursor.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS); 
    while(cursor.moveToNext()) { 
        if(cursor.getString(columnIndex).equals(match)) 
         break; 
    } 
    spinReps.setSelection(cursor.getPosition()); 
    

如果您使用的是不同類型的適配器並且無法修改上面的代碼以適應它,請告訴我。希望有所幫助!

+0

我想了解如何使用我的字符串值來獲取字符串在我的適配器中的位置並使用返回的值來設置我的微調器中的位置後,我剛剛閱讀了此答案,我不確定,但我相信這是你在做什麼解釋。感謝您付出的努力,在我需要幫助之前,我已經自己找到了自己的工作解決方案!我會將你的答案設置爲我的問題的解決方案,因爲它似乎是我做到這一點的方式。 – 2012-08-08 20:00:25

+0

是的,您的解決方案與我的第一個選項相同。感謝您將我的答案標記爲正確,我很高興您也找到了答案! – Sam 2012-08-08 20:54:32