Reputation: 6522
What I'm trying to do is simple, I have a spinner with a few items, edittext and a button. I want to be able to select a certain item with a spinner and then type a certain value to edittext and then click a button. According to which spinner item I have selected earlier a textview will then change in the activity.
public void submitButtonClick (View submit){
Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Button b1 = (Button)findViewById(R.id.button2);
if (b1.performClick())
{
switch (){
}
}
}
This is what I came up with so far, if I click button b1, the following switch statement should start (in case item 1 is selected, do a certain thing, etc.) but I don't know how to achieve this. If someone could help I would appreciate it. Thank you
This is what I have so far:
public void submit (View v){
Button b1 = (Button)findViewById(R.id.button2);
final Spinner s1 = (Spinner)findViewById(R.id.spinner1);
final Context context = this;
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = s1.getSelectedItemPosition();
switch (position){
case 0:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setTitle("Warning");
alertDialogBuilder.setMessage("Please choose an item from the list");
alertDialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
Bifrost.this.finish();
}
});
AlertDialog spinnerError = alertDialogBuilder.create();
spinnerError.show();
break;
case 1:
break;
}
}
});
}
The code gives no errors and app starts normally but when I select the first item and then click the button nothing happens. Did I do something wrong creating the dialog?
Upvotes: 0
Views: 1457
Reputation: 39820
You can assign a listener to your spinner.
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
// will run every time the user select an item from your spinner
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// change your textView here, base on position (index of item selected in spinner)
if (position == 0) {
// user selected the first item in spinner
}
else if (position == 1) {
// user selected the second item in spinner
}
// and so on...
}
});
After that, you can assign another listener (similar way as the code above - anonymous inner class, anonymous declaration and initialization) for your button. It will also have to override onClick etc etc. There are lots of resources online regarding all of this.
Hope this helps!
Upvotes: 0
Reputation: 5162
You first need to set an onClickListener to your button, and in this you need to get the selected item of the spinner.
b1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
int position = s1.getSelectedItemPosition();
switch(position){
case 0: //first item
break;
case 1: //second item
break;
}
}
});
Upvotes: 1
Reputation: 54682
You can do something like this. you need to set a onClickListener
to the button
. when the button
is clicked then the onClick
method will be called. At that method check the selected item of the spinner
.. Forexmple
Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Button b1 = (Button)findViewById(R.id.button2);
b1.setOnclickListener(new onClickListener(){
public void onCLick(View v){
switch(s1.getSelectedItemPosition()){
case 0:
// do something
`enter code here`break
..........
}
}
}
);
I just tried to give a conceptual idea the code is not exact , but this is how you should do it. So my suggestion is before implementing this leanr the basic features of buttons
, spinners
, onCLickListeners
.
Upvotes: 0