How to pass value from onItemSelected to another spinner - android

I have got 3 spinners in a activity which are populated from webservices with 3 methods as follows using loopj. I want to pass the value of "ida" from onItemSelected of spinner1 to spinner3. So how can I pass a value from one method to another.
protected void onCreate(Bundle savedInstanceState) {
getSpinner1();
}
private void getSpinner1() {
RestClient1.getForSpinner1(
.
.
public void onItemSelected(AdapterView<?> parent, View view, int position, long ida) {
getSpinner2(ida);
}
private void getSpinner2(Long ida) {
RestClient2.getForSpinner2(
.
.
public void onItemSelected(AdapterView<?> parent, View view, int position, long idb) {
getSpinner3(ida,idb);
}
.
.
private void getSpinner3(Long ida,Long idb) {
RestClient3.getForSpinner3(
.
.
public void onItemSelected(AdapterView<?> parent, View view, int position, long idc) {
}

Try to use OnItemSelectedListener. There is a bried description on how to found here at StackOverFlow - how-to-pass-a-selected-spinner-item-between-spinners
I have used this before and it worked a charm.
EDIT: To summarise #Skizo-ozᴉʞS solution, the following -
Then create a globalVariable to get the String to your first Spinner as follows :
String FirstValue = "";
Then you'll need to call this :
spinnerLengthInput.setOnItemSelectedListener(this);
spinnerLengthOutput.setOnItemSelectedListener(this);
Of course you'll need to implements OnItemSelectedListener
Then inside you can do the same that you were doing.
#Override
public void onItemSelected(AdapterView<?> spinner, View view, int position,long id)
{
FirstValue = spinner.getItemAtPosition(position).toString();
checkIfConvertingFromMeter(itemSelectedInSpinnerLengthInput);
}
Then in your other Spinner use the FirstValue value.

Related

How to capture or store value of AppCompatSpinner as a string in a String variable?

I have an AppCompatSpinner with 3 entries in it. I want the entry chosen to be stored as a String so that I can save it in my database.
Here's the XML code:
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/randomId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="25dp"
android:entries="#array/aList"
style="#style/Base.Widget.AppCompat.Spinner">
</android.support.v7.widget.AppCompatSpinner>
How can I do this?
You will need to add an onItemClickListener to your Spinner that does what you want.
It might look something like this:
appCompatSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
saveToDb(adapterView.getItemAtPosition(position).toString());
}
});
As an alternative, you could implement OnItemClickListener in your class if you prefer.
That is done like so:
public class MySpinnerActivity extends Activity implements AdapterView.OnItemClickListener {
#Override
public void onCreate {
AppCompatSpinner appCompatSpinner = (AppCompatSpinner) findViewById(R.id.my_spinner);
appCompatSpinner.setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
saveToDb(adapterView.getItemAtPosition(position).toString())
}
}

How do I change a list item text for a ListFragment/ArrayAdapter?

I have a fragment which extends ListFragment.
In it I use an ArrayAdapter
public class MyFragment extends ListFragment {
public void onStart() {
super.onStart();
List<String> list = new ArrayList<String>();
list.add("Superman");
list.add("Batman");
arrayAdapter = new ArrayAdapter<OptionToStringAdapter>(
getActivity(),
android.R.layout.simple_list_item_1,
list);
getListView().setAdapter(arrayAdapter);
getListView().setOnItemClickListener(new ModifyOption(this));
setListShown(true);
}
When the user taps an item I want to modify the text.
How do I modify the text?
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
???What goes here????
}
}
Assuming you want to change the content of the list item you clicked.
First, change the value using a dialog or widget of your choice.
Second, change the value of the item at the position in the list by using 'position' argument.
Third, call notifyDataSetChanged() method of the adapter object.
public class ModifyOption implements OnItemClickListener {
public void onItemClick(AdapterView parent, View view, int position, long id) {
// create a dialog or other widget
String newValue = "your_new_value";
list.set(position,newValue);
arrayAdapter.notifyDataSetChanged(); //informs views that data has been changed
}
}
try with below code:
public class ModifyOption implements OnItemClickListener { public void onItemClick(AdapterView parent, View view, int position, long id) {
arrayAdapter.remove(arrayAdapter.getItem(position));
arrayAdapter.insert("new text",position);
} }
This seems to do the trick. I don't know if this is an appropriate way though.
public void onItemClick(AdapterView parent, View view, int position, long id) {
TextView textview = (TextView) view.findViewById(android.R.id.text1);
textview.setText("Spiderman");
}

Global variable inside Spinner onItemSelected does not work

I've been thinking about this a long while but realy can't figure out what the problem is. The first code piece works in one of my activities. selectedUser is globally declarend and I can use it everywhere.
spUsers.setAdapter(new MyAdapter(this, R.layout.user_sp_row,userArray));
spUsers.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int index = spUsers.getSelectedItemPosition();
selectedUser = userList.get(index).get(Constants.TAG_UNAME);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
selectedUser = "";
}
});
This code does not work allthough it looks the same like the other code I wrote. selectedCat is also globally declared. The thing is the value is set in the onItemSelected method but as soon as it leaves the method selectedCat is an empty string.
spCat.setAdapter(new MyAdapter(this, R.layout.category_sp_row,tempArray));
spCat.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int index = spCat.getSelectedItemPosition();
selectedCat = categoryList.get(index).get(Constants.TAG_UPID);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
selectedCat = "";
}
});
I don't know why are using int index = spUsers.getSelectedItemPosition(); to get the index when you can use the position integer provided in onItemSelected() method.
I've got this working by indeed doing everything that needs to be done in the setOnItemSelectedListener().
Ty DrkStr

how can i get spinner item's id?

I have a spinner named spinnerArray
hot can I get the selected item's ID of spinner by integer data type after click a button?
(when I have selected First one of spinner, I want to get integer 0,Second one,integer 1)
Use
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(final AdapterView<?> parent, final View view, final int position, final long id) {
// position is what you want i think
}
#Override
public void onNothingSelected(final AdapterView<?> parent) {
}
});
And to get the position when clicked on other views use spinner.getSelectedItemPosition();
You can do this in your button's onClick method:
mySpinner.getSelectedItemPosition() + 1;
getSelectedItemPosition() gives you the item position starting at 0, so you need to add 1.
i think you are looking for position in spinner
code sample
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView,
int position, long id) {
// your code here
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});
Are you looking to see when the items on the spinner have been clicked? Then OnItemSelectedListener will allow you to override onItemClick(AdapterView parent, View view, int position, long id).

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)

Categories

Resources