String[] categories = {"All", "Surgeon", "Pediatrician", "Cardiologist", "Dermatologist"}; String[][] dca={ {"Dr Chandiram","Dr Hamza","Dr Pervaiz","Dr Naeem","Dr Rajesh","Dr Sami"}, {"Dr Chandiram","Dr Hamza",}, {"Dr Pervaiz"},{"Dr Naeem","Dr Rajesh"}, {"Dr Sami"} }; ArrayAdapter aa = new ArrayAdapter(this,android.R.layout.simple_spinner_item,categories); aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); drspin.setAdapter(aa); drspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String a = drspin.getSelectedItem().toString(); if(a.equals("All")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[0]); drcat.setAdapter(myad); }else if(a.equals("Surgeon")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[1]); drcat.setAdapter(myad); }else if(a.equals("Pediatrician")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[2]); drcat.setAdapter(myad); } else if(a.equals("Cardiologist")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[3]); drcat.setAdapter(myad); } else if(a.equals("Dermatologist")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[4]); drcat.setAdapter(myad); } // sd.setText(a); } @Override public void onNothingSelected(AdapterView<?> parent) { } }); drcat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { sd.setText(drcat.getSelectedItem().toString()); } @Override public void onNothingSelected(AdapterView<?> parent) { } });
How can we keep the edit box by default empty, which displays the result of the spinner, when we click on any item of spinner then only it must show the result in the edit box else it must remain empty
Advertisement
Answer
One of the solution in your case can be as follows:
String[] categories = {"Select","All", "Surgeon", "Pediatrician", "Cardiologist", "Dermatologist"}; drspin.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String a = drspin.getSelectedItem().toString(); if(a.equals("Select")){ editText.setText(""); } else if(a.equals("All")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[0]); drcat.setAdapter(myad); }else if(a.equals("Surgeon")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[1]); drcat.setAdapter(myad); }else if(a.equals("Pediatrician")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[2]); drcat.setAdapter(myad); } else if(a.equals("Cardiologist")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[3]); drcat.setAdapter(myad); } else if(a.equals("Dermatologist")){ ArrayAdapter myad = new ArrayAdapter(getApplicationContext(),R.layout.support_simple_spinner_dropdown_item,dca[4]); drcat.setAdapter(myad); } }