The items of the first one are defined in xml (<string-array>) but the second one should present different items arrays of strings according what is selected on the first...
The possible array of strings for the seconds are fetch from a web service using an AsyncTask (This part is working). In my onPostExecute(Void result) I have this:
private class GetInfoTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog = new ProgressDialog(StateTabActivity.this);
//...
#Override
protected void onPostExecute(Void result) {
Log.d("StateTabActivity","onPostExecute");
sectorsArray = getSectorsName(); // sectorsArray is an array of strings
roomsArray = getRoomsName(); // roomsArray is an array of strings
subcategorySpinnerAdapter = new ArrayAdapter<String>(StateTabActivity.this, R.layout.my_spinner,sectorsArray);
subcategorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
subCategorySpinner.setAdapter(subcategorySpinnerAdapter);
dialog.dismiss();
}
}
On the onCreate() of my activity I have:
Spinner categorySpinner = (Spinner) findViewById(R.id.statetab_category_spinner);
ArrayAdapter<String> categorySpinnerAdapter = new ArrayAdapter<String>(this, R.layout.my_spinner,getResources().getStringArray(R.array.array_category));
categorySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
categorySpinner.setAdapter(categorySpinnerAdapter);
subCategorySpinner = (Spinner) findViewById(R.id.statetab_subcategory_spinner);
categorySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Log.d("StateTabActivity","in onitemselected");
switch (arg2) {
case 0:
//I want to set here the items of sectorsArray to be displayed on the second spinner (subCategorySpinner)
break;
case 1:
//I want to set here the items of roomsArray to be displayed on the second spinner (subCategorySpinner)
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
So my question is: What should I do to bind the right array to the second spinner, according what is selected on the first one?
This is my code to get district list as per selected state.
final Spinner state = (Spinner)_activity.findViewById(R.id.state);
final Spinner district= (Spinner) _activity.findViewById(R.id.district);
_activity.findViewById(R.id.name_of_city);
state.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int i, long lng) {
if(i == 0){
districtAdapter =new ArrayAdapter<CharSequence>( _activity , android.R.layout.simple_spinner_item, **DistrictList**.AndraPradesh);
//DistricList is another class.its code given below
districtAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
district.setAdapter(districtAdapter);
if(_viewState == SAVED_VIEW){
district.setSelection(getArraySpinner(DistrictList.AndraPradesh,_initialValues.getAsString("District")),true);
}
}
In DistricList class,
public class DistrictList {
public static final String[] AndraPradesh = new String[] {"Adilabad",
"Anantapur",
"Chittoor",
"East Godavari",
"Guntur",
"Hyderabad",
"Karimnagar",
"Khammam"};
}
}
Related
I'm supposed to make a task , I have a spinner connected with a String array x , this array contains three values , so I want when clicking any choice of the spinner a specific list view will will give a specific three values and , this is my code :
public class Four extends ActionBarActivity {
String x [] = {"Jordan","Saudi Arabia", "Syria"};
String Jordan[] = {"Amman","Aqaba","Sarqa"};
String Saudi[] = {"Riyadh","Jeddah","Khobar"};
String Syria[] = {"Hems","Halab","Demashk"};
Spinner sp1 ;
ListView lv1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.four);
lv1 = (ListView)findViewById(R.id.listView1);
// Jordan List View
ArrayAdapter<String> jor = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Jordan);
lv1.setAdapter(jor);
// Saudi Arabia List View
ArrayAdapter<String> saud = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Saudi);
lv1.setAdapter(saud);
// Syria List View
ArrayAdapter<String> syr = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Syria);
lv1.setAdapter(syr);
sp1 = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> a = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item , x);
sp1.setAdapter(a);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
The thir parameter of onItemSelected() (int arg2 in your example, but I would suggest you rename them) is the position you selected in the Spinner. So you could implement that method like this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position == 0)
lv1.setAdapter(jor);
else if (position == 1)
lv1.setAdapter(saud);
else if (position == 1);
lv1.setAdapter(syr);
}
Remember that all the variables involved (lv1, jor, saud, syr) must be defined as final to be used inside the anonymous class, e.g.
final ArrayAdapter<String> jor = ...
final ArrayAdapter<String> saud = ...
got problem
Have 2 spinner on layout and wanna get dependence of this spinner
Structure
String[] main={"Moscov","LosAngeles","Pekin"};
String[] one={"1","2","3","4"};
String[] two={"2","2","5","8"};
String[] tree={"5","7","3","2"};
Create first adapter for first spinner with main array.
And dependently for item choice must crete adapter with array which depend of choice
If item 1 then adapter with array one, if item 2 then array two
You will create your spinner and just set the Adapter for the second spinner according to the selection using onItemSelected on the first Spinner. Something like
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
TextView tv = (TextView)view; // convert the view to TextView
String selected = tv.getText().toString();
if (selected.equals("Moscov"))
{
// set the Adapter here with the first set of values
}
else if(selected.equals("LosAngeles"))
{
// set the Adapter here with the second set of values
}
else
{
// set the Adapter here with the third set of values
}
}
You also could use the position which may be more scalable if you put your String Arrays into an ArrayList.
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long arg3)
{
switch(position)
{
case : 0;
// called first array
break;
case : 1;
// called second array
break;
}
}
Checkout this code snippet , in this values of spinner 2 is dependent on spinner 1
public class MainActivity extends Activity {
Spinner s1,s2;
Button btn;
String s;
TextView tv;
ArrayAdapter<String> adap1,adap2,adap3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
s1=(Spinner)findViewById(R.id.spinner1);
s2=(Spinner)findViewById(R.id.spinner2);
btn=(Button)findViewById(R.id.button1);
tv=(TextView)findViewById(R.id.tv1);
String[] v1=getResources().getStringArray(R.array.c1);
String[] v2=getResources().getStringArray(R.array.c2);
String[] v3=getResources().getStringArray(R.array.c3);
adap1=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v1);
adap2=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v2);
adap3=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, v3);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public class ItemSelectedListenerr implements OnItemSelectedListener {
String[] v=getResources().getStringArray(R.array.c1);
public String s;
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
s=arg0.getItemAtPosition(arg2).toString();
if(arg2==0)
{
s2.setAdapter(adap1);
}
if(arg2==1)
{
s2.setAdapter(adap2);
}
if(arg2==2)
{
s2.setAdapter(adap3);
}
tv.setText(s);
Log.i("hahaha", "item selected is"+s);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
}
I hope this will help you out. :)
Here is my code:
public class MainActivity extends Activity {
Spinner spin;
TextView tex;
String[] country = {"A", "Afghanistan", "Albania", "Etc"};// A to Z country names
String[] code = {"+93", "+91", "Etc"}; // A to Z country Code
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spin = (Spinner)findViewById(R.id.spinner1);
tex = (TextView)findViewById(R.id.tex);
ArrayAdapter aa1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, country);
aa1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setPrompt("Select the Country");
spin.setAdapter(aa1);
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
tex.setText(code[arg2]);
// tex.setText(country[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
I want to display the country's list in alphabetic order on spinner. And before that it should display the A, B, C up to Z. But This A to Z must be unselectable mode in spinner list. How can I achieve that?
You'll have to create your custom adapter that extends the ArrayAdapter.
it will probably be very easy, something like:
// the get view on your adapter
getView(LayoutInflater, etc, etc){
convertView = super.getView(inflater, etc, etc);
if(getItem(position).equals("A") || getItem(position).equals("B") || // etc, or create some clever way to go through a ArrayList with just the letters ){
convertView. // set not clickable stuff
}
}
but I reckon the Spinner still will close the list whenever the user clicks. Maybe you must override the spinner OnItemClick to get a coherent behaviour.
I am trying to get a screen transition when i select a value from the spinner. My spinner has just 2 values. The 1st one is selected by default. What I want is that when i click on the 2nd value in my spinner, it should take me to the new screen.
Please Help!
Thanks in advance!
Use onItemSelectedListener of your Spinner. Here is a Demo,
public class AndroidSpinner extends Activity implements OnItemSelectedListener {
TextView selection;
Spinner spin;
String[] items = { "bangladesh", "bangla", "bd", "australia", "japan",
"china", "indiaA", "indiaC" };
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spin = new Spinner(this);
setContentView(spin);
spin.setOnItemSelectedListener(this);
ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, items);
spin.setAdapter(aa);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
// Do your Stuff Here
Intent intent = new Intent(MyActivity.this, NextActivity.class);
startActivity(intent);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
selection.setText("");
}
}
Assuming that you know how to set up the spinner adapter and everything, simply add the code to go to the next screen when the other item is selected on the spinner.
public void onItemSelected(AdapterView<?> parent, View v, int position, long id)
{
if(position ==0)
//do nothing (assuming that you would want to stay in the same screen)
else if (position ==1)
{
//go to the next screen, probably by using an INTENT to the next activity or using setContentView(theLayoutXmlFile)
}
}
Use below code for open new activity or new screen onitemselected event of spinner.
public class SpinnerExample extends Activity implements OnItemSelectedListener {
String[] items = { "Dipak", "Aadi", "Bharat", "Pratik", "Usha", "Jayesh", "Deep", "Imran" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner mSpn1 = (Spinner) findViewById(R.id.mSpn1);
mSpn1.setOnItemSelectedListener(this);
ArrayAdapter<String> adpt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items);
mSpn1.setAdapter(adpt);
}
#Override
public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
// Do your Stuff Here
// For Open New Activity
Intent mInNewAccount = new Intent(MainActivity.this, SecondActivity.class);
startActivity(mInNewAccount);
finish();
// For Open New Screen
setContentView(R.layout.second_screen);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I am very new to android. I want to use 2 spinners in my application, one shows the countries list, when any country is selected the other spinner should show the list of cities of that country. when city is selected some action is performed. plz help me with some sample code. thanks in anticipation
Here is something what we can use to add options to spinner2 w.r.t to spinner 1.
public class Activity extends Activity implements View.OnClickListener
{
private Spinner spinner0, spinner1, spinner2, spinner3;
private Button submit, cancel;
private String country[], state[], city[], area[];
Australia aus = new Australia();
Object object;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner0 = (Spinner)findViewById(R.id.spinnerCountry);
spinner1 = (Spinner)findViewById(R.id.spinnerQ1);
spinner2 = (Spinner)findViewById(R.id.spinnerQ2);
spinner3 = (Spinner)findViewById(R.id.spinnerQ3);
submit = (Button)findViewById(R.id.btnSubmit);
cancel = (Button)findViewById(R.id.btnCancel);
submit.setOnClickListener(this);
cancel.setOnClickListener(this);
country = new String[] {"Select Country", "Australia", "USA", "UK", "New Zealand", "EU", "Europe", "China", "Hong Kong",
"India", "Malaysia", "Canada", "International", "Asia", "Africa"};
ArrayAdapter<String> adapter0 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, country);
adapter0.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner0.setAdapter(adapter0);
Log.i("AAA","spinner0");
spinner0.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id)
{
Log.i("AAA","OnItemSelected");
int loc;
loc = pos;
switch (loc)
{
case 1:
state = aus.getState();
object = aus;
Log.i("AAA","ArrayAdapter1");
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, state);
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter1); Log.i("AAA","spinner1");
break;
default:
Log.i("AAA","default 0");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg1)
{
Log.i("AAA","Nothing S0");
}
});
spinner1.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View view1, int pos, long id)
{
Log.i("AAA","OnItemSelected S1");
int loc = pos;
switch(loc)
{
case 1:
Log.i("AAA","Australia");
if(object.equals(aus))
{
city = aus.getType(loc);
}
else
{
break;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, city);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner2.setAdapter(adapter); Log.i("AAA","spinner2");
break;
default:
Log.i("AAA", "default 1");
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Log.i("AAA","Nothing S1");
}
});
spinner2.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int pos, long id)
{
int loc = pos;
switch (loc)
{
case 1:
if(object.equals(aus))
{
area = aus.getTitle(loc);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(Activity.this, android.R.layout.simple_spinner_item, area);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner3.setAdapter(adapter); Log.i("","spinner3");
break;
default:
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
// TODO Auto-generated method stub
}
});
}// on-create
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnSubmit:
break;
case R.id.btnCancel:
finish();
break;
default:
break;
}
}
}
If you find this useful, then do give it up vote, so that others can find a good answer easily.
For each Country, you have to create a class for it, to just add State, City & Area. So that it doesn't become a mesh at a single at single page.
Have fun.
Regards,
Haps.
Here is a sample code which depicts the usage of spinner and action performed when spinner item is selected
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener;
public class MainActivity extends Activity {
Spinner spin;
String spin_val;
String[] gender = { "Male", "Female" };//array of strings used to populate the spinner
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//setting layout
spin = (Spinner) findViewById(R.id.spinner_id);//fetching view's id
//Register a callback to be invoked when an item in this AdapterView has been selected
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
spin_val = gender[position];//saving the value selected
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
//setting array adaptors to spinners
//ArrayAdapter is a BaseAdapter that is backed by an array of arbitrary objects
ArrayAdapter<String> spin_adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item, gender);
// setting adapteers to spinners
spin.setAdapter(spin_adapter);
}
}
To add a list of values to the spinner, you then need to specify a SpinnerAdapter in your Activity, which extends Adapter class.. A spinner adapter allows to define two different views: one that shows the data in the spinner itself and one that shows the data in the drop down list when the spinner is pressed.You can also download & refer to the complete spinner_demo example project for better understanding.
Check the following examples :
https://developer.android.com/guide/tutorials/views/hello-spinner.html
http://www.designerandroid.com/?cat=4