Android - How to add selection from Spinner to EditText - android

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));
}

Related

OnItemSelectedListener for Spinner in Android development

I'm using a spinner in order to select a certain value, and in the activity that contains the spinnr, I've implemented OnItemSelectedListener.
in the OnCreate() method, I've defined:
spinner1.setOnItemSelectedListener(this);
but it doesn't get to the method onItemSelected(), my guess is that I'm not sending the right variable ("this").
what should I send to setOnItemSelectedListener in order for this to get to the method?
Thank You!
activity that contains the spinnr, I've implemented
OnItemSelectedListener
If OnItemSelectedListener listener is implemented in Activity then override onItemSelected in Activity :
#Override
public void onItemSelected(AdapterView<?> parent,
View selectedItemView, int position, long id) {
// your code here
}
Try this.
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//Do something
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Also you can look at this. http://developer.android.com/guide/topics/ui/controls/spinner.html

Android Spinners

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)

Calling methods on objects in a ListView

I have a listview that is populated by a by objects from an "Offer" class I've written. Obviously, the toString() method is called and that's the text that's displayed in each list item. My question is, how do I implement an onItemClickListener that will call one of my getter or setter methods on the particular object whose toString method had been used to populate that item?
For example, I'd like to raise a toast or something when an item is clicked that retrieves a string my getClaimCode() method and displays in the toast (or whatever other dialog or even a new activity).
can I just call the methods by doing something like item.getClaimCode()...?
You don't have to implement AdapterView.OnItemClickListener, but I do, and in onCreate() I call myListView.setOnItemClickListener(this).
Then for the AdapterView.OnItemClickListener interface, I have the method:
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
//Code
}
For you, you might do something like:
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2, long arg3) {
//Arg2 is the position in the list so...
OfferObject offer = (OfferObject) arg0.getItemAtPosition(arg2);
//Go to town...
}
Of course your Activity doesn't have to implement the AdapterView.OnItemClickListener and you could create the listener on the fly.
yourList.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
OfferObject offer = (OfferObject) arg0.getItemAtPosition(arg2);
//Same idea here...
}
});

Spinner item gets automatically selected upon entering activity. How do I avoid this?

I have a spinner in my Android app, and its onItemSelected() event automatically gets triggered upon entering the activity.
How do I avoid this?
We can use a flag, and just enable it when the spinner is really touched.
private boolean isSpinnerTouched = false;
spinner.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
isSpinnerTouched = true;
return false;
}
});
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View arg1,
int arg2, long arg3) {
if (!isSpinnerTouched) return;
// do what you want
}
});
To add on Jerry Abraham, You should clear selection before enabling setOnItemSelectedListener
Spinner mSpinner=(Spinner)findViewById(R.id.mySpinner);
int initialSelectedPosition=mSpinner.getSelectedItemPosition();
mSpinner.setSelection(initialSelectedPosition, false); //clear selection
mSpinner.setOnItemSelectedListener(this); //set listener after clearing section
I have solved this issue,
You can avoid this issue by not setting any default values to the spinner
int initialposition=spinner.getSelectedItemPosition();
spinner.setSelection(initialposition, false);
This will avoid entering into onItemSelected()
There are no any way to avoid this.
You may add some flag, indicating readiness of your application and use it in your onItemSelected() method to decide, what to do in each case.
Well, you can add a dummy selection to the initial adapter, and ignore position number in the setOnItemSelectedListener. It's not pretty but it works. See this code for setting up the items for an array adapter.
List<String> names = new ArrayList<String>();
names.add("");
names.addAll(realValues);
Then in your setOnItemSelectedListener you can do this:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
if (position > 0)
{
String name = names.get(position - 1);
}
else
{
Log.d(TAG, "selected nothing or perhaps the dummy value");
}
}
I have found a solution for this problem and posted it here (with code sample):
Spinner onItemSelected() executes when it is not suppose to
Simple and easy is this...
validate with a boolean to see if is first time...
Spinner mySpinner = (Spinner)findViewById(R.id.spinner_xml_pro);
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(isSpinnerInitial){ // globar var boolean isSpinnerInitial = false;
//do something
}else
isSpinnerInitial=true;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Check this with spinner.post(new Runnable()...)
or this other my source
I think that you can use spinner position which is a better approach in my opinion.
Create a global variable where you store the spinner position, in onItemSelected method the position is provided you can compare them, if they are the same do not make an action.
private int spinnerPosition; \\ Global variable
mySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
if(spinnerPosition != position){
// Do whatever you like
// Do not forget to save the new position
spinnerPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
You can avoid it by ignoring the first click by,
private boolean isSpinnerInitial = true; //As global variable
public void onItemSelected(xxx xxx, xxx xxx, xxx xxx, xxx xxx) {
if(isSpinnerInitial) {
isSpinnerInitial = false;
return;
}
// Write your code here
}

Spinner on value Change

I have two spinner in my system. Now I have to change the selected value of the 2nd spinner depending on the first spinner value. As soon as the user will change the 1st spinner value, the 2nd spinner value will set automatically depending upon the 1st spinner's selected value. How to implement this?
From the Hello Spinner tutorial:
Now create a nested class that implements AdapterView.OnItemSelectedListener. This will provide a callback method that will notify your application when an item has been selected from the Spinner. Here's what this class should look like:
public class MyOnItemSelectedListener 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.
}
}
The AdapterView.OnItemSelectedListener requires the onItemSelected() and onNothingSelected() callback methods. The former is called when an item from the AdapterView is selected, in which case, a short Toast message displays the selected text; and the latter is called when a selection disappears from the AdapterView, which doesn't happen in this case, so it's ignored.
Now the MyOnItemSelectedListener needs to be applied to the Spinner. Go back to the onCreate() method and add the following line to the end:
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
In other words, you need to create an OnItemSelectedListener that modifies the value of the second spinner, and attach it to the first spinner.
You have to put the condition on onItemSelected of very first spinner. By this example you can get value of 2nd spinner depending on 1st spinner:
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
if(arg0.equals(spin0)){
spin1.setClickable(true);
if(spin0.getSelectedItem().equals("India"))
{
ArrayAdapter <String> s1 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_india);
s1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s1);
}
else if(spin0.getSelectedItem().equals("Pakistan"))
{
ArrayAdapter <String> s2 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_pak);
s2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s2);
}
else if(spin0.getSelectedItem().equals("China"))
{
ArrayAdapter <String> s3 = new ArrayAdapter <String> (this,android.R.layout.simple_spinner_item,states_china);
s3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin1.setAdapter(s3);
}
}
}
You should define the onItemSelected() separately for each spinner. Otherwise the code gets executed if anything is selected from either spinners.
newCategory.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
String selCat = newCategory.getItemAtPosition(arg2).toString();
if (selCat != "New")
{
loadSpinnerData(topic);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
newTopic.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
loadSpinnerData()
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

Categories

Resources