Been making my first app and slow progress is being made. Just wondering how I return a Spinner value to pass it as a string.
Here is my code:
First the event class:
public void event(){
calanderBtn = (Button)findViewById(R.id.eventBtn);
calanderBtn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_INSERT);
intent.setType("vnd.android.cursor.item/event");
intent.putExtra(CalendarContract.Events.TITLE, "Home");
intent.putExtra(CalendarContract.Events.DESCRIPTION, "Cleaning : ");
intent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Home");
startActivity(intent);
}
});
}
Second the Spinner Class:
public void selectcleaning() {
spCleaning = (Spinner) findViewById(spinner);
adapterCleaningType = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cleaningType);
adapterCleaningType.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spCleaning.setAdapter(adapterCleaningType);
}
I'm trying to select the value from selectcleaning (Value is either Yes or No) and pass it in the descrption in the Calendar of the event class and I'm not sure what to do.
you can use this code to get the value
Spinner spinner = (Spinner)findViewById(R.id.spinner);
String text = spinner.getSelectedItem().toString();
or get the value when spinner selected using OnItemSelectedListener (Android Doc)
public class SpinnerActivity extends Activity implements
OnItemSelectedListener {
#Override
protected void onStart() {
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
You should try using the Overrides for the spinner class as so:
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
}});
For your purposes 'int position' would equal 0 (yes) or 1 (no)
Try setting an on item selected listener. When an item is selected, you can store that value in a global variable. It gets the first item by default too.
spCleaning.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Value is current spinner item
globalStringVariable = (String) parent.getItemAtPosition(position);
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
#Override
public void onNothingSelected(AdapterView<?> parent) {
// This is default, before you've selected anything.
// It gets the first value.
globalStringVariable = parent.getItemAtPositon(0);
}
});
Related
Here is screenshot ...my changes in spinner value reflects only after 2 swipes I have an app with 10 screens and i have 1 spinner in my main activity and a textbox in fragment .I want to display the recently selected spinner value in my textbox .
I wrote a code to display the selected spinner value in my text box and its become stable throughout all my screens.But i don't know how to dynamically change the text box value according to the recently selected spinner value. (Before downvoting kindly suggest me some solutions)
Here is code to get the spinner value
String pos = (String) spinner.getSelectedItem();
SharedPreferences sharedPref = getSharedPreferences("Mode", Activity.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putString("userChoicemode", pos);
prefEditor.commit();
Here is the code to retrieve it
TextView modeselect = (TextView) view.findViewById(R.id.pass);
SharedPreferences sharedPref = this.getActivity().getSharedPreferences("Mode", Activity.MODE_PRIVATE);
String get = sharedPref.getString("userChoicemode",selected);
modeselect.setText("" + get);
Call below method
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text=spinner.getSelectedItem().toString();
txtview.setText(text);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
this above method calls when list set to your spinner and whenever user selects any item from spinner, you can get your spinner value from button click also by calling getSelectedItem() or if u want to get text only when user selects item from spinner you have to call this above override method
Try this
yourSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedText = parent.getItemAtPosition(position).toString()
// Set your TextView here
myTextView.setText(selectedText);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
And if your TextView is in different Fragment then follow this link Communication between Activity and Fragment
You can call spinner's setOnItemSelectedListener
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
modeselect.setText(spinner.getSelectedItem().toString());
//or
//modeselect.setText(adapterView.getItemAtPosition(i));
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
textView.setText(spinner.getSelectedItem().toString());
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
I wanted to display selected item in the textView when selected from dropdown list of spinner I implemented AdapterView.OnItemSelectedListener but when I'm selecting item its always null/empty here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new CustomAdapter(MainActivity.this, Languages));
btn.setOnClickListener(this);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
item = (String)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
SO answer source: https://stackoverflow.com/a/49301966/5461982
I know this is an older question now but if anyone else comes across it be sure to check that your custom implementation of the Adapter you're setting for your Spinner overrides the getItem method:
#Override
public Object getItem(int position) {
return spinnerItems.get(position);
}
By default, you're required to override this method when implementing a custom BaseAdapter but the default return type is null. Be sure to modify the return type to return spinnerItems.get(position).
Hope this helps, I spent around 30 mins trying to work this out originally!
Try this,
String selected_item = spinner.getSelectedItem().toString();
Just retrieve value from String Array:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String item = Languages[position];
Toast.makeText(MainActivity.this, item, Toast.LENGTH_SHORT).show();
}
try this
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sSelectedItem = arr_spiner.get(spinner.getSelectedItemPosition())
.getName();
txtSpinnerValue.setText(sSelectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
https://stackoverflow.com/questions/45159011/spinner-item-is-not-visible-data-is-coming-from-server-android/45161202#45161202
Try This
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new CustomAdapter(MainActivity.this, Languages));
btn.setOnClickListener(this);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
//item = (String)parent.getItemAtPosition(position);
item = (String) spinner.getSelectedItem().toString(); //Here is the Change
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
The change here is
From
item = (String)parent.getItemAtPosition(position);
TO
item = (String) spinner.getSelectedItem().toString();
I'm trying retrieve an object from my spinner array and set it to a certain constant, in this case "EFFECT_AQUA"
My array
String[] spinnerValues = {"Aqua", "Mono", "Blackbird", "Negative"};
when the user clicks on the "Aqua" in the spinner I want the screen to change to Aqua.
My spinner is set and called
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this,R.layout.custom_spinner,spinnerValues));
But not sure how should I approach. Seen many different answers but haven't found anything working.
I know my switch will come in this part
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
}
Any help is appricated!!
If you want to retrieve an object from your spinner array on item click, you can easily do it with the position parameter in the method onItemSelected.
For example :-
class SpinnerActivity extends Activity implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String itemSelected = spinnerValues[position];
//Set this to a constant
}
}
Now if you select Aqua, then code will set the variable itemSelected to Aqua.
I hope this is what you need
final Spinner cardStatusSpinner1 = (Spinner)findViewById(R.id.text_interested);
String cardStatusString;
cardStatusSpinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
cardStatusString = parent.getItemAtPosition(pos).toString();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
put the code below to get the item first then you can manipulate by taking the values like aqua or any color.
spinner = (Spinner)findViewById(R.id.spinner1);
spinner.setAdapter(new ArrayAdapter<>(getApplicationContext(), android.R.layout.simple_expandable_list_item_1,spinnerValues));
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
TextView tv = (TextView)view;
Toast.makeText(getApplicationContext(), tv.getText().toString(), 5000).show();
switch(tv.getText().toString()){
case "Aqua":{
//change the color to aqua
break;
}
case " ...":{
//
break;
}
//.... for all the option
}
}
I have two spinners on my layout as below:
public class DemoActivity extends Activity {
private static final String STATE_SCALE = "state-scale";
private static final String STATE_CENTER_X = "state-center-x";
private static final String STATE_CENTER_Y = "state-center-y";
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
String text1 = spinner1.getSelectedItem().toString();
Spinner spinner3 = (Spinner) findViewById(R.id.spinner3);
String text3 = spinner3.getSelectedItem().toString();
if (text1.equals("Harris Academy"))
Harris(spinner1);
if (text1.equals("Harris Academy") && text3.equals("Ground Floor")) {
Toast.makeText(getBaseContext(), text3, Toast.LENGTH_LONG).show();
try {
SubsamplingScaleImageView imageView =
(SubsamplingScaleImageView) findViewById(id.imageView);
imageView.setImageAsset("DSC00277.png");
if (savedInstanceState != null &&
savedInstanceState.containsKey(STATE_SCALE) &&
savedInstanceState.containsKey(STATE_CENTER_X) &&
savedInstanceState.containsKey(STATE_CENTER_Y)) {
imageView.setScaleAndCenter(savedInstanceState.getFloat(STATE_SCALE),
new PointF(savedInstanceState.getFloat(STATE_CENTER_X),
savedInstanceState.getFloat(STATE_CENTER_Y)));
}
} catch (IOException e) {
Log.e(DemoActivity.class.getSimpleName(), "Could not load asset", e);
}
}
}
}
This works fine when the app starts it gets the value of spinner 1 and spinner 3 and then does something depending on the values.
How do I extend it so that when I select an item on either spinner it does something based on the new values?
To get updated value as user select item from spinner,
You have to implement spinner's onItemSelectedListner
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// Do your stuff here for spinner1
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner3.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// Do your stuff here for spinner3
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
If you want to manipulate views or data on the selection of item in Spinner you need to implement
ItemSelectedListener to the particular Spinner.
Sample Code:
// in onCreate() or where you want to bind this Views with XML views
{
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner3 = (Spinner) findViewById(R.id.spinner3);
// mItemSelectedListener Object of OnItemSelectedListener to handle Item Selection to Spinners.
spinner1.setOnItemSelectedListener(mItemSelectedListener);
spinner3.setOnItemSelectedListener(mItemSelectedListener);
}
// Creating an Object of Anonymous Class so no need to create another instance for same class
// OutSide of onCreate() method.
OnItemSelectedListener mItemSelectedListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (view.getId()) {
case R.id.spinner1:
// Manipulates Views at selection of item in Spinner1
break;
case R.id.spinner3:
// Manipulates Views at selection of item in Spinner3
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {}
};
i have this code
go_btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent myIntent = new Intent(LoginActivity.this, WebActivity.class);
myIntent.putExtra("id", ed_txt.getText().toString());
myIntent.putExtra("type", spinner.getText().toString());
LoginActivity.this.startActivity(myIntent);
i want the selected option in the spinner to my string.
this give me an error
myIntent.putExtra("type", spinner.getText().toString());
when i use
myIntent.putExtra("type", spinner.getContext().toString());
i am getting my package name.
how do i get my selected item
use
myIntent.putExtra("type", spinner.getSelectedItem().toString());
instad of
myIntent.putExtra("type",spinner.getText().toString());
for getting selected item text from Spinner
You will have to add a listener to the spinner to get the selected item. It should be something like this
String selection = null;
//Initialise Spinner (you may have done this already)
Spinner spinner = (Spinner) findViewById(R.id.[your spinner id]);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
selection = parent.getItemAtPosition(pos).toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
})
You can then use put the "selection" string into your intent. Good luck :)
Just check for this code and i hope this code useful,
String getSelectItem = "null";
Spinner spinner = (Spinner) findViewById(R.id.[your spinner id]);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
getSelectItem = spinner.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});