Reputation: 793
I am developing an android app in which I have 3 spinners populated from a database called Department,Practice and Address
I am loading them conditionally i.e, if it is department A load Practice A,B and based on practice A address is set.
Then I am saving these positions in an another database table by getSelectedItemPosition().
Now when I am trying them to 3 variables deptpos,practipos and addrpos. But when I use setSelection after initializing the spinners it is showing only correct position for department rest of them are not working :(
Here is my code
//Initializing the shared preferences variable
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
//Loading the database in writable format
db=mydbhelper.getWritableDatabase();
//For Medicines Table
try{
//For medicines
Cursor medspin = db.query("medicines", null,"_id="+prefs.getInt("pos",1),null,null,null,null);
medspin.moveToFirst();
//Filling the variables to set selemedspintion
deptpos=medspin.getInt(medspin.getColumnIndex("provider_department"));
practipos=medspin.getInt(medspin.getColumnIndex("provider_practice"));
addrpos=medspin.getInt(medspin.getColumnIndex("provider_address"));
hsname=medspin.getString(medspin.getColumnIndex("healthsystem"));
medspin.close();
Toast td=Toast.makeText(this,"Loaded dept is "+deptpos+"Loaded Provider is "+practipos+"Address is "+addrpos,Toast.LENGTH_LONG);
td.show();
}
catch(Exception e){
Log.e("Error","Error",e);
}
try{
//Cursor
Cursor depcur = db.rawQuery("SELECT _id,department,"+prefs.getString("hsname",null)+ " FROM dept_masterdata WHERE "+prefs.getString("hsname",null)+"=1",null);
depcur.moveToFirst();
startManagingCursor(depcur);
String[] from = new String[]{"department"};
int[] to = new int[] { android.R.id.text1 };
// Now creating an array adapter and set it to display using my row
//Adaptor for Department
SimpleCursorAdapter deptype =new SimpleCursorAdapter(this,android.R.layout.simple_spinner_item, depcur, from, to);
deptype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dep.setAdapter(deptype);
}
catch(Exception e){
Log.e("Error","Error",e);
}
//Now determine which department is selected
//Department
dep.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
deptid=dep.getSelectedItemId();
//Setting the practice spinner based on the department selected
Cursor pracur = db.rawQuery("SELECT _id,practice,dept_link,hs_id FROM practice_masterdata WHERE dept_link="+deptid+" AND hs_id="+(prefs.getInt("hsid",0)),null);
pracur.moveToFirst();
startManagingCursor(pracur);
String[] from1 = new String[]{"practice"};
int[] to1 = new int[]{android.R.id.text1};
SimpleCursorAdapter practype =new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_spinner_item, pracur, from1, to1);
practype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
pra.setAdapter(practype);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Practice
pra.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
practid=pra.getSelectedItemId();
//Setting the address based on the practice selected
Cursor addcur =db.rawQuery("SELECT * FROM address_masterdata WHERE practice_link="+practid,null);
addcur.moveToFirst();
startManagingCursor(addcur);
String[] from2 = new String[]{"address"};
int[] to2 = new int[]{android.R.id.text1};
SimpleCursorAdapter addtype =new SimpleCursorAdapter(view.getContext(),android.R.layout.simple_spinner_item,addcur, from2, to2);
addtype.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
add.setAdapter(addtype);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Address
add.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent,View view,int pos,long id){
addrid=add.getSelectedItemId();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
//Setting the spinners based on the above variables
dep.setSelection(deptpos);
pra.setSelection(practipos);
add.setSelection(addrpos,false);
}
I confirmed with a toast if the variables are loading correct values. They are but it is simply not setting them :(. The strange thing is department is showing the correct setting while both practice and address are not all :(. Please kindly point out where I am going wrong
thank you
Upvotes: 1
Views: 710
Reputation: 3619
Does practice
and address
have values already? If not then load data for practice and address out of depot selected listener, with by default d first as selected element.
depots[] = loadDepots();
depotsSpinner.setAdapter();
depotsSpinner.setSelected(0);
practice[] = loadPractice(depots[0]);
practiceSpinner.setAdapter();
practiceSpinner.setSelected(0);
address[] = loadAddress(depots[0], practice[0]);
addressSpinner.setAdapter();
addressSpinner.setSelected(0);
Upvotes: 2