How do I use the inputs of 2 separate spinners - android

I'm trying to build an app that takes 2 inputted redox pairs from 2 spinners to display information about the cell that could be made from these chemicals such as what would be used as the anode/cathode, the reduction potential of each half-reaction and the cell etc.
My issue is I haven't been able to find any way to take the input of both the spinners to be able to make the calculations, only have both spinners do the same thing.
My current code is this:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1, spinner2;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(MainActivity.this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(MainActivity.this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String choice1 = parent.getItemAtPosition(position).toString();
double first = 0;
double second = 0;
double anode;
double cathode;
if (choice1 == getResources().getString(R.string.MnO4)){
first = 0.60;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
} else if (choice1 == getResources().getString(R.string.Cl)) {
first = 1.36;
Toast.makeText(parent.getContext(), choice1, Toast.LENGTH_SHORT).show();
}
if (first > second) {
cathode = first;
anode = second;
} else {
cathode = second;
anode = first;
}
double cellValue = anode - cathode;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I'm very new to android studio so any help is greatly appreciated.
Thanks

I suggest that you use anonymous classes to handle the onItemSelected. That way you can handle the selection of each Spinner independently.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
adapter = ArrayAdapter.createFromResource(this, R.array.pairs, android.R.layout.simple_spinner_item);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner1 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// Do what you need to do with spinner2 info
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
Now you can remove the Override method:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
and you can remove implements :
implements AdapterView.OnItemSelectedListener

Related

how to change imageview view based on spinner selection in android studio?

I am trying to display image in image view, based on spinner selection. If anyone know how to solve this please help.
public class activity_tables extends AppCompatActivity {
Spinner s1;
String[] tbls;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tables);
s1 = (Spinner)findViewById(R.id.spinner);
tbls = getResources().getStringArray(R.array.tables);
final ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,tbls);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
}
}
Set onItemSelectedListener for your spinner like this
s1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
if(position==0)
{
qImageView.setImageResource(R.drawable.whatever);}
else if(position ==1)
{
qImageView.setImageResource(R.drawable.whatever);}
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

How can I pick only once a value from spinner in android

I have a spinner with ten numbers 0-10 and I if someone picks a number I want him not to be able to pick the same value again. So inside ItemSelected I do the following with no result
#Override
public void onItemSelected(AdapterView<?> adapter, View v, int position, long id) {
// On selecting a spinner item
if (position > 0 && position!=tempPosition)
{
TeamSpinnerNumber = adapter.getItemAtPosition(position).toString();
tempPosition = position
}
}
Check this i worked out for you may it help you.
public class MainActivity extends AppCompatActivity {
ArrayList<String> mStrings;
ArrayAdapter<String> mStringArrayAdapter;
Spinner mSpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSpinner = (Spinner) findViewById(R.id.spinner);
mStrings = new ArrayList<String>() {{
add("None");
add("one");
add("two");
add("three");
}};
mStringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mStrings);
mSpinner.setAdapter(mStringArrayAdapter);
mSpinner.setSelection(0);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String string = parent.getItemAtPosition(position).toString();
if (!string.equals("None"))
removeThisFromSpinner(string);
mSpinner.setSelection(0);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
private void removeThisFromSpinner(String s) {
mStrings.remove(s);
mStringArrayAdapter.notifyDataSetChanged();
}
}
try this, hope it helps :
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Log.i(TAG, "onItemSelected: "+position);
//checking the position of the selected spinner item, if the previous position is not the same then do something here
if(position>0 && position!=myPosition){
}
myPosition = position;
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
and don't forget to add global variable to the class :
int myPosition = 999; // the non existed position make it global variable

Spinner App crash on opening.

I am trying to make my Spinner1 change visibility on spinner2.My code compiles ok, but app crashes on opening.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onNothingSelected(AdapterView<?> arg0) {
}
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
TextView textview1 = (TextView)findViewById(R.id.textView1);
if (spinner.getSelectedItemPosition() == 1) {
textview1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.VISIBLE);
}
}
});
}
Please dont set ID's during an event; Its simply not good; do like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
Spinner spinner1 = (Spinner)findViewById(R.id.spinner1);
TextView textview1 = (TextView)findViewById(R.id.textView1);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onNothingSelected(AdapterView<?> arg0) {
}
public void onItemSelected(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
if (spinner.getSelectedItemPosition() == 1) {
textview1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.VISIBLE);
}
}
});}
Ok, so i fixed the initial app crash using an class to check the seleced item
main code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(new checkSelectedItem());
}
My class:
public class checkSelectedItem implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner1 = (Spinner)parent.findViewById(R.id.spinner1);
TextView textview1 = (TextView)parent.findViewById(R.id.textView1);
if (pos == 1) {
textview1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.VISIBLE);
}
else {
if ( textview1.getVisibility() == View.VISIBLE) {
textview1.setVisibility(View.INVISIBLE);
}
if ( spinner1.getVisibility() == View.VISIBLE) {
spinner1.setVisibility(View.INVISIBLE);
}
}
}
public void onNothingSelected(AdapterView parent) {
}
So the initial crash is fixed, but now when i select the option 1 in my spinner2 the app crashes..

Get the position of a spinner in Android

I'm trying to get the position (number) of the spinner when selected to use it in another Activity that will display a different map each time depending on the item selected. when I run the application it crashes. this is the first Activity code:
public class TestProjectActivity extends Activity {
public Spinner spinner1;
public Integer number;
private Button valideButton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MySpinner();
valide_button();
}
public void MySpinner() {
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.num, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView,
View selectedItemView, int position, long id) {
// Object item = parentView.getItemAtPosition(position);
TestProjectActivity.this.number = spinner1
.getSelectedItemPosition() + 1;
}
public void onNothingSelected(AdapterView<?> arg0) {// do nothing
}
});
}
public void valide_button() {
valideButton = (Button) findViewById(R.id.valide_button);
valideButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(TestProjectActivity.this,
MetroMapActivity.class);
startActivity(intent);
}
});
}
}
The way to get the selection of the spinner is:
spinner1.getSelectedItemPosition();
Documentation reference:
http://developer.android.com/reference/android/widget/AdapterView.html#getSelectedItemPosition()
However, in your code, the one place you are referencing it is within your setOnItemSelectedListener(). It is not necessary to poll the spinner, because the onItemSelected method gets passed the position as the "position" variable.
So you could change that line to:
TestProjectActivity.this.number = position + 1;
If that does not fix the problem, please post the error message generated when your app crashes.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt = findViewById(R.id.button);
spinner = findViewById(R.id.sp_item);
setInfo();
spinnerAdapter = new SpinnerAdapter(this, arrayList);
spinner.setAdapter(spinnerAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//first, we have to retrieve the item position as a string
// then, we can change string value into integer
String item_position = String.valueOf(position);
int positonInt = Integer.valueOf(item_position);
Toast.makeText(MainActivity.this, "value is "+ positonInt, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
note: the position of items is counted from 0.
final int[] positions=new int[2];
Spinner sp=findViewByID(R.id.spinner);
sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText( arg2....);
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
if (position ==0) {
if (rYes.isChecked()) {
Toast.makeText(SportActivity.this, "yes ur answer is right", Toast.LENGTH_LONG).show();
} else if (rNo.isChecked()) {
Toast.makeText(SportActivity.this, "no.ur answer is wrong", Toast.LENGTH_LONG).show();
}
}
This code is supposed to select both check boxes.
Is there a problem with it?

Create Spinner Programmatically

I have to create a Spinner which is not in my Layout.
so I want to create it Programmatic I write the following code in My Activity class
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
so My question is how I can access the spinner selected item in the activity
Your only way to access it outside your listener is making a variable that will hold the position, so you would use it later.
Something like this:
public class YourActivity extends Activity {
private int curPos;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Spinner spinner = new Spinner(this);
String option[]={"By Date","By Transaction ID","By Customer Phone"};
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,option);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item );
spinner.setAdapter(spinnerArrayAdapter);
spinner.performClick();
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
curPos = position;
Log.d("in select","yes");
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
Toast.makeText(MainMenu.this,"no",Toast.LENGTH_LONG).show();
Log.d("In No select","No");
}
});
}
In your onItemSelected:
#Override
public void onItemSelected(AdapterView<?> parentview, View v,int position, long id)
{
Log.d("in select","yes");
}
The position helps you to retrieve your selected item. Which, in your case, it should be:
option[position]

Categories

Resources