I need to work with 3 Spinners, meaning that if one item from the Spinner number 1 is selected, then the spinner number 2 will become visible, and if another item from the Spinner number 1 is selected, then the Spinner number 3 will become visible, and the number 2 invisible, in order to do this, do i need to work with 3 ArrayAdapter, one per each?? or can i work with one ArrayAdapter? Any help will be very appreciated
ArrayAdapter means 'An adapter for an array of elements`. Not 'An array of adapters'. If you want to work with 3 spinners, you will have to use 3 adapters.
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner1); // invisible by default
final Spinner spinner3 = (Spinner) findViewById(R.id.spinner1); // invisible by default
spinner1.setAdapter(adapter1);
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter3);
spinner1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position == 0) {
spinner2.setVisibility(View.VISIBLE);
} else if(position == 1) {
spinner3.setVisibility(View.VISIBLE);
}
}
}); //like this for all the three
An Adapter object acts as a bridge between an AdapterView and the underlying data for that view.
If you want to show same data to all of the three Spinners, Then you can use one adapter for the spinners else you need to create separate adapter for each.
Related
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.
A little context: I have a stable tutorials app on the market. Recently the users have been requesting some external sources where they can learn more. This question is regarding the solution I came up with.
I have an activity with a listview and a spinner on top.
The listview can be populated with up to 25 elements at once (should I make it lesser?) .... I want the spinner to have options like (1-25, 26-50, 51-75).
The default can be set to 1-25. Depending on what the user selects, I need to re-populate the listview accordingly. I reckon a two dimensional array would be the neatest. But I don't want to hinder the performance too.
Anyways, here's what I've done yet. I need to know how exactly should I update the listview elements in the Spinners OnClick.
Spinner spinner;
String path[] = {"Laptops","DesktopPC","Tablets","Add-Ons","Gaming"};
String Laptops[] = {"Dell","Hp","Apple"};
ListView lstView;
lstView = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (Category.this, android.R.layout.simple_spinner_item, path );
spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setAdapter(adapter);
spinner.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
ArrayAdapter<String> lstAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, Laptops);
lstView.setAdapter(lstAdapter);
//lstView.refreshDrawableState();
}
});
Assume that I have to switch between the two arrays depending on the spinner selection.
Thank you for reading! If you require any more information, kindly ask :-)
In my application I have 1 spinner option. In that I am displaying 6 values (which is array from values file). Now in that when I click its 5 items of the spinner value the selected item will show in the spinner in the activity screen.
When I select 6th item in spinner I have to start a new activity. Is it possible? Do I have to do this by position value?
Also in the new activity (after selecting 6th item) I have 5 edittext values in which user can enter a string which should replace the old value in the spinner. Is it possible to dynamically update the spinner?
My spinner code:
Spinner s2=(Spinner)findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.group_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter1);
It's possible
Codings:
ArrayAdapter yearadp = new ArrayAdapter(this,android.R.layout.simple_spinner_item, yearlist);
yearadp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
year_spinner.setAdapter(yearadp);
index=0;
year_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
spintext=parent.getItemAtPosition(pos).toString();
if(spintext=="6")
{
intent = new Intent(ListMonthActivity.this,NewChart.class);
startActivity(intent);
}
}
}
Ya we can do with setting with
mon_spinner.setSelection(iSelectedMonth);
intent = new Intent(ListMonthActivity.this,NewChart.class);
You may or not mention while the action are certain action finished, and you can passed into another activity .
I have four Spinner ,First spinner display data when user select on spinner item data then other spinner data is displayed .First time,i have loaded all data in spinner when user select first spinner then data should be changed to refresh to second Spinner
How data is changed in second spinner
First you would need to get a reference of your spinner like -
Spinner mySpinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.myArray, android.R.layout.mySpinnerItem);
adapter.setDropDownViewResource(android.R.layout.myDropdownItem);
spinner.setAdapter(adapter);
To change the values you would do -
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
//change content
}
}
public void onNothingSelected(AdapterView parent) {
//do nothing
}
}
Set a listener on the setOnItemSelectedListener of your first Spinner, with the appropriate code to populate your second Spinner. This way when you change the value of the first Spinner, the second Spinner will be updated.
I have a question that, I want to load items in a spinner on ItemClick of another Spinner. Actually I two spinners, data is loaded into first spinner from json_parsing and I have to load data in second Spinner after selecting an item from first spinner, So, I don't know how it will implemented? Please suggest me the right solution.
Thanks in advance.
You can do it like this,
First time you data will be loaded in First and Second Spinner.
On Selection of item from First Spinner do this.
1.) Clear the previous ArrayList or Array whateven you have passed the
Second Spinner.
2.) Fill the ArrayList or Array of new data & Update the Second Spinnner using
adapter.notifyDataSetChanged();
second_spinner.setSelection(0);
First Set an OnItemClickListner for your First Spinner. In the OnItemClickListner Method first parse your XML. After completing XML parsing, set parsed data to the adapter and set that adapter with your second spinner
Set an OnItemClickListener on your first spinner that would prepare and set the adapter to the second spinner.
Here is a more complete code example:
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Cursor c1 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c1, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner1.setAdapter(adapter1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Cursor c2 = (some code for getting a cursor from an data source, for example, a sqlite database)
SimpleCursorAdapter adapter2 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, c2, new String[]{"column_name"}, new int[]{android.R.id.text1});
spinner2.setAdapter(adapter2);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Cursor c_new = (create a new cursor);
adapter2.changeCursor(c_new);
adapter2.notifyDataSetChanged(); // this is important for notifying the UI
spinner2.setAdapter(adapter2);
}
});
What you do is set a listener to the first Spinner, there change the Cursor of the second Adapter to a new one, notify the UI and reset the Adapter of the second Spinner.