I have the following doubt I have to use some Arrays List in my project they are ...
List Restaurant Dishes Restaurant 1 Dishes Restaurant 2 Price Restaurant 1 Price Restaurant 2
Within my Activity I put two Spinners one that will get the Array List Restaurants and the other the I want it to appear the data of the Array list when I select the restaurant and in the end I would pick and setaria as a Text View of the price.
Just to get an idea of what's being populated in Spinner
<string-array name="restaurant">
<item>McDonalds</item>
<item>KFC</item>
</string-array>
<string-array name="restaurant_McDonalds_Plate">
<item>Combo Big Mac</item>
<item>Combo Deluxe Bacon</item>
<item>Combo Club House</item>
</string-array>
<string-array name="Restaurant_McDonalds_Plate_Price">
<item>R$ 31,00</item>
<item>R$ 29,00</item>
<item>R$ 35,00</item>
</string-array>
<string-array name="Restaurant_KFC_Plate">
<item>Combo Balde de 6 peças</item>
<item>Combo Balde de 9 peças</item>
<item>Combo Balde de 12 peças</item>
</string-array>
<string-array name="Restaurant_KFC_Plate_Price">
<item>R$ 25,00</item>
<item>R$ 35,00</item>
<item>R$ 45,00</item>
</string-array>
try below code , i have explained and wrote it ..
final Spinner retaurant;
final Spinner plate;
final TextView txt_retaurant;
final TextView price;
retaurant.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String restaurantname = retaurant.getSelectedItem().toString();
txt_retaurant.setText(restaurantname);
//if McDonalds, populates plate spinner with respective
if(position == 0){
ArrayAdapter<String> adapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.restaurant_McDonalds_Plate));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plate.setAdapter(adapter);
}
//if kfc, populates plate spinner with respective
if(position == 1){
ArrayAdapter<String> adapter= new ArrayAdapter<String>(view.getContext(),android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.Restaurant_KFC_Plate));
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
plate.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
plate.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// if McDonalds selected , it gets respective price list of plate and sets the price list to textview according to selected value in plate spinner
if(retaurant.getSelectedItemPosition() == 0){
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.Restaurant_McDonalds_Plate_Price));
price.setText(myArrayList.get(position));
}
// if kfc selected , it gets respective price list of plate and sets the price list to textview according to selected value in plate spinner
if(retaurant.getSelectedItemPosition() == 1){
List<String> myArrayList = Arrays.asList(getResources().getStringArray(R.array.Restaurant_KFC_Plate_Price));
price.setText(myArrayList.get(position));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
I want to implement in my application, certain mathematical calculation, but, it has a variable that covers age group and data, I will show in practice: 'Adequação da CB = CB obtida/CB percentile 50 * 100' . This is the formula, where the variable "CB percentile 50" has 28 different forms of being expressed, for example, from 1 to 2 years old it has the value of 16, from 2 to 3 has the value of 16.3, from 3 To 4 has the value of 16.8 and so on... How do I make the spinner check the age data and return this value to be placed in the mathematical expression?
My strings file
<string name="fxetaria">Selecione a Faixa Etária</string>
<string-array name="faixaet">
<item>1 a 1.9</item>
<item>2 a 2.9</item>
<item>3 a 3.9</item>
<item>4 a 4.9</item>
<item>5 a 5.9</item>
<item>6 a 6.9</item>
<item>7 a 7.9</item>
<item>8 a 8.9</item>
<item>9 a 9.9</item>
<item>10 a 10.9</item>
<item>11 a 11.9</item>
<item>12 a 12.9</item>
<item>13 a 13.9</item>
<item>14 a 14.9</item>
<item>15 a 15.9</item>
<item>16 a 16.9</item>
<item>17 a 17.9</item>
<item>18 a 24.9</item>
<item>25 a 29.9</item>
<item>30 a 34.9</item>
<item>35 a 39.9</item>
<item>40 a 44.9</item>
<item>45 a 49.9</item>
<item>50 a 54.9</item>
<item>55 a 59.9</item>
<item>60 a 64.9</item>
<item>65 a 69.9</item>
<item>70 a 74.9</item>
</string-array>
And my activity:
public class adeqcbh extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adeqcbh);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.faixaet, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I'm thinking the best way would be to have another array with the values (the same amount as your first array).
<string-array name="faixaet_values">
<item>16</item>
<item>16.3</item>
...
<string-array>
Then, in your onItemSelected method, just get the value from the new string array, like so;
String valueString = getResources().getStringArray(R.array.faixaet_values)[position - 1];
float value;
try {
value = Float.valueOf(valueString);
} catch(NumberFormatException e) {
Log.e(TAG, "This is not a number");
}
I have a spinner in my activity which contain Operators name ,what I want when user selected item from spinner I want to send another value to server,like
if user select "Airtel" from spinner I have to send "AR" to server same like other items .how can I do that.
code:-\
<string-array name="operators">
<item>Select Operator</item>
<item>Aircel</item>
<item>Airtel</item>
<item>BSNL</item>
<item>Idea</item>
<item>Vodafone</item>
<item>MTNL Delhi</item>
<item>MTNL Mumbai</item>
</string-array>
here when user select item from above list I have to send value from below list according to above items.
<string-array name="Operators_Code">
<item>AC</item>
<item>AT</item>
<item>BS</item>
<item>ID</item>
<item>VD</item>
<item>MT</item>
</string-array>
A Spinner, similar to ListView, RecyclerView, etc., is an "adapter backed" View. That is, it gets the items to be displayed from an Adapter.
When you set the entries to be shown using android:entries="#array/operators, Spinner internally creates an Adapter with the supplied array items. This simple solution, however, doesn't support complex objects.
For your use case, you'll have to create a custom Adapter for your Spinner.
public class Operator {
String name;
String code;
#Override
public String toString() {
return name;
}
}
final List<Operator> operators = new ArrayList(7);
operators.add(new Operator("Select operator", null));
operators.add(new Operator("Aircel", "AC"));
operators.add(new Operator("Airtel", "AC"));
....
Next, set the adapter on your spinner:
final ArrayAdapter<String> operatorsAdapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, operators);
spinner.setAdapter(operatorsAdapter);
That's it. Now if you want to listen to user's selections, add a listener to your Spinner by:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final Operator selectedOperator = operatorsAdapter.getItem(position);
final String selectedOperatorCode = selectedOperator.code;
// TODO: Send the selected operator to the server.
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Okay spinner 1 uses Manu_array
spinner 2 uses Manu 1xsd, Manu 2xrsd or 3x4rsd
all I need for now is to be able to populate the second spinner, after selecting the a value from the Manu_array in spinner 1.
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
My java file code:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// 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
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
I can process code that will allow the spinner to populate with the array data, but I cant get the second spinner to populate based on the selection of the first spinner, surely its simple if equals value use array.
The spinner above shows the values for the Manu_array, the next spinner has to take the value selected and populate with the appropriate array values, but all I have managed to do is generate a second spinner with the array values I have manual typed in, not selected.
if(Manu.equals("Manu 1xsd")){spinner2 languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......
Any suggestions where I may look for examples please looked at a few on here and confused me something chronic.
Set the second spinner adapter when an item from the first spinner is clicked
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch(position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd;
break;
};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
spinner2.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
i am developing one spinner this spinner i am string array
spinner = (Spinner)this.findViewById(R.id.mlg);
final CharSequence[] itemArray =getResources().getTextArray(R.array.RectBeam);
final List<CharSequence> itemList =new ArrayList<CharSequence>(Arrays.asList(itemArray));
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,itemList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new 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();
}
............................
<string-array name="RectBeam">
<item value="3000000" > Steel</item></string-array>
this is the spinner related string array i am get the spinner item i am using parent.getItemAtPosition(pos).toString(),done my problem is particular item value how can get
example : steel----------->3000000
I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.
Create two arrays in your array.xml file like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>Nokia 1200</item>
<item>Nokia 1600</item>
<item>Nokia 5130</item>
</string-array>
<string-array name="values">
<item>1000</item>
<item>2000</item>
<item>5000</item>
</string-array>
</resources>
Now load first array in your adapter and store the second one in other Array to hold values of items as:
String items [] = getResources().getStringArray(R.array.items);
String values [] = getResources().getStringArray(R.array.values);
And you can simply get the respective item name and value in your onItemSelected() method like this:
String item = parent.getItemAtPosition(pos).toString();
String value = values [pos];
In Android, I am trying to get the selected Spinner value with a listener.
What is the best way to get the spinner's value?
Spinner mySpinner = (Spinner) findViewById(R.id.your_spinner);
String text = mySpinner.getSelectedItem().toString();
The Spinner should fire an "OnItemSelected" event when something is selected:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Say this is your xml with spinner entries (ie. titles) and values:
<resources>
<string-array name="size_entries">
<item>Small</item>
<item>Medium</item>
<item>Large</item>
</string-array>
<string-array name="size_values">
<item>12</item>
<item>16</item>
<item>20</item>
</string-array>
</resources>
and this is your spinner:
<Spinner
android:id="#+id/size_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/size_entries" />
Then in your code to get the entries:
Spinner spinner = (Spinner) findViewById(R.id.size_spinner);
String size = spinner.getSelectedItem().toString(); // Small, Medium, Large
and to get the values:
int spinner_pos = spinner.getSelectedItemPosition();
String[] size_values = getResources().getStringArray(R.array.size_values);
int size = Integer.valueOf(size_values[spinner_pos]); // 12, 16, 20
Yes, you can register a listener via setOnItemSelectedListener(), as is demonstrated here.
View view =(View) getActivity().findViewById(controlId);
Spinner spinner = (Spinner)view.findViewById(R.id.spinner1);
String valToSet = spinner.getSelectedItem().toString();
If you already know the item is a String, I prefer:
String itemText = (String) mySpinner.getSelectedItem();
Calling toString() on an Object that you know is a String seems like a more roundabout path than just casting the Object to String.
add setOnItemSelectedListener to spinner reference and get the data like that`
mSizeSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
selectedSize=adapterView.getItemAtPosition(position).toString();