Multiple spinners Onitemselected Result overwritten - android

I have two spinners in my code. The problem I face is I'm unable to differentiate the results of the two spinners.I know we can use ids but I'm unable to do it.here's my code
public class AddProduct extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
Spinner spinner = findViewById(R.id.spinner1);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner, new String[]{"Men", "Women", "Kids"});
spinner.setAdapter(adap);
spinner.setTag(1);
spinner.setOnItemSelectedListener(this);
Spinner spinner2 = findViewById(R.id.spinner2);
spinner2.setTag(2);
SpinnerAdapter adap2 = new ArrayAdapter<String>(this,
R.layout.spinner, new String[]{"Casual", "Formals", "Ethinic"});
spinner2.setAdapter(adap2);
spinner2.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();
}

Modify onItemSelected method
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = "";
if(parent.getId() == R.id.spinner1) {
text = "spinner1 " + parent.getItemAtPosition(position).toString();
} else if(parent.getId() == R.id.spinner2) {
text = "spinner2 " + parent.getItemAtPosition(position).toString();
}
Toast.makeText(parent.getContext(), text , Toast.LENGTH_SHORT).show();
}

differentiate the results:
Spinner s1, s2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_product);
s1= findViewById(R.id.spinner1);
SpinnerAdapter adap = new ArrayAdapter<String>(this, R.layout.spinner, new String[]{"Men", "Women", "Kids"});
s1.setAdapter(adap);
s1.setTag(1);
s2= findViewById(R.id.spinner2);
s2.setTag(2);
SpinnerAdapter adap2 = new ArrayAdapter<String>(this,
R.layout.spinner, new String[]{"Casual", "Formals", "Ethinic"});
s2.setAdapter(adap2);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.spinner1)
{
String sp1= String.valueOf(s1.getSelectedItem());
// Toast.makeText(this, sp1, Toast.LENGTH_SHORT).show();
if(sp1.contentEquals("Men")) {
//Do Something
}
if(sp1.contentEquals("Women")) {
//Do Something
}
if(sp1.contentEquals("Kids")) {
//Do Something
}
} else if(spinner.getId() == R.id.spinner2)
{
String sp2= String.valueOf(s2.getSelectedItem());
// Toast.makeText(this, sp2, Toast.LENGTH_SHORT).show();
if(sp2.contentEquals("Casual")) {
//Do Something
}
if(sp2.contentEquals("Formals")) {
//Do Something
}
if(sp2.contentEquals("Ethinic")) {
//Do Something
}
}
}

Related

how to invisible recyclerview item getting value from spinner

I want when user click on "قرآن" my program invisible else things mean tarjuma and tafsir,
same like when he clicks on "مع ترجمہ" my program visible Quran and tarjuma and hide the tafsir.
I am using spinner it works fine but after refresh my recyclerview but I want without refreshing the recyclerview, mean is that when the user selects any Spinner value recyclerview change the view.
refresh mean going to another activity.
anybody who help me!!
here is my RecyclerView Adapter
#Override
public void onBindViewHolder(#NonNull final ViewHolder holder, int position) {
final Quran quran = quranic.get(position);
if (Quran.spinner.equals("قرآن")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.reference.setText(quran.getReferences());
holder.tarjuma.setVisibility(View.INVISIBLE);
holder.tafsir.setVisibility(View.INVISIBLE);
} else if (Quran.spinner.equals("مع ترجمہ")) {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.reference.setText(quran.getReferences());
holder.tafsir.setVisibility(View.INVISIBLE);
} else {
holder.quran.setText(Html.fromHtml(quran.getQuranText()));
holder.tarjuma.setText(Html.fromHtml(quran.getTarjuma()));
holder.tafsir.setText(Html.fromHtml(quran.getTafsir()));
holder.reference.setText(quran.getReferences());
}
}
my MainActivity
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Quran.java
public static String spinner = "";
You just need to call notifyDataSetChanged(); on your recycler view adapter each time you select an item from your spinner.
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});
Even better if you keep a reference to your recycler view adapter, then replace:
RecyclerView.Adapter adapter = recyclerView != null ? recyclerView.getAdapter() : null;
if (adapter != null) {
adapter.notifyDataSetChanged();
}
by:
adapter.notifyDataSetChanged();
}
Spinner spinner = (Spinner) findViewById(R.id.visibility);
List<String> spinnerList = new ArrayList<>();
spinnerList.add(0, "Select");
spinnerList.add(1, "قرآن");
spinnerList.add(2, "مع ترجمہ");
spinnerList.add(3, "مع تفسیر");
ArrayAdapter dataAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, spinnerList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getItemAtPosition(position).equals("Select")) {
// do nothing
} else {
String item = parent.getItemAtPosition(position).toString();
Quran.spinner = item;
//add this line
yourAdapterObject.notifyDataSetChanged();
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
});

How do I use the inputs of 2 separate spinners

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

Spinner in android studio

I am creating a gpa calculator and have used a spinner to populate the grades. how do I multiply the value in spinner2 which is credits to the grade points value in textview1 so it displays the result in textview2 which is the gradevalue.(should be something like: gradevalue=credits*gradepoints)
`public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = String.valueOf(points1[position]);
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
enter code here`
you just need to set Onitemselected listener on spinner depending upon selected item's index you can change textview's value.Hope it will help you
You need to set the text on your spinner and you can get it with a method like :
getSelectedValue() and you add +1 on it because it start at 0.
Hey KunjiDaniel this is working at my end.Please check this Spinner spinner;
ArrayAdapter<String> adapter;
String[] spinnerData = {"1", "3", "4", "5", "6"};
TextView text2, resultText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_spinner);
spinner = (Spinner) findViewById(R.id.spinner);
text2 = (TextView) findViewById(R.id.numbr2);
text2.setText(String.valueOf(3));
resultText = (TextView) findViewById(R.id.result);
adapter = new ArrayAdapter<String>(SampleSpinner.this, android.R.layout.simple_dropdown_item_1line, spinnerData);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
String selected_value = adapterView.getItemAtPosition(position).toString();
changeResultantValues(selected_value);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
int value = 0;
public void changeResultantValues(String spinnerSelected) {
if (!spinnerSelected.equals("")) {
value = Integer.parseInt(spinnerSelected);
}
String textview2Value = text2.getText().toString();
int value2 = Integer.parseInt(textview2Value);
String resultant = String.valueOf(value * value2);
resultText.setText(resultant);
}

how to multiply value selected from a spinner to a textview to display result in another text view

i want to multiply the credits by the gradePoints to return the gradevalue in textview2. how do i do that? the credits is the second spinner. `public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = String.valueOf(points1[position]);
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
`
Maybe you can try this:
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner, spin2;
TextView TextView1;
TextView TextView2;
Double point = -1.0;
Double value = -1.0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
AdapterView.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
point = points1[position];
TextView1.setText(String.valueOf(point));
if ( value != -1.0 ) {
double res = (double) point * value;
TextView2.setText(String.valueOf(res));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
String valueStr = credits[position];
value = Double.parseDouble(valueStr);
if ( point != -1.0 ) {
double res = (double) point * value;
TextView2.setText(String.valueOf(res));
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}
Hope this will work!
You can try making your selected values member variables then changing the value of the textview each time it's selected:
public class MainActivity extends AppCompatActivity{
int selectedCredit = 0;
double selectedPoints = 0;
#Override
public void onCreate(Bundle savedInstanceState){
int[] credits ={1,2,3,4};
double[] points = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
selectedCredit = credits[position];
double value = selectedCredit * selectedPoints;
textView.setText("The calculated value is " + value);
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
spinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id){
selectedPoints = points[position];
double value = selectedCredit * selectedPoints;
textView.setText("The calculated value is " + value);
}
#Override
public void onNothingSelected(AdapterView<?> parent){}
});
}
}
I have given a solution according to what I understood from your question. Hope this helps. :)
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] grades ={"A","B+","B","C+","C", "D+", "D", "E"};
String[] credits ={"1","2","3","4"};
double [] points1 = {4.0, 3.5, 3.0, 2.5, 2.0, 1.5, 1.0, 0 };
Spinner spinner;
TextView TextView1;
TextView TextView2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView1 = (TextView) findViewById(R.id.gradePoints);
TextView2 = (TextView) findViewById(R.id.gradeValue);
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter1 =
new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, grades);
spinner.setAdapter(adapter1);
spinner.setOnItemSelectedListener(onItemSelectedListener1);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_spinner_item, credits);
spin2.setAdapter(adapter2);
spin2.setOnItemSelectedListener(this);
}
spinner.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = spinner.getSelectedItem().toString();
TextView1.setText(s1);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
spin2.OnItemSelectedListener onItemSelectedListener1 = new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String s1 = spinner.getSelectedItem().toString();
Double gradePoint = 0.0;
if(s1.equals("A")){
gradePoint = 4.0;
}
else if(s1.equals("B+")){
gradePoint = 3.5;
}
else if(s1.equals("B")){
gradePoint = 3.0;
}
else if(s1.equals("C+")){
gradePoint = 2.5;
}
else if(s1.equals("c")){
gradePoint = 2.0;
}
else if(s1.equals("D+")){
gradePoint = 1.5;
}
else if(s1.equals("D")){
gradePoint = 1.0;
}
else if(s1.equals("E")){
gradePoint = 0;
}
int courseCredit = Integer.parseInt(spin2.getSelectedItem().toString());
double result = gradePoint*courseCredit;
TextView2.setText(String.valueOf(result));
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
};
public void onItemSelected(AdapterView<?> parent, View v, int position, long id ){
Spinner spin = (Spinner)parent;
Spinner spin2 = (Spinner)parent;
if(spin.getId() == R.id.spinner)
{
Toast.makeText(this, "Your choice :" + grades[position],Toast.LENGTH_SHORT).show();
}
if(spin2.getId() == R.id.spinner2)
{
Toast.makeText(this, "Your choice :" + credits[position],Toast.LENGTH_SHORT).show();
}
}
public void onNothingSelected(AdapterView<?> parent){
Toast.makeText(this, "Choose grades :", Toast.LENGTH_SHORT).show();
}
}

setOnItemSelectedListener of Spinner does not call

I used setOnItemSelectedListener to specify which item is selected in a spinner, but i think it is not called. it should print "It works" when the program runs.
XML code:
<Spinner
android:id="#+id/quantity"
android:layout_width="94dp"
android:layout_height="27dp"
android:layout_x="11dp"
android:layout_y="118dp"
/>
Java code:
public class quantity extends Activity
{
Spinner quantity;
public void onCreate(Bundle savedInstanceState)
{
quantity=(Spinner)findViewById(R.id.quantity);
quantity.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
Object item = parent.getItemAtPosition(pos);
System.out.println("it works... ");
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
}
Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout);
//to fill your Spinner
List<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("Item 1");
spinnerArray.add("Item 2");
spinnerArray.add("Item 3");
spinnerArray.add("Item 4");
spinnerArray.add("Item 5");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner) findViewById(R.id.quantity);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
if (item != null) {
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
Toast.makeText(MainActivity.this, "Selected",
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
}
you have missed setContentView(R.layout.yourlayout);

Categories

Resources