1

我有checkBoxSpinner。如果checkbox选中,spinner则应调用数据库并从那里获取值,否则为默认值。

我的问题是: 如果选中 CheckBox,则应该调用 Spinner。如何实施?

我的代码是:

private HashMap<Integer,ReturnProduct> retrunTypes  =new HashMap<Integer, ReturnProduct>();

checkBox1=(CheckBox)findViewById(R.id.checkBox1);
  checkBox1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(isChecked) {
            rtnStatus =true;
            retrunTypes = getReturnRason(); 
        }else {
            rtnStatus =false;
        }

    }
   });

微调器:

     retrunTypes = getReturnRason();
    ArrayList<String> returnTypeList = new ArrayList<String>();
    for (Map.Entry<Integer, ReturnProduct> entry : retrunTypes.entrySet()) {
            ReturnProduct myProduct = entry.getValue();
            returnTypeList.add(myProduct.getDescription());
    }

    retuReason = (Spinner) findViewById(R.id.retuReason);
    reTypeAdapter = new ArrayAdapter<String>(SalesActivityGroup.group.getApplicationContext(),android.R.layout.simple_spinner_item, returnTypeList);
    reTypeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    retuReason.setAdapter(reTypeAdapter);
    retuReason.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view,int arg2, long arg3) {
            //selectedReType = parent.getSelectedItem().toString();
            selectedReTypeId= arg2;
            selectedReType = retrunTypes.get(selectedReTypeId).getReturnReason();

            //ReturnProduct rProduct = findReturnType(selectedReType);
            processingRequird = retrunTypes.get(selectedReTypeId).getProcessingRequired();
            selectedReTypeCode = selectedReType;
            selectedRetCategory =  retrunTypes.get(selectedReTypeId).getReturnCategory();
        }
        public void onNothingSelected(AdapterView<?> arg0) {
        }
    });


 private HashMap<Integer,ReturnProduct> getReturnRason(){
    DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(this);
    HashMap<Integer,ReturnProduct> returnType = new HashMap<Integer, ReturnProduct>();
    try {
        dbAdapter.openDataBase();
        String  query ="";
        if(rtnStatus) {
            query = "SELECT rs.ReturnReasonCode,rs.ReturnType,rs.Description,rt.ProcessingRequired,rt.ReturnCategory " +
                    " FROM WMReturnReason rs,WMReturnType rt" +
                    " WHERE rs.ReturnType =rt.ReturnType AND rs.BusinessUnit=? AND Status ='1' AND  rt.ProcessingRequired ='1' ";
        }else {
            query = "SELECT rs.ReturnReasonCode,rs.ReturnType,rs.Description,rt.ProcessingRequired,rt.ReturnCategory " +
                    " FROM WMReturnReason rs,WMReturnType rt" +
                    " WHERE rs.ReturnType =rt.ReturnType AND rs.BusinessUnit=? AND Status ='1' ";
        }

        String[] d = new String[]{strBusinessUnit};
        ArrayList<?> stringList = dbAdapter.selectRecordsFromDBList(query, d);

        dbAdapter.close();
        //System.out.println("===getReturnType=="+stringList.size());
        if(stringList.size() > 0){
            for (int i = 0; i < stringList.size(); i++) {
                ArrayList<?> arrayList = (ArrayList<?>) stringList.get(i);
                ArrayList<?> list = arrayList;
                ReturnProduct returnProduct = new ReturnProduct();
                returnProduct.setReturnReason((String) list.get(0));
                returnProduct.setReturnType((String) list.get(1));
                returnProduct.setDescription((String) list.get(2));

                returnProduct.setProcessingRequired((String) list.get(3));
                returnProduct.setReturnCategory((String) list.get(4));

                returnType.put(i, returnProduct);
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return returnType;
 }
4

1 回答 1

1

可以spinner.setSelection(pos)在里面使用setOnCheckedChangeListener。我可以看到您已经推出了自己的方法来从数据库中获取值。现在根据返回的值来选择你的微调器。

于 2012-06-18T09:19:14.233 回答