How to make a spinner populate another spinner? - java

How would you go about making a spinner populate another spinner based on the first spinners selection?
So for example:
Spinner1 items are vegetarian or meat eater.
<string-array name="spinnerarray_veg_meat">
<item >Vegetarian</item>
<item >Meat eater</item>
</string-array>
Spinner2 would then need to display either vegetarian meal names or meat eater ones depending on spinner1's selection.

To do this you'll have to set a OnItemSelectedListener on your first Spinner to populate the second Spinner programmatically.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
if(position == 0) {
// Populate the Spinner2 with different values
} else {
// Populate the Spinner2 with different values
}
}
public void onNothingSelected(AdapterView<?> parent) {
return;
}
});

There are a number of ways to do it. One being, create an Array of meat items and one of vegetable items. In onItemSelected() of spinner1 set the adapter for spinner2 according to the position
Spinner Docs
This link has many useful functions and properties available to Spinners

Related

Android, spinner item disappeared after clicking on other spinner

Scenario: Let's say there are 2 spinner in the fragment called spinner a and b:
Spinner a contain: dog cat elephant
Spinner b contain: micky, minny, moe
My question is: Is it possible that when user using apps, and clicked Spinner a, and choose elephant, the micky and minny option in Spinner b option be gone and only leaving moe as an option?
And what is the best way to do it? (not using any database)
xml:
<Spinner
android:id="#+id/spinner_a"
android:entries="#array/value_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<Spinner
android:id="#+id/spinner_b"
android:entries="#array/value_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
string value:
<resources>
<string-array name="value_a">
<item>dog</item>
<item>cat</item>
<item>Elephant</item>
</string-array>
<string-array name="value_b">
<item>micky</item>
<item>minny</item>
<item>moe</item>
</string-array>
java:
package...
import....
public class testing123 extends Fragment{
final Spinner a123 = (Spinner) getView().findViewById(R.id.spinner_a);
final Spinner b123 = (Spinner) getView().findViewById(R.id.spinner_b);
final String ax = a123.getSelectedItem().toString();
final String bx = b123.getSelectedItem().toString();
if (ax.equals("elephant")) {
//what is the best way to do it?
}else{}
}
Yes you can but not with your way.
For spinner a it's ok, but for b you should manage it dinamically from Java code.
Add to spinner A a listener and modify string-array of spinner b based on item a selected.
Listener:
Spinner spinner_a = (Spinner) findViewById(R.id.spinner_a);
spinner_a.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// retrieve selected item
String spinner_a_choice = parent.getItemAtPosition(position).toString();
// elaborate and set here your spinner b with its own adapter
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// Something else
}
});
How to add an adapter to spinner (you have to manipulate it):
Spinner spinner_b = (Spinner) findViewById(R.id.spinner_b);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.value_b, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_b.setAdapter(adapter);

How to get the value of a TextView that is part of a ListView row in all checked items

As illustrated in the photo, i have a list view that is made up of a custom layout which has two TextView. One TextView is for storing numbers which has a visibility of gone, the other is for storing the name, which is visible.
all i want is to be able to get a string of all numbers that are selected when the send button is clicked.
A good suggestion here is to use a recyclerview instead of list view.
To achieve want you want with a list view you just set an item click listener to your list view in your activity. The item click listener will pass the View which is the current cell in the list view. Then you can find textview by id to locate. The Textview ID is set in the layout xml.
Example
listView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View cell,int pos, long arg1)
{
TextView textView = (TextView)cell.findViewByID(R.id.myTextView);
String texViewContents = textView.getText().toString();
}
});

Three spinners in one Activity

I have three spinners in one Activity , so I want to choose from one spinner then getting some specific values to the second spinner and after choose one of the second then getting some specific values to third spinner .
i have tired with using loops and switch case but i think it takes much time and long code .
is there is simple way to make this three spinner dependent to each other ?
Disable spinner 2 in your layout, and then enable and populate it when spinner 1 item is selected. Then do the same for spinner 3 based on spinner 2.
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// Enable Spinner 2
// Set spinner 2 adapter
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// Disable spinner 2
// Set spinner 2 adapter to empty list
}
});

Adding custom data to ListView & ArrayAdapter items

I'm creating an Android application. Inside a Fragment I have a ListView that is populated using an ArrayAdapter and an ArrayList. I'm using android.R.layout.simple_list_item_1 for the layout for the list items. I want to have an OnItemClickListener, so that when an item is clicked it will show another Activity based on its data.
The problem is, there may be items with the same name. I'd like to attach an ID value to each of the elements, so that my code can distinguish them from each other.
My items that I use to populate the list are of a custom class to hold their data, but the important fields here are the ID and the name (which is shown in the ListView).
My code for populating the ListView:
List<String> items;
ArrayAdapter<String> adapter;
List<MyCustomDataObject> listOfDataObjects;
...
// Get the ListView
ListView list = (ListView) layoutRootView.findViewById(R.id.listView1);
// Create the item List and the ArrayAdapter for it
items = new ArrayList<String>();
adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, items);
// Set the list adapter
list.setAdapter(adapter);
// Add the data items
for (MyCustomDataObject obj : listOfDataObjects) {
items.add(obj.name);
}
items.add(getResources().getString(R.string.no_items));
// Create the item click listener
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Open the Activity based on the item
}
});
How could I add an ID to the list items for identifying each item?
The solution is quite simple actually.
You're populating the ListView from a List. The List is an ordered collection of items, so when adding it as the datasource for the ListView you will always know the index of each item.
So when selecting an item from the ListView you get the position of the View clicked. This position will correspond to the position in your List.
You won't really need the id field of your MyCustomDataObject, but of course when you populate the List of MyCustomDataObject you could use a normal for-loop (not enhanced) and use the index to set the id of each MyCustomDataObject.
Lookup the position in listOfDataObjects to find the ID:
// Create the item click listener
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position==listOfDataObjects.size()) { .... no_items clicked ... }
else {
MyCustomDataObject obj = listOfDataObjects.get(position);
... // Open the Activity based on the item
}
}
});

Enable a currently disabled Spinner in Android

I was fooling around with Android and my Java knowledge is limited at best (for instance, I'm perplexed by the fact that it allows inline classes!?).
My question is as follows:
I have a layout where there are three dropdown menus. I initialise all three of them inside onCreate().
The first one takes its values from a string-array. The second one, however, depends on the choice of the first one and the third one depends on the choice of the second one in turn!
I have a few string-arrays for the second Spinner but I was wondering what would be the correct way to implement a list of successively enabled Spinners. I'm tempted to just hack into it and make it work but I don't want to run the risk of it being malformed and unstable.
So for example's sakes, my case is as if I had the Google Spinner tutorial with an extra Spinner for the moons of each planet (and then maybe for craters on each of the moons).
In my resources, I have an arrays.xml such as:
<resources>
<string-array name="planets">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
<item>Jupiter</item>
<item>Saturn</item>
<item>Uranus</item>
<item>Neptune</item>
</string-array>
<string-array name="earth">
<item>The Moon</item>
</string-array>
<string-array name="mars">
<item>Deimos</item>
<item>Phobos</item>
</string-array>
<string-array name="jupiter">
<item>Metis</item>
<item>Adrasthea</item>
<item>Amalthea</item>
<item>Thebe</item>
<item>Io</item>
<item>Europa</item>
<item>Ganymede</item>
<item>Callisto</item>
[...] etc. etc.
</resources>
In the onCreate method I initialise all three:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.planetSpinner);
Spinner spinner2 = (Spinner) findViewById(R.id.moonSpinner);
Spinner spinner3 = (Spinner) findViewById(R.id.craterSpinner);
spinner2.setEnabled(false);
spinner3.setEnabled(false);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(
this, R.array.planets, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnPlanetSelectedListener());
}
My listener class is
public class MyOnPlanetSelectedListener 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 there it is. My question is where and how to implement the listeners for the successive Spinners so that they're enabled once the first one is selected. My first guess is:
public class MyOnPlanetSelectedListener 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();
spinner2.setEnabled(true);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
But obviously this is wrong since spinner2 is not encapsulated here.
Then, the successive spinners should pick a string-array for their adapters depending on what the choice of the Spinners before it are. I tried encapsulating the spinners in the activity class, in the constructor, but the application crashes before running.
Thank you very much in advance for your help.
Use the view parameter of onItemSelected to decide which Spinner sent the onItemSelected event. From this doing the logic is simple.
Make spinner1 2 and 3 member of the class (activity)
private Spinner spinner1;
Then simply compare the view param against
if (view==this.spinner1) {
// event came from spinner 1
// create a new adapter, assign the new adapter to spinner 2
} else if (view==this.spinner2) {
// event came from spinner 2
}
Try to use this :
switch(parent.getId())
{
case R.id.spinner1:
Log.d(TAG,"spinner1");
break;
case R.id.spinner2:
Log.d(TAG,"spinner2");
break;
}
good luck !

Categories