2012-10-18 25 views
2

其值設置找到了我的問題,檢查下的「更新」爲代碼動態創建一個微調和SQLite的

好吧,我已經從加載的sqlite的正確的價值爲我的微調的一個問題。這裏是我的應用程序是如何構建

我的onCreate()通過設置一個微調叫spinTypes:

public class MyClass extends Activity{ 
// local members . . . 
    . 
    . 
    . 
//spinner 
private Spinner spinTypes, spinNames = null; 

// string array decl. 
private String [] types, names1, names2, names3 = null; 

// array adapters for string arrays 
private ArrayAdapter<String> typesAdapter, names1Adapter, 
          names2Adapter, names3Adapter; 

// create a Dbhelper object to access the db 
private DbHelper mdbHelper = null; 

// on create 
@Override 
public void onCreate(Bundle savedIstanceState){ 
    // call to super class 
    // set my content view 
    // instantiate all members 

    // Dbhelper object to access the db 
    mdbHelper = new DbHelper(getBaseContext()); 

    // spinners 
    spinTypes = (Spinner) findViewById(R.id.spin_types); 
    spinNames = (Spinner) findViewById(R.id.spin_names); 

// get string arrays from resources and create a ArrayAdapter<string> 
types = getResources().getStringArray(R.array.types); 

typesAdapter = new ArrayAdapter<String>(getBaseContext(), 
             android.R.layout.simple_spinner_item, types); 
typesAdapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

// want to create a dynamic spinner based on these arrays 
names1 = getResources().getStringArray(R.array.names_1); 
names2 = getResources().getStringArray(R.array.names_2); 
names3 = getResources().getStringArray(R.array.names_3); 

// do this for all names++ arrays 
names1Adapter = new ArrayAdapter<String>(getBaseContext(), 
             android.R.layout.simple_spinner_item, names1); 
names1Adapter. setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

// etc . . . for the other two 

// set the adapter for the types spinner 
spinTypes.setAdapter(typesAdapter); 

} 

我有我的所有數據插入到工作正常的數據庫創建方法,我的問題是當我想嘗試和填充旋轉編輯條目。

這個應用程序的工作原理是這樣的:你按添加按鈕彈出,在調用視圖創建一個列表項的對話框。我用兩個微調器來創建它,一個是動態地填充第二名稱微調基於該類型的類型。

當我訪問數據庫對象我從微調,但由於某種原因,返回的字符串時,我創造我的if語句檢查字符串數組中的第二微調器設置每次位置0的位置。

的方法包括,如果我上述的說法是這樣:

public void populate(){ 
    mdbHelper.open(); 
    // if there is an item create a cursor rowId != null 
    Cursor mTypes = mdbHelper.fetchItem(rowId); 
    if(mTypes.moveToFirst()){ 
    String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE)); 
    String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME)); 

    int t = spinTypes.getPosition(tType); 
    spinTypes.setSelection(t); 

    // ** this does not seem to do the job i want it to** 
    if(t == 0){ 
    spinNames.setAdapter(names1Adapter); // set the adapter based on t value 
    int n1 = names1Adapter.getPosition(tName); // use name return from db to get pos 
    spinNames.setSelection(n1);   // set the position 
    }else if(t ==1){ 
    spinNames.setAdapter(names2Adapter); 
    int n2 = names1Adapter.getPosition(tName); 
    spinNames.setSelection(n2); 
    }else{ 
    spinNames.setAdapter(names3Adapter); 
    int n3 = names3Adapter.getPosition(tName); 
    spinNames.setSelection(n3); 
    } 

}// end populate 

} // end class 

紗廠所有的工作,但他們不與TNAME我正在返回值結束。

任何人都可以幫忙嗎?

**我記錄從數據庫返回的名稱,並用它來找到我的錯誤項的指數,我返回正確的值,但微調仍然默認爲0 **

任何人都知道我的錯誤可能是什麼?請

更新(最終代碼貼在下面) 我做了相當多的變化來得到這個工作,以瞭解我做了什麼,你應該閱讀這整個職位,這樣你就不會錯過任何東西!

添加一個變量之前onCreate() private int spinNamePos;

我的onCreate()

// add a listener to the spinner 
spinTypes.setOnItemSelectedListener(TypesListener); 

最後一行之後添加此行,並創造了這個監聽器,以便

OnItemSelectedListener TypesListener = new OnItemSelectedListener(){ 
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){ 
    // use the position of this spinner to set the second spinner 
    switch(pos){ 
    case 0 : 
     spinNames.setAdapter(names1Adapter); 
     break; 
     //etc. . for the other two 
    case 1: 
     // etc 
     break; 
    } 
    case 2: 
     // etc 
     break; 
    } 
    // created an int for the position of the second spinner before calling onCreate() 
    // and use it to set this spinners position 
    spinNames.setSelection(spinNamePos); 
// this value is grabbed in onSavedInstanceState() and used to set the pos when onRestoreInstanceState() is called to handle orientation changes and activity paused 
    } 
}; 

然後我改變了我的onPopulate()像調用一種新方法來設置第二個微調器一樣工作位置

public void populate(){ 
    mdbHelper.open(); 
    // if there is an item create a cursor rowId != null 
    Cursor mTypes = mdbHelper.fetchItem(rowId); 
    if(mTypes.moveToFirst()){ 
    String tType = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_TYPE)); 
    String tName = mTypes.getString(mTypes.getColumnIndexOrThrow(DbAdapter.KEY_NAME)); 

    int t = spinTypes.getPosition(tType); 
    spinTypes.setSelection(t); 

    // call to the new method 
    spinNamePos = setSpinNamesPos(t, tName); 
    // set the position 
    spinNames.setSelection(spinNamePos) 


}// end populate 

// new method to set the position when i want to update/change the list item 

private int setSpinNamesPos(int m, String name){ 
int pos = 0; 
switch(m){ 
    case 0: 
    pos = names1Adapter.getPosition(name); 
    break; 
    case 1: 
    pos = names2Adapter.getPosition(name); 
    break; 
    case 2: 
    pos = names3Adapter.getPosition(name); 
    break; 
    } 
return pos; 
} 

非常高興看到這方面的工作有任何疑問或鼓勵

+0

您是否在類型'Spinner'的字符串數組與數據庫中的其他名稱數組之間存在映射關係? Spinner的默認值是0(你只能得到(並設置)第一個適配器,或者無論你選擇什麼,名稱微調器中的選定項目都設置爲0)? – Luksprog

+0

在數據庫中我有兩個字符串。我用一個遊標項拉兩個然後我使用第一個字符串來設置第一個微調器,它使用一個字符串數組資源的位置。然後根據這個字符串,我將三個適配器中的任意一個設置到我的下一個微調器,然後使用第二個字符串在我的數組中找到此元素的位置,並嘗試將我的微調器設置爲適配器的該位置。但第二個微調保持默認爲數組的索引。 –

+0

這是嚴格用於更新數據庫中的現有項目。如果我添加項目,我的微調工作正常。這是當我去更新項目,我的第二個微調每次默認爲數組的0索引,但我從數據庫返回正確的字符串。由於某種原因,當我調用'spinNames.setSelection(n1);'它沒有設置位置。 –

回答

1

好了,所以我都沒能弄清楚我自己的答案,評論周後!我遇到的問題是,我不知道,我的OnItemSelectedListener()被稱爲每次我adapter設置爲我的第一次spinner

所以現在意識到這一點,我設置OnItemSelectedListener()第一spinner然後使用這個spinner id設置我的第二spinneradapter(即基於我把我的第二spinner至1 3的選擇adapterid。)

adapter設置完成之後我用從我cursor返回value找到這個adapter串並設置spinnerstringposition在我array

這一切都很好,現在我將把最終的代碼放在UPDATE的標題下。感謝所有幫助。