App crashes on spinner item click - java

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

Related

Unable to set onItemSelectedListener for spinner in Android studio

This is my main activity
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
}
#Override
public void onSearchStateChanged(boolean enabled) {
}
#Override
public void onSearchConfirmed(CharSequence text) {
}
#Override
public void onButtonClicked(int buttonCode) {
}
}
Here I want to set onItemClickListener for spinner. I have tried by creating a Inner class and by creating a anonymous class, I have even tried implementing AdapterView.onItemSelectedListener and passing
spinner.setOnItemSelectedListener(this);
But every way android studio gives me compile-time error.
What can be the possible and correct way to setOnItemSelected for spinner and How?
error :
enter image description here
Code:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener, AdapterView.OnItemSelectedListener{
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(this);
}
#Override
public void onSearchStateChanged(boolean enabled) {
}
#Override
public void onSearchConfirmed(CharSequence text) {
}
#Override
public void onButtonClicked(int buttonCode) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Using anonymous class:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public void onSearchStateChanged(boolean enabled) {
}
#Override
public void onSearchConfirmed(CharSequence text) {
}
#Override
public void onButtonClicked(int buttonCode) {
}
}
Using inner class:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener{
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(new spinner_class());
}
#Override
public void onSearchStateChanged(boolean enabled) {
}
#Override
public void onSearchConfirmed(CharSequence text) {
}
#Override
public void onButtonClicked(int buttonCode) {
}
public class spinner_class implements AdapterView.OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
This way there is no way error but is it correct:
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener{
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener((AdapterView.OnItemClickListener) new spinner_class());
}
#Override
public void onSearchStateChanged(boolean enabled) {
}
#Override
public void onSearchConfirmed(CharSequence text) {
}
#Override
public void onButtonClicked(int buttonCode) {
}
public class spinner_class implements AdapterView.OnItemSelectedListener{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
}
Add setOnItemClickListener in your oncreate method
public class MainActivity extends AppCompatActivity implements MaterialSearchBar.OnSearchActionListener {
Spinner spinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.sort_spinner);
ArrayAdapter<CharSequence> spinnerAadapter = ArrayAdapter.createFromResource(this,
R.array.sort_array, android.R.layout.simple_spinner_item);
spinnerAadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAadapter);
spinner.setOnItemClickListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.d(TAG,"###setOnItemClickListener position:"+position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
#Override
public void onSearchStateChanged(boolean enabled) {
}
#Override
public void onSearchConfirmed(CharSequence text) {
}
#Override
public void onButtonClicked(int buttonCode) {
}
}
I bet (because neither error message nor exact method setOnItemSelectedListener call location in code is known) that you have to implement OnItemSelectedListener, not AdapterView.OnItemSelectedListener interface in MainActivity. Described here.

How to get text back from the android spinner?

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.

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

Passing a string to another activity in android

I am working on an app and I need to pass the contents of some textviews to a new activity, but I want to save the content I pass while the app is open so that the user can select more items from other activities and send them to the final checkout activity.
right now I have spinners which save the selection to a Textview
public class Americano extends AppCompatActivity {
// MyDBHandler dbHandler;
String result;
TextView tvSize;
Spinner spinner;
int mPos;
String mSelection;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_americano);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner spinnerSize = (Spinner) findViewById(R.id.spinner_size);
AdapterView.OnItemSelectedListener listener = new myOnItemSelectedListener();
spinnerSize.setOnItemSelectedListener(listener);
Spinner spinnerSyrups = (Spinner) findViewById(R.id.spinner_syrups);
AdapterView.OnItemSelectedListener listenerSyrups = new myOnItemSelectedListener2();
spinnerSyrups.setOnItemSelectedListener(listenerSyrups);
Spinner spinnerTopping = (Spinner) findViewById(R.id.spinner_toppings);
AdapterView.OnItemSelectedListener listenerTopping = new myOnItemSelectedListener3();
spinnerTopping.setOnItemSelectedListener(listenerTopping);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
DBAdapter dbAdapter = new DBAdapter(view.getContext());
}
});
}
public class myOnItemSelectedListener implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
Americano.this.mPos = pos;
Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.tvSize);
resultText.setText(Americano.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
public class myOnItemSelectedListener2 implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
Americano.this.mPos = pos;
Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.tvSyrup);
resultText.setText(Americano.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
public class myOnItemSelectedListener3 implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View v, int pos, long id) {
Americano.this.mPos = pos;
Americano.this.mSelection = parent.getItemAtPosition(pos).toString();
TextView resultText = (TextView) findViewById(R.id.tvTopping);
resultText.setText(Americano.this.mSelection);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I have more menu item classes exactly like this one
I want to pass the textview data to the checkout page. But save that data in that page until the user closes the app.
thanks
You can do it sending the information through an Intent.
In your listener you would go to another actvity like this:
Intent intent = new Intent(getActivity(), NewActivity.class);
intent.putExtra("RESULT_TEXT", resultText);
startActivity(intent);
And on the NewActivity you can get the information like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
Bundle bundle = getIntent().getExtras();
String resultText = bundle.getString("RESULT_TEXT", "");
}
Hope it helps.

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