Dynamiczne tworzenie spinnera i ustawianie jego wartości z SQLite
Znalazłem mój problem w sekcji „Aktualizacja” kodu
Dobra, mam problem z załadowaniem odpowiedniej wartości z Sqlite do mojego Spinnera. Oto jak moja aplikacja jest zbudowana
mój onCreate () ustawia spinner o nazwie spinTypes przez:
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);
}
Mam metodę tworzenia do wstawiania wszystkich moich danych do db, która działa dobrze, mój problem jest, gdy chcę spróbować zapełnić spinners do edycji wpisu.
Aplikacja działa tak: naciskasz przycisk dodawania, aby wyświetlić okno dialogowe, które tworzy element listy w widoku wywołującym. Do utworzenia używam dwóch spinnerów, jednego dla typu, który dynamicznie wypełnia spinner drugiego imienia na podstawie typu.
kiedy uzyskuję dostęp do obiektu bazy danych, zwracam ciągi znaków z spinnera, ale z jakiegoś powodu, gdy tworzę instrukcję if sprawdzając pozycję ciągu w tablicy, za każdym razem, gdy druga spinner ustawia pozycję 0.
metoda obejmująca instrukcję if, o której wspomniałem powyżej, jest następująca:
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
Błystki działają, ale nie kończą się na wartości z Nazwą wracam.
Czy ktoś może w tym pomóc?
** Zapisuję nazwę zwróconą z bazy danych i używam jej do znalezienia indeksu elementu w moim błędzie i zwracam poprawną wartość, ale pokrętło nadal ma wartość domyślną 0 **
Ktoś wie, jaki może być mój błąd? Proszę
Aktualizacja (ostateczny kod opublikowany poniżej) Wprowadziłem sporo zmian, aby to zadziałało, aby zrozumieć, co zrobiłem, powinieneś przeczytać cały post, żeby niczego nie przegapić!
wcześniej dodałem zmiennąonCreate()
private int spinNamePos;
dodałem tę linię po mojej ostatniej liniionCreate()
// add a listener to the spinner
spinTypes.setOnItemSelectedListener(TypesListener);
i stworzył tego słuchacza jako taki
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
}
};
potem zmieniłem mojeonPopulate()
pracować tak, nazywając nową metodę ustawiania pozycji drugiej przędzarki
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;
}
Z radością widzę tę pracę, zachęcając do zadawania pytań lub komentarzy