无法根据从数据库检索的值设置微调器位置

时间:2011-08-22 15:26:49

标签: java android sqlite android-layout android-emulator

我正在开发一个Android应用程序,其中我从一个名为的数据库中填充了3个微调器 部门,实践和地址

我有条件地加载它们,即如果它是部门A加载练习A,B并且基于练习A地址被设置。

然后我通过getSelectedItemPosition()将这些位置保存在另一个数据库表中。

现在当我尝试使用3个变量deptpos,practipos和addrpos时。 但是当我在初始化微调器后使用setSelection时,它只显示正确的位置,因为其余部分不起作用:(

这是我的代码

//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);

}

如果变量加载了正确的值,我用吐司确认了。它们只是没有设置它们:(。奇怪的是部门显示正确的设置,而练习和地址都不是全部:(。请指出我出错的地方

谢谢

1 个答案:

答案 0 :(得分:2)

practiceaddress是否已有值?如果没有,则加载数据以进行练习并从库选定的侦听器中解决,默认情况下首先将d作为所选元素。

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);