ok rob,
I did what you mentioned above:
MyDC.java:- Code: Select all
package com.super.mydc;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class MyDC extends Activity {
private EditText amount1;
private double x=0;
private double y=2.0;
private double z=0;
private TextView tt;
private Button calculate;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// We want to view some very simple text, so we need a TextView
TextView tv = new TextView(this);
// Put some text to the newly created TextVIew
tv.setText("Test");
// Tell our Application to display the textView
this.setContentView(tv);
super.onCreate(icicle);
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);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
z=x-(x*y/100);
tt.setText(Double.toString(z));
String discountSelected = discountDropDownField.getSelectedItem().toString();
}
}
In my Calculate(), this line:
String discountSelected = discountDropDownField.getSelectedItem().toString();
it was giving error so I right-clicked on discountDropDownField and create new class for it
Here it is: discountDropDownField.java- Code: Select all
package com.super.mydc;
public class discountDropDownField {
double discount = 0.0;{
if ("10% discount A".equals(discountSelected)){
discount = 0.1;
}
if ("15% discount B".equals(discountSelected)){
discount = 0.15;
}
else
if ("18% discount C".equals(discountSelected)){
discount = 0.18;
}
}
}
If you see the code above, I changed default discount to 0.0 since I don't want any value to be selected by default in dropdown menu, so I will just an item saying "Select discount type". (I hope I am doing correctly).
But after adding all these code you provided me, I am getting a lot of error messages like this:
- Code: Select all
discountSelected cannot be resolved to a variable discountDropDownField.java line 7 Java Problem
discountSelected cannot be resolved to a variable discountDropDownField.java line 10 Java Problem
discountSelected cannot be resolved to a variable discountDropDownField.java line 14 Java Problem
The method getSelectedItem() is undefined for the type discountDropDownField MyDC.java
please help...