I was using a spinner in a TableRow and it was working perfectly well except that I didn't like spinner icon is stretching out according to the selected item. I tried to delete Spinner section in xml and create it on my code.
To create a spinner in OnCreate():
selectArea = /*(Spinner)this.findViewById(R.id.spinner);*/new Spinner(this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());
selectArea.setSelection(prefInt);
To handle a selection event:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Log.d(TAG, "onItemSelected() " + id);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
It worked almost perfect. TableRow show no spinner icon, on touching TableRow, it pops up items to be selected. My only problem is that, on selecting one tiem, I never get my onItemSelected() called..
What could be wrong?
please change this line
spinner.setAdapter(adapter);
into the line
selectedArea.setAdapter(adapter);
see i have created spinner here
Spinner selectArea= new Spinner(Activity.this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setAdapter(adapter);
linearLayout1.addView(selectArea);//to add your spinner
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());
Related
I want to make an app which contains a Spinner and a ListView and when I click a Sinner item it will open a new ListView, no new Activity just to update the ListView from the database.
For instance, I have Europe and Africa in the Spinner and when I click Europe it will show me a ListView with the European countries, when I click Africa it will show me a ListView with the African countries.
I searched this on the internet, but I could not find anything.
First of all, to set up a spinner you need an adapter. Then you need to use an OnItemSelectedListener. Use this code:
europeListView = (ListView) findViewById(R.id.europeListView);
africaListView = (ListView) findViewById(R.id.africaListView);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(adapter.getItem(i) == "Europe"){
europeListView.setVisibility(View.VISIBLE);
africaListView.setVisibility(View.INVISIBLE);
} else if(adapter.getItem(i) == "Africa"){
africaListView.setVisibility(View.VISIBLE);
europeListView.setVisibility(View.INVISIBLE);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Finally, make a string array called "countries" containing Europe and Africa in your strings.xml file like so:
<string-array name="countries">
<item>Europe</item>
<item>Africa</item>
</string-array>
Hope this helps!
I have one ListView and one Spinner. The Spinner is situated at the right top of the layout. My ListView contains some names (eg. country names) and the spinner has some alphabets (a-z). Suppose if I choose the letter "f" from the Spinner, my ListView must show the country names that starts only with the letter "f" . I want to sort my ListView by values from the Spinner?
You need to implement a Filter for your ListView. Everytime the OnItemSelectedListener of your Spinner gets called you need to filter the items. If you're not sure how to implement a filter in your ListView's adapter have a look at this: Filtering ListView with custom (object) adapter
I guess there won't be any way around implementing an own ListAdapter (it's easy).
Try to use the getFilter() method of the ListView adapter:
String[] filterL = { "a", "b", "c" }; //etc
//...
Spinner spin = (Spinner) findViewById(R.id.spinner1);
final ArrayAdapter<String> aspin = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, filterL); //the adapter for the Spinner
aspin.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(aspin);
final ArrayAdapter<String> aa = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, countries); //the adapter for the list
setListAdapter(aa); //set the adapter for the list(if you extend LisActivity) or call setAdapter on the ListView element
//add the listener:
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
boolean status = false;
public void onItemSelected(AdapterView<?> parent, View view,
int position, long arg3) {
if (!status) {
status = true;
return;
}
aa.getFilter().filter(filterL[position]);
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I have to develop the application based on a spinner.
I have taken the spinner1 as the product choice and I have to give the spinner2 the data, which is in that particular product category so I have to update spinner2 on the base choice of spinner1.
I have search a lot but I can get any sure example code or resource for it.
I agree with #user639183, there are lot's of similar questions... however, there some explanation on how you do it:
Create member variables for the arrays containing your options to display.
Populate your first spinner with your category values.
Hook on to the Spinner's OnItemSelected event by using spinner1.setOnItemSelectedListener(...)
In the event listener, populate your second Spinner with the corresponding values.
Example for step 1:
private String[] spinner1values = new String[] { "cat1", "cat2" };
private String[][] spinner2values = new String[][] {
new String[] { "a1", "b1", "c1" },
new String[] { "a2", "b2" }
};
Population of spinner1 as follows:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinner1values);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
This has so far been absolutely straight forward, if you read the documentation and examples for Spinners!
Next, hook on to the OnItemSelectedListener:
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// create a new adapter with the corresponding values
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, spinner2values[position]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// set adapter
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// nothing selected, so set empty options
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(arg0.getContext(),
android.R.layout.simple_spinner_item, new String[0]);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
((Spinner)findViewById(R.id.spinner2)).setAdapter(adapter2);
}
});
Pay attention, that the order of the arrays in spinner2values is corresponding to the order of the category values!
i have 2 spinners :
FIRST SPINNER :
array_spinner=new String[3];
array_spinner[0]="Color";
array_spinner[1]="Model";
array_spinner[2]="Price";
Spinner s = (Spinner) findViewById(R.id.select);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,
array_spinner);
s.setAdapter(adapter);
SECOND SPINNER :
array_color=new String[3];
array_color[0]="Black";
array_color[1]="Pink";
array_color[2]="Green";
array_model=new String[3];
array_model[0]="Ipod";
array_model[1]="mp3";
array_price=new String[3];
array_price[0]="1000-2000";
array_price[1]="3000-4000";
Spinner val=(Spinner)findViewById(R.id.select_val);
ArrayAdapter adapter1 = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_color);
val.setAdapter(adapter1);
When the user clicks Model in the First Spinner , immediately the Second Spinner should display the models which are in the array_model ... Please Help !!!!
use setOnItemSelectedListener for the first spinner. In the onItemSelected() set a new arrayAdapter for the second spinner with the new data set
I think you must put a listener on the spinner.
Then You create the good second spinner.
example :
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
if(array_spinner[int]=="model"){
//Do your second spinner
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
I am trying to connect 2 spinners together. Meaning, the items inside 2nd spinner will depend on whatever item is chosen for the 1st spinner.
This is the code inside the main java file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
This is the code inside MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So the onItemSelected function will print out the item that was chosen in 1st spinner on the screen. However, I can't figure out how to create the 2nd spinner fully based on the value inside 1st spinner.
I know that there should be something needed to be done inside onItemSelected, but I just can't figure it out since I am a newbie in Java Android.
Can you guys assist me on this?
Thank you.
You should just do something very similar to what you do in onCreate, only with the other spinner. For example:
List<String> values = findValuesBySelection(parent.getItemAtPosition(pos).toString());
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, values, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);