Android - When we select checkbox , Spinner need to change

I have checkBox and Spinner. If the checkbox is checked, then spinner should call the database and get the values from there, otherwise default value.

My Problem is : If CheckBox is checked, Spinner should be called. How to implement this?

My code is :

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

    }
   });

Spinner:

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

Answers


You can use spinner.setSelection(pos) inside the setOnCheckedChangeListener. I can see that you have rolled out your own method to get the value from the database. Now based on the value returned just make the selection of your spinner.


Need Your Help

What is the command to close a single tab in Sublime?

tabs sublimetext

Cmd-shift-w closes the whole window. I want to just close out a single tab. How do I do that?

How scalable is Jetty?

java c jetty libevent

Greetings! I wrote a highly scalable HTTP event (long-polling) server in C/C++ using libevent. However, it's messy, hardly portable, and lets face it: it's C. Let alone that I've been having some m...