I'd like to show the text containing result of addition of two values: one from onespinner selected item, and another from twospinner selected item. But eclipse shows an error in line
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
What's the matter? Full code goes below.
public class photographer extends Activity implements OnItemSelectedListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner onespinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> unitadapter = ArrayAdapter.createFromResource(
this, R.array.onespinner, android.R.layout.simple_spinner_item);
unitadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
onespinner.setAdapter(unitadapter);
onespinner.setOnItemSelectedListener(this);
Spinner twospinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> courseadapter = ArrayAdapter.createFromResource(
this, R.array.twospinner, android.R.layout.simple_spinner_item);
courseadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
twospinner.setAdapter(courseadapter);
twospinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
TextView text = (TextView)findViewById(R.id.result);
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
getSelectedItem is a method, but you are referencing it like an instance variable. You need to change your code to:
text.setText(onespinner.getSelectedItem() + twospinner.getSelectedItem());
You haven't posted the exact error, but I'm guessing that your problem is that you are trying to make a reference to onespinner and twospinner in onItemSelected and those two objects are not in the scope of that function; they are declared in onCreate.
Now, the View view argument of onItemSelected is the spinner that was clicked, but you need a reference to both spinners (not just the one that was selected). Easiest way to do this is to globally declare oneSpinner and twoSpinner and that should solve your problem.
EDIT: Also what Ted Hopp said.
Related
firstly I'm a newbie to using this program so any help is appreciated. I need spinner 1 to show 7 cities the user can choose from and spinner 2 to show the same 7 cities that the user can select.
I have some code below but my tutor says it's wrong and it won't work for the second spinners I tried to find a way but can seem to do so
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
}
spinner 1 is meant to be the start destination and spinner 2 is meant to be the destination they are going to. once the user selects the options it tells them the distance from spinner 1 to spinner 2.( the distances are already given to us in a table)
Your code contains only one spinner.
Spinner spinner = findViewById(R.id.spinner1);
Define one more spinner in XML and assign the same adapter to new spinner and set a separate OnItemSelectedListener each spinner.
Get the values(city names) from each spinner and calculate the distance.
TextView tvRes;
Button btn;
String fgpa;
int maxGrade[]=new int[] {4,3,3,3,3,3,2,2,2};
double rcvdGrade[]=new double[9];
double[] fres=new double[9];
int ssp1;
double sumRes=0,sumPoints=0;
double fail=0;
Spinner sp1,sp2,sp3,sp4,sp5,sp6,sp7,sp8,sp9;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1 = findViewById(R.id.sp1);
sp2 = findViewById(R.id.sp2);
sp3 = findViewById(R.id.sp3);
sp4 = findViewById(R.id.sp4);
sp5 = findViewById(R.id.sp5);
sp6 = findViewById(R.id.sp6);
sp7 = findViewById(R.id.sp7);
sp8 = findViewById(R.id.sp8);
sp9 = findViewById(R.id.sp9);
btn=findViewById(R.id.btn);
tvRes=findViewById(R.id.tvRes);
adapter=ArrayAdapter.createFromResource(this,R.array.grades,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
sp2.setAdapter(adapter);
sp3.setAdapter(adapter);
sp4.setAdapter(adapter);
sp5.setAdapter(adapter);
sp6.setAdapter(adapter);
sp7.setAdapter(adapter);
sp8.setAdapter(adapter);
sp9.setAdapter(adapter);
ssp1=sp1.getSelectedItemPosition();
if(ssp1==0){
rcvdGrade[0]=10;
}
if (ssp1=='1'){
rcvdGrade[0]=9;
}
if(ssp1=='2'){
rcvdGrade[0]=8;
}
if(ssp1=='3'){
rcvdGrade[0]=7;
}
if(ssp1=='4'){
rcvdGrade[0]=6;
}
if(ssp1=='5'){
rcvdGrade[0]=5;
}
if(ssp1=='6'){
fail=0;
}
I am new to programming. I am trying to get a Spinner position and by using that value, I will assign a value to a variable from which I can calculate a percentage. But this code always selects the first condition and sets the value to 10. I cannot figure out what's wrong with this code. Help me, guys. Also, if you have any suggestions or improvements with this code, please let me know, friends. Thanks in advance.
Extra: I also tried using the getSelectedItemPosition(); function directly on the if statement and also tried to get the values by comparing the text in the Spinner but it gives me the same (always selects the first condition) result.
Your code is running as soon as the activity is created. That means it gets the position of the spinner as soon as it's made, which will always be position zero. That code will not run again to give you a different value. You need to set up an OnItemSelectedListener so the value will change every time you change the position of the spinner.
private AdapterView.OnItemSelectedListener spinnerListener =
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Called anytime a spinner dropdown item is clicked
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
Then in your onCreate() method write this:
sp1.setOnItemSelectedListener(spinnerListener);
This is only applying to sp1, but if you set all the spinners with this same listener, know that the parameter parent in the listener is the spinner that changed. If you need to know which spinner changed, use the setTag() and getTag() methods.
In onCreate():
sp1.setTag("sp1");
sp2.setTag("sp2");
// set Listeners, etc
In your onItemSelected()
if (parent.getTag().equals("sp1")) //It was sp1 that changed
// do stuff
else if (parent.getTag().equals("sp2")) //sp2 changed
// do other stuff
EDIT: As per your request, here is a combination of the above:
// Previous declarations
int ssp1;
double sumRes=0,sumPoints=0;
double fail=0;
Spinner sp1,sp2,sp3,sp4,sp5,sp6,sp7,sp8,sp9;
ArrayAdapter<CharSequence> adapter;
private AdapterView.OnItemSelectedListener spinnerListener =
new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int position = parent.getSelectedItemPosition(); // also equal to the parameter position
if (parent.getTag().equals("sp1")) // sp1 was changed this time
// Change rcvdGrade[0] as you please
else if (parent.getTag().equals("sp2")) // sp2 changed
// Change rcvdGrade[1] as you please
else if (parent.getTag().equals("sp3")) //etc
//etc
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// This is required to make the listener happy, but leave it blank
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1 = findViewById(R.id.sp1);
sp2 = findViewById(R.id.sp2);
sp3 = findViewById(R.id.sp3);
// others set, adapters set, etc
sp1.setTag("sp1");
sp2.setTag("sp2");
sp3.setTag("sp3");
// etc
sp1.setOnItemSelectedListener(spinnerListener);
sp2.setOnItemSelectedListener(spinnerListener);
sp3.setOnItemSelectedListener(spinnerListener);
// etc
}
I'm not here to write all your code, but I hope this helps you understand this better. If not, please look up tutorials for the OnItemSelectedListener because that is what you want to best work with spinners. If you are certain you want to use OnClickListener, then set that to a button instead.
I've got a spinner that opens programmatically. It pops up and appears to be working fine, but for some reason my OnItemSelectedListener does not trigger any of the events within it.
public class BeerConverter extends Activity {
ArrayAdapter<CharSequence> adapter3;
Spinner spinner03;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner03 = new Spinner(this);
adapter3 = new ArrayAdapter<CharSequence> (this, android.R.layout.simple_spinner_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner03.setAdapter(adapter3);
spinner03.setOnItemSelectedListener(new MyOnItemSelectedListener3());
adapter3.add("Stuff");
spinner03.performClick();
}
Then I create the listener as a nested class:
public class MyOnItemSelectedListener3 implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id){
curPos = position;
Context context = getApplicationContext();
CharSequence text = "Test text. If you see this, it means MyOnItemSelectedListener3 was called.";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void onNothingSelected(AdapterView<?> arg0)
{
//do nothing
}
};
So when I send the spinner03.performClick(); the Spinner pops up correctly, but when an item in the Spinner is selected, it just closes and does not call the OnItemSelectedListener. It looks like this person had the same problem a while back, but didn't ever end up posting the solution.
As the comment thread above suggests, many if not all events related to a View will not behave as you expect if the View in question is not attached to a window. Do not use unattached Views to drive any sort of user interaction.
I had the same problem. I fixed it by setting spinner visibility not "Gone".
I am working on an app, which has a spinner in two activities. In the first activity the user can edit the spinner item and save the value. I used an ArrayAdapter and a onItemSelectedListener to store the value in the database. This works fine.
In the second activity the spinner with the changes of the user should be shown. Can anybody tell me how to solve this problem? If you need some more information, please ask, because I am a newbie on android.
This is the code for the first activity:
#Override
protected void onCreate(final Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.my_spinner1);
final Spinner spinner1 = (Spinner)findViewById(R.id.sp_countries);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.countries,
R.layout.spinner1);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parentView, View childView, int position, long id)
{
mSave.countries = spinner1.getSelectedItem().toString();
}
public void onNothingSelected(AdapterView<?> parentView)
{
}
});
Thanks for your answers in advance.
if you get the value from the updated database and set the whole values through setAdapter()
method in the spinner or notify the spinner to change something in database.
I am trying to connect 2 spinners together. Meaning, the items inside 2nd spinner will depend on whatever item is chosen for the 1st spinner.
This is the code inside the main java file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
This is the code inside MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So the onItemSelected function will print out the item that was chosen in 1st spinner on the screen. However, I can't figure out how to create the 2nd spinner fully based on the value inside 1st spinner.
I know that there should be something needed to be done inside onItemSelected, but I just can't figure it out since I am a newbie in Java Android.
Can you guys assist me on this?
Thank you.
You should just do something very similar to what you do in onCreate, only with the other spinner. For example:
List<String> values = findValuesBySelection(parent.getItemAtPosition(pos).toString());
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, values, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);