update spinner array depend on other's selection - android

Spinner spinner, spinner2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_check_box_tuts);
spinner = (Spinner)findViewById(R.id.spinner1);
spinner2 = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> v, View arg1, int arg2,
long arg3) {
switch(v.getId()){
case R.id.spinner1:
if(spinner.getSelectedItemPosition() == 1){
//what goes here??
break;
}
}
}
Like my first spinner lists are countries : USA, JAPAN etc.. so after USA has been selected for example, my second spinner would able to get a list of USA's state array..
I use my newbie logic to try to think this way : set the 2nd spinner to View.GONE, then if lets say USA is selected, show the spinner2 which it alone carry the states.. but if I have 60 countries, then I must have 60 spinner, that's funny so help me :D

Rather than creating a Spinner for each country instance, keep the same Spinner, and replace the data stored in it when a value is selected from your first Spinner with
ArrayAdapter.createFromResource() and Spinner.setAdapter(). Here is an example using those two calls.
Rather than hiding your second Spinner before an option is selected in the first, consider having a default selection in the first Spinner, and populating the second with the matching data. In your example, USA would be a good default selection.

Related

How to populate 2 spinner based on selection of 1 spinner where data comes from server in android using volley?

I am new to android and i want to make 3 spinner, 1 is to work, 2 is stain and 3rd is price. The data should come from server through JSON. If i select Metier example plumber then by selection of plumber i should get the list of tasks related to the plumber. . Price should get the price. The spinner should change according to 1st spinner.
All you have to do that give a network call on first spinner selection
for example:
if plumber is selected in first spinner post this string to server and get the data regarding this string "plumber"
after you get the data populate the data with second and third spinner
Hope its help :)
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
//Here give a network call
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

How to disable and enable Spinner items on user click

I am developing an android app, which asks security questions after Sign Up for forgot password purpose, which has 10 questions in total. User can select any 3.
I have 3 Spinner for 3 question. Once user select the question from first spinner, second and third spinner should not have them in their list. Please help me to disable or remove that from the list.
screen shot of the activity
First, set a boolean check if it is the first time a spinner is selected. Store the selected item, so that you can add them on question change later.
Boolean ifFirstCheck = true;
String storeItem = "";
Then, Use below code:
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(!ifFirstCheck){
listSp2.add(storeItem);
sp2adapter.notifyDataSetChanged();
listSp3.add(storeItem);
sp3adapter.notifyDataSetChanged();
}
String selectedItem = spinner1.getSelectedItem().toString();
listSp2.remove(selectedItem) // Get selected value from spinner1 and remove thar item from spinner2
sp2adapter.notifyDataSetChanged(); // Notify adapter of spinner2 to that dataset has been changed
listSp3.remove(selectedItem)
sp3adapter.notifyDataSetChanged();
storeItem = selectedItem;
ifFirstCheck = false;
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Do as above for rest of the spinners.Hope this helps.
I assume you are using an Adapter for the Spinners. If you use an ArrayAdapter and each adapter holds the same list of items, you can just remove the selected items from the list and notify the adapters to update.

How to display the distance and time travelled between two points using spinners?

I want to create an app by selecting town-a from one spinner and then town-b from the other spinner. I then want to display the distance between those towns as well as the time it would take to get there from the selected towns.
I have found something like this:
var dist = [
['Town B', [], []],
['Town C', [67], ['1h00m']],
['Town D', [282,251], ['11h10m','10h00m']],
['Town E', [243,210,41], ['9h45m','8h25m','1h40m']],
Now this is obviously for HTML dropdown boxes and then calculates from the above file (dist.js)
Now I would like to know how to convert that to using two spinners. I think I have the basic idea but not sure how to implement. What I am thinking is when spinner 1 is selected and then spinner 2 then it needs to say that spinner 1 = spinner 2 and the distance is 67km and time to get the is 1h00m.
I don't have code for this cause I haven't tried it yet because I am not sure where to start. So am hoping someone would help me?
EDIT
This is what I have sofar:
public class MainActivity extends Activity implements OnItemSelectedListener {
private Spinner startloc;
private Spinner enddes;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startloc = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter_start = ArrayAdapter
.createFromResource(this, R.array.start_location,
android.R.layout.simple_spinner_item);
adapter_start.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
startloc.setAdapter(adapter_start);
startloc.setOnItemSelectedListener(this);
enddes = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter_end = ArrayAdapter
.createFromResource(this, R.array.end_location,
android.R.layout.simple_spinner_item);
adapter_end.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
enddes.setAdapter(adapter_end);
enddes.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
And this:
<string-array name="start_location">
<item>Town a</item>
<item>Town b</item>
<item>Town c</item>
</string-array>
<string-array name="end_location">
<item>Town a</item>
<item>Town b</item>
<item>Town c</item>
</string-array>
<string-array name="distance">
<item>0</item>
<item>67</item>
<item>282</item>
<item>251</item>
</string-array>
<string-array name="time">
<item>0m</item>
<item>2h40m</item>
<item>11h10m</item>
<item>10h00m</item>
So basically this means: Town a to Town a is 0 and time is 0, then Town a to Town b is 67km and time is 2h40m, Town a to town c is 282km 11h10m and finally Town b to town c 251km and time is 10h00m. (and needs to obviously work in reverse of list order as well)
Now how do I do this?
Screen shot:
I think you may be hung up on how the data that populates a spinner works in Android.
A Spinner in Android uses an Adapter to fill the Spinner with backing data. The data could be a collection of custom objects, Strings, Integers, etc... The Spinner can also register a listener so that when an item is selected from the Spinner a callback is made that indicates which item was selected.
A very simple implementation would be to use arrays of Strings populate the Spinners. When a selection is made for Spinner1, you'd store that value somewhere. When a selection is made for Spinner2 you'd store that value and check to see that a selection had been made for Spinner1. If a selection had been made for each spinner, then you'd be able to look up the distance / time for each selection. A very simple way to do this would be to use if-else statements:
if (spinnerOne.equals("Denver")) {
if (spinnerTwo.equals("New York") {
//Print out that distance equals 1280 miles
} else if (spinnerTwo.equals("San Fransisco") {
//Etc...
}
} else if (spinnerOne.equals("New York") {
//Etc...
}
The implementation I describe above is a super simple way of doing it and would work but is mainly just for illustration. There are probably better ways of structuring the data.

How to getting value from checkbox and spinner simultaneously at a time in android

I have checkboxs and spinners just like this:
In the above pictures: first spinner asiaSpinner have different countries and others europeSpnner and africaSpinner.
If I checked Asia and choose japan from spinner and checked Europe and choose Germany from spinner. then click set Details button. set Details both japan and Germany with respective information.
I want to set following field and save on database, what i checked. If i checked only one then one and if i checked all then save all.
My main intension is how to bind check box and spinner.
Each spinner should have an OnItemSelectedListener like this:
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Get/set your info from the spinner here (depends on how you are populating the spinner
}
});
And then just check the state of each checkbox:
final CheckBox asiaCk = (CheckBox) findViewById(R.id.asia_cb);
if (asiaCk.isChecked()) {
// Set whatever data you need appropriately
}

How to change data on one spinner by onclick on another spinner in android?

all i have two spinners spinner1 & spinner2. in one spinner i am showing names from database.(Ex. onkar) now if i select onkar name on spinner1 then all names must be occurred on spinner2 except name onkar.
i want to do like this.but i am not getting how to achieve it?
give me some sample or give idea for the same.
or can i change data of spinner dynamically????
Thanks in Advance--
consider that u have used two adapters to set data to spinner when u select first spinner just remove that value from second adapter and then call method notifyDataSetChanged() it will change second spinner data.
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
super.onItemSelected(parent, view, position, id);
// Step 1: Remove Data from ArrayList 1
// Step 2: Adpater for first spinner .notifiyDataSetChanged()
}

Categories

Resources