I am working on my first ever android app, and I'm creating basic exercise/calorie counter. I have two spinners, one for the selected type of exercise, and one for the time spent preforming said exercise in minutes. I need to be able to check the value/position of both spinners so I can do something like this:
PSUEDO CODE:
if(Exercise spinner = "push-ups")
{
CaloriesBurned = TimeSpinnerValue*450
}
if(Exercise spinner = "sit-up")
{
CaloriesBurned = TimeSpinnerValue*350
}
etc . . . nothing fancy. My spinners are populated from a String Array in my String.xml. But I dont know how get the value of the spinner so I can use it in some IF statements in my java code.
use like this for compair any string with your spinner.
spinner.equals("push-ups");
You need to implement OnItemSelectedListener for getting the selected value from the Spinner. Then override,
public void onItemSelected(AdapterView<?> parent, View v, int position,
long id) {
int id = parent.getId();
switch (id) {
case R.id.first_spinner:
// your stuff here
break;
case R.id.second_spinner:
// your stuff here
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
yourSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View arg1, int position, long arg3) {
String selectedString = (String) (yourSpinner.getAdapter()).getItem(position);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}});
check this link fro more info
Generically, you'll want to follow this pattern:
SpinnerAdapter adapter = Spinner.getAdapter();
int position = Spinner.getSelectedItemPosition();
Object value = adapter.getItem(position);
Since you are loading it with String values, you can then cast value to a String.
Have some field in your activity to hold value of spinner, initialize with some defaultvalue
implement onItemSelectedListener on spinners, and in onItemSelected get value of selected item, by position argument of onItemSelected(AdapterView adapterView, View view, int position, long id)
Related
I'm trying to get an AutoCompleteTextView's ID after I clicked a value on the list. Tried looking up on google and stackoverflow, but the provided answers didn't work. Here's what I've got:
Created the view in my class declaration:
public class ActivityCadastrarCliente extends Activity implements OnClickListener, OnItemClickListener {
AutoCompleteTextView E_Nome_Cliente, E_CPF;
List<String> Nomes = new ArrayList<String>();
...
Associated the view to an XML element:
E_Nome_Cliente = (AutoCompleteTextView)findViewById(R.id.Nome_Cliente);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, Nomes.toArray(new String[0]));
E_Nome_Cliente.setAdapter(adapter);
E_Nome_Cliente.setOnItemClickListener(this);
and my onItemClick method is called normally as below:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//switch (parent.getId()) {
//case R.id.Nome_Cliente:
...
//}
}
Does anybody know how I can access this view inside onItemClick? Tried several ways, but I only get exceptions:
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)view.getParent();
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)parent;
//Class cast exception
AutoCompleteTextView input = (AutoCompleteTextView)parent.getParent();
I need to identify which view was clicked, because I'm using 3 to 5 AutoCompleteTextView and based on the selected value I'll automatically fill in a bunch of other fields.
Have a look at the class AutoCompleteTextViewClickListener in this answer.
Change your setOnItemClickListener call in the following way:
E_Nome_Cliente.setOnItemClickListener(
new AutoCompleteTextViewClickListener(E_Nome_Cliente, this));
Now you can get the id by accessing the modified view parameter:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//view had been modified by AutoCompleteTextViewClickListener
//to contain the original AutoCompleteTextView
switch (view.getId()) {
case R.id.Nome_Cliente:
//...
}
}
An easier way:
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Adapter adapter = parent.getAdapter();
if (adapter == autoCompleteTextView1.getAdapter()) {
// Do something
} else if (adapter == autoCompleteTextView2.getAdapter()) {
// Do something else
}
}
i am not sure what do you mean by view id? do you want to get the selected value?
if yes, then the below code will do it, otherwise please clarify more what do you need and why you want to access the view itself.
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//switch (parent.getId()) {
String selected = adapter.getItem(position);
//}
}
more on adapter methods are here
Use parent.findViewById(R.id.id_of_autocompleteTextView) on the parent of the AutoCompleteTextView.
I have 2 arraylists, 1 is use to display the elements to the spinner and another one is use to display on a textview when one of the element from spinner is selected.
Example:
0---a---football
1---b---badminton
2---c---basketball
"a,b,c" are the elements in arraylist1; "football, badminton, basketball" are the elements in arraylist2; "0,1,2" are the index for both arraylists.The index of elements on both of the arraylists has already arranged properly as shown above.
What I want to do now is to let the spinner to display "a,b,c". When I select "b" in the spinner, the textview will show me "badminton".
What should I write in the onItemSelected of the spinner?Any idea for this?
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
switch(position){
case 0:
textView1.setText(sportsList.get(0));
break;
case 1:
textView2.setText(sportsList.get(1));
break;
case 2:
textView3.setText(sportsList.get(2));
break;
}
}
});
You can get the selected item in the spinner by this code
String selected = spinner1.getText().toString();
Then check for the condition,
if(selected.equals("a")){
textview1.setText(array2.get(0));
}else if(selected.equals("b")){
......
try this,
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3)
{
Textview your_tv = (Textview)findviewbyid(R.id.tv);
your_tv.setText(your_array[position]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
I got a Spinner in my app and it does not work when an item is clicked. I get the values but the if condition is not getting worked.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
String Text = effecttwo.getSelectedItem().toString();
System.out.println("spinner is -"+item+"-");
// I get the correct values in System.out.println
if(Text=="Hue"){
// not entering into this condition or any other condition
}else if(Text=="Saturation"){
}else if(Text=="Brightness"){
}else{
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Please suggest me what I am doing wrong.
I get the values but the if condition is not getting worked.
if(Text=="Hue"){
Wrong
use .equals or .equalsIgnoreCase to compare strings
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#equals(java.lang.Object)
if(Text.equals("Hue"))
{
}
What is the difference between == vs equals() in Java?
You dont compare string with ==. Use this:
if(text.equals("Hue"))
{
}
So your code goes this way:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
String Text = effecttwo.getSelectedItem().toString();
System.out.println("spinner is -"+item+"-");
// I get the correct values in System.out.println
if(text.equals("Hue")){
// not entering into this condition or any other condition
}else if(text.equals("Saturation")){
}else if(text.equals("Brightness")){
}else{
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
Don't do the listener inside anonymous class.
Do it withspinner.setOnItemSelectedListener(this) and put implement AdapterView.OnItemSelectedListener after your fragment or activity.
I've tried this a few different ways and all the code does is print out the top value of the spinner.
final String [] equipment=new String[]{//items
}
ArrayAdapter<?> ad=new ArrayAdapter<Object>(this,android.R.layout.simple_spinner_item,equipment);
ad.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spin=(Spinner)findViewById(R.id.spinneritem);
spin.setAdapter(ad);
int position = spin.getSelectedItemPosition();
final String equipmentitem = equipment[position].toString();
From what I can tell that should be able to take the value of the array that the user has selected, but no matter what I select in the list, it only select the first item, and ignores the actual selection.
You probably need to add a listener to the spinner.
spin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
equipmentitem = equipment[position].toString();
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
what I'm trying to do is make a selection from a spinner in android and then whatever is selected to be added to an edittext box. The code I have so far is this...
spinner.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
edittext.setText("");
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//add some code here
}
);
Problem is this seems to be run even before the spinner is selected so it always sets my edittext to "". Ideally I would like to have it set the text to the selection made in the spinner. So, anyone have any ideas?
At startup, your spinner will get its defaultvalue, that counts as a selection.
Do a boolean FirstTime or something like that.
You probably initialize your spinner from some array or something?
The function actually looks like this
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id);
So just use the position variable
{
edittext.setText(myArray[position]);
}
You can use the getItem method in the adapter to get the object that is shown. Like this:
onItemSelected(AdapterView<?> parent, View view, int position, long id) {
editText.setText((String) adapter.getItem(position));
}