How to get text back from the android spinner? - java

I am creating an android spinner. It is displaying properly but nothing happens on selecting an option. Why is 'if' command not working?
public class Main2Activity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
spinner= (Spinner) findViewById(R.id.spinner);
adapter=ArrayAdapter.createFromResource(this,R.array.spinner,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<?> adapterView, View view, int i, long l) {//why 'if' command is not working?
if(adapterView.getItemAtPosition(i).toString().equals("Question 1"))
{
Intent intent=new Intent(Main2Activity.this,Main2Activity.class);
}
if(adapterView.getItemAtPosition(i).toString().equals("Question 2"))
{
Intent intent=new Intent(Main2Activity.this,main3Activity.class);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});`

You are not starting the activity with intent. Do this:
startActivity(intent);
After intent declaration and assignment in both if and else.

Related

Can't get selected spinner item outside #Override method and into another class

I have MainActivity and SpinnerActivity, I want to get selected spinner item value from SpinnerActivity into the MainActivity. I tried to declare String as Static, I also tried with getter but without any success.
in Spinner Activity I also have if statement, how can I get value outside "if statement" and "#Override method" into another class?
MainActivity.class
public class MainActivity extends AppCompatActivity {
SpinnerActivity spinnerActivity = new SpinnerActivity();
Spinner spinnerProvince;
String selectedSpinnerProvince = spinnerActivity.inSpinnerSelectedProvince;
// String selectedSpinnerProvince = SpinnerActivity.inSpinnerSelectedProvince;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerProvince = findViewById(R.id.spinnerProvince);
populateSpinnerProvinces();
spinnerProvince.setOnItemSelectedListener(spinnerActivity);
}
public void populateSpinnerProvinces() {
ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.province));
provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvince.setAdapter(provincesAdapter);
}
}
SpinnerActivity.class
public class SpinnerActivity implements android.widget.AdapterView.OnItemSelectedListener {
public String inSpinnerSelectedProvince;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spinnerProvince) {
inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
} else {// code here}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I were about to advice you to use Intent to share data between both activities when i noticed that your "SpinnerActivity" is not really an Activity since it not extends Android Activity class (AppCompactActivity or other classes like this).
Your SpinnerActivity is a Listener. You can use it to implement the action to trigger when an action is performed on your Spinner view. For that you need to do it inside the "#Override" method of onItemSelected.
If you don't like to use the previous methode because of the Override methode you should implement directly the action to trigger at on click events on your spinner view in your MainActivity by doing this:
public class MainActivity extends AppCompatActivity {
SpinnerActivity spinnerActivity = new SpinnerActivity();
Spinner spinnerProvince;
String selectedSpinnerProvince = spinnerActivity.inSpinnerSelectedProvince;
// String selectedSpinnerProvince =
SpinnerActivity.inSpinnerSelectedProvince;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerProvince = findViewById(R.id.spinnerProvince);
populateSpinnerProvinces();
//spinnerProvince.setOnItemSelectedListener(spinnerActivity);
spinnerProvince.setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spinnerProvince) {
inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();
} else {// code here}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
public void populateSpinnerProvinces() {
ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_item,
getResources().getStringArray(R.array.province));
provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerProvince.setAdapter(provincesAdapter);
}
}

App crashes on spinner item click

I have this code for my simple spinner:
private Spinner spnr_gender;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complete_profile);
//GUI
spnr_gender = (Spinner)findViewById(R.id.spnr_finish_gender);
ArrayAdapter<CharSequence> gender_adapter = ArrayAdapter.createFromResource(this, R.array.strs_gender, android.R.layout.simple_spinner_item);
gender_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnr_gender.setAdapter(gender_adapter);
spnr_gender.setOnItemClickListener((AdapterView.OnItemClickListener) this);
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selectChoice = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(), selectChoice, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
What I want is to toast what the user has selected on the spinner. My string array in the strings.xml:
<string-array name="strs_gender">
<item>Male</item>
<item>Female</item>
</string-array>
When running the app it just crashed.
I can't find anything on the logcat but my suspect is this line spnr_gender.setOnItemClickListener((AdapterView.OnItemClickListener) this);.
How can I fix this?
Use this
spnr_gender.setOnItemSelectedListener(this);
Instead of
spnr_gender.setOnItemClickListener((AdapterView.OnItemClickListener) this);
setOnItemSelectedListener
void setOnItemSelectedListener (AdapterView.OnItemSelectedListener listener)
Register a callback to be invoked when an item in this AdapterView has been selected.
EDIT
Use this
ArrayAdapter<CharSequence> gender_adapter = ArrayAdapter.createFromResource(this, android.R.layout.simple_spinner_item,getResources().getStringArray(R.array.strs_gender));
Instead of this
ArrayAdapter<CharSequence> gender_adapter = ArrayAdapter.createFromResource(this, R.array.strs_gender, android.R.layout.simple_spinner_item);
Spinner example
Use Only (this) context while declaring item click listener
Do not cast with (AdapterView.OnItemClickListener) with it.
Use only
spnr_gender.setOnItemSelectedListener(this);
make it in this fashion
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
here is the solution
private Spinner spnr_gender;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_complete_profile);
//GUI
spnr_gender = (Spinner) findViewById(R.id.spnr_finish_gender);
ArrayAdapter<CharSequence> gender_adapter = ArrayAdapter.createFromResource(this, R.array.strs_gender, android.R.layout.simple_spinner_item);
gender_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spnr_gender.setAdapter(gender_adapter);
spnr_gender.setOnItemSelectedListener((AdapterView.OnItemSelectedListener) this);
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String selectChoice = adapterView.getItemAtPosition(i).toString();
Toast.makeText(adapterView.getContext(), selectChoice, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}

Passing spinner selection between activities

I have 4 activity with spinner inside every activity and this spinner include 3
string data (drop-down selection), when I pass from activity to another one I must pass this selected data inside spinner like if I have chosen data x from the list in the spinner and click in button the selected data must be in second activity spinner as x too.
I read several solutions without any solving. I hope to solve it here and this is my code for spinner and where to put the intent code
public class Page1 extends AppCompatActivity {
Spinner spinner;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this,R.array.film_type,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " selected", Toast.LENGTH_LONG).show();
switch (position)
{case 0:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
double thick = Double.valueOf(editText1.getText().toString());
double width = Double.valueOf(editText2.getText().toString());
}
});
break;
case 1:
btn[0] = (FloatingTextButton) findViewById(R.id.btn);
btn[0].setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
try {
} catch (NumberFormatException e) {
//not a double
}
}
});
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
First save the position of selected data from spinner in a String variable,
int positionOfSelectedDataFromSpinner;
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
positionOfSelectedDataFromSpinner= position;
}
Then on button click send intent to Another activity with putExtra
Intent i = new Intent (this, activity2.class);
i.putExtra("position", positionOfSelectedDataFromSpinner);
startActivity(i);
get int from getIntent in another activity
Intent intent = getIntent();
int positionToShowToSpinner = intent.getStringExtra("position");
then set the position to spinner
spinner.setSelection(positionToShowToSpinner);
I think this my solve your problem.
this is the most painless way i can think of:
make a new class or a static member of an existing class but the second solution makes the code less understandable.
public class SpinnerPosHolder{
public static int poition;
}
then in all 4 of them:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
SpinnerPosHolder.position = spinner.getSelectedItemPosition();
For using it:
spinner.setSelection(SpinnerPosHolder.position)
You can do it in this way
String selectedItem=spinner1.getSelectedItem().toString();
And that String you can pass using Intent

How to get the spinner value not in integer but in character

public class MainActivity extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
int noc = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner =(Spinner)findViewById(R.id.spinner);
String text= spinner.getSelectedItem().toString();
adapter= ArrayAdapter.createFromResource(this,R.array.Coffee_names,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) {
Toast.makeText(getBaseContext(),parent.getItemIdAtPosition(position) + " is selected",Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
My app stops when i add " String text= spinner.getSelectedItem().toString(); " to my code but it works fine when i remove this line from the program. But when i select a item from dropdown list it says 4 is selected.
I want it to say the name of the item i selected and not the index value.
Please help.
I think it should be:
Toast.makeText(getBaseContext(), parent.getSelectedItem() + " is selected", Toast.LENGTH_SHORT).show();
Alternative way
final String[] coffeeNames = getResources().getStringArray(R.array.Coffee_names);
spinner.setAdapter(new ArrayAdapter<>(activity, android.R.layout.simple_list_item_1, coffeeNames));
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedCoffeeName = coffeeNames[position];
Toast.makeText(getBaseContext(), selectedCoffeeName + " is selected", Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Spinner Default Value / Change position

Hello to the community,
got a problem with the implementation of my project, which I hope someone can give me because some clarity.
Short description of my project:
I want to change from the Activity_A means of a button onclick events the default position of a spinner in the Activity_B. In my sample code I've tried using an Intent, Value Activity_A to the spinner in the Activity_B in an if - to give statement which then starts the spinner in the corresponding position.
Here is my code:
Activity_A:
public class Start extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
Button button = (Button) findViewById(R.id.button_1);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Start.this, Spinnerwert.class);
intent.putExtra("position", "3");
startActivity(intent);
}
});
}
}
Activity_B:
public class Spinnerwert extends Activity {
private TextView beschreibung_1;
private TextView beschreibung_2;
private Spinner s1;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_wert);
// TextView issue of Spinner
beschreibung_1 =(TextView)findViewById(R.id.tV_spinner_1);
// TextView output Intent worth
beschreibung_2 =(TextView)findViewById(R.id.tV_spinner_2);
// Intent data receiving
Intent i = getIntent();
String selected = i.getStringExtra("position");
beschreibung_2.setText(selected);
s1 = (Spinner) findViewById(R.id.spinner_1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Spinner position - Response to Intent worth handover --------
s1.setSelection(position);
if (position == 0) {
s1.setSelection(0);
} else if (position == 1) {
s1.setSelection(1);
} else if (position == 2) {
s1.setSelection(2);
}
beschreibung_1.setText((CharSequence) parent.getItemAtPosition(position));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}
It seems as though your intent is passing a String extra, and only changing a text view within the second activity. If I am correct at understanding your question, you are wanting to place the spinner in a certain position based on the intent passed from the first activity.
Using the calls similar to those in your IF statement, simply parse the selected String to an int and call s1.setSelection on it.
It should look something like this:
s1 = (Spinner) findViewById(R.id.spinner_1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setSelection(Integer.parseInt(selection));
I hope this helped!
It looks like you're never setting the spinner to the selected index. Your if statements in onItemSelected aren't doing anything since "position" is a value that gets passed in from the spinner and not the previous intent.
Try putting this
s1.setSelection(Integer.parseInt(selected));
after you set your adapter.
Spinner variant with equals value
public class Spinnerwert_2 extends Activity {
private TextView beschreibung_3;
private TextView beschreibung_4;
private Spinner s2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_wert_2);
beschreibung_3 =(TextView)findViewById(R.id.tV_spinner_3);
beschreibung_4 =(TextView)findViewById(R.id.tV_spinner_4);
Intent intent_spinner_2 = getIntent();
String spinner_2_auswahl = intent_spinner_2.getStringExtra("position_spinner_2");
beschreibung_4.setText(spinner_2_auswahl);
// #2 Spinner + Funktion(Auswahl aus Resource values/array.xml)
s2 = (Spinner) findViewById(R.id.spinner_2);
ArrayAdapter<CharSequence> adapter_2 = ArrayAdapter.createFromResource(this, R.array.auswahl, android.R.layout.simple_spinner_item);
adapter_2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s2.setAdapter(adapter_2);
// #2 SPINNER-ITEM POSITIONSEINSTELLUNG / ZUWEISUNG
s2.setSelection(adapter_2.getPosition(spinner_2_auswahl));
s2.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (s2.getSelectedItem().toString().equals("Mercury")){
Toast.makeText(getApplicationContext(), "Auswahl Mercury", Toast.LENGTH_SHORT).show();
} else if (s2.getSelectedItem().toString().equals("Venus")){
Toast.makeText(getApplicationContext(), "Auswahl Venus", Toast.LENGTH_SHORT).show();
} else if (s2.getSelectedItem().toString().equals("Earth")){
Toast.makeText(getApplicationContext(), "Auswahl Earth", Toast.LENGTH_SHORT).show();
}
beschreibung_3.setText((CharSequence) parent.getItemAtPosition(position));
}
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
}
}

Categories

Resources