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) {
}
});
Related
I want to create an application with two Spinners. When you select one of the items inside the first spinner, the second spinner will get only a few items (depends on which item selected).
For example: in the first spinner I select "Mazda", and then on the second I will be able to see only Mazda's models, not BMW, Ford etc. Can I do something like this?
I tried to create a spinner without items, and set the entries of the spinner on the XML when item selected, but there is no method to do this.
I don't create Lists. I want to create string-array resources in my strings.xml, and give that array to the second spinner.
Of course it is doable and pretty straightforward. Spinners works with the model provider, which in Android it is called Adapter pattern. So what you can do is put in your first spinner an adapter that holds all the brands of your cars, and listen on the first spinner for changes using setOnItemSelectedListener.
When the item changes, you can create a new adapter instance for the second spinner with the only values that are valid in this case.
Try following code. I've organized the sample data in a HashMap but you can do it in your own way.
// hashmap object containing data of spinner1 as 'keys' with relevant
// data of spinner2 in List<String> object as 'values'
final Map<String, List<String>> data = new HashMap<>();
data.put("A", Arrays.asList("1","2","3","4"));
data.put("B", Arrays.asList("4", "5"));
data.put("C", Arrays.asList("6", "7", "8", "9"));
data.put("D", Arrays.asList("10", "11", "12"));
data.put("E", Arrays.asList("13", "14"));
// obtaining a string array containing keys(data of spinner1) of above hashmap
final String[] dataSpinner1 = new String[data.keySet().size()];
data.keySet().toArray(dataSpinner1);
// initializing an string type, ArrayAdapter for spinner1
// you will need to pass activity context, layout for the spinner item and
// spinner content(as string array) as arguments to create an array adapter
final ArrayAdapter<String> spinner1Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner1);
spinner1.setAdapter(spinner1Adapter);
// setting listner for spinner1 to trigger when an spinner item is being
// clicked by the user
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// obtaining relevant data for spinner2
List<String> dataSpinner2 = data.get(dataSpinner1[position]);
// crating an setting array adapter for spinner2
ArrayAdapter<String> spinner2Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner2);
spinner2.setAdapter(spinner2Adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I have a spinner:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int childposition, long id) {
textView.setText(spinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
textView.setText("");
}
});
Above you'll see that textView - is my text object. I'm displaying a text item spinner in the textView when I click it. If I dont click the spinner then my textView must be textView.setText("");
But the spinner is always set text in my textView, even if I do not choose spinner.
Question
How can I accomplish this?:
If I dont choose item spinner, textView is empty: textView.setText("");
If I do choose the item spinner, textView gets: textView.setText(spinner.getSelectedItem().toString());
String item = parent.getItemAtPosition(childposition).toString(); //Get selected item
if(item.equals("spinner")){ // Check if it equals spinner
textView.setText(item); // Set text to item
}else{
textView.setText(""); // If it doesn't equal spinner set text to ""
}
If I understood the question right, putting this instead of textView.setText(spinner.getSelectedItem().toString()); and deleting content of onNothingSelected should do the trick.
UPDATE
I finally understood what you mean. To do this create your spinner like this and add "" as first choice in your string-array resource :
String[] newArray = getResources().getStringArray(R.array.yourArray);
List<String> myResArrayList = Arrays.asList(newArray);
ArrayList<String> spinnerItems = new ArrayList<String>(myResArrayList);
//Making adapter with ArrayList instead of String[] allows us to add/remove items later in the code
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, spinnerItems);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinnerItems.remove(0);
adapter.notifyDataSetChanged(); // Here we remove the first choice which is "" so the user won't be able to select empty.
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 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());