Hi I got something wrong in my code and I don't know why My program have 2 spinner box and 1 textview. when I choose something in both spinner it should call method from getrate inner class which return some value to textview. This getrate class working on my java app but it's don't working on android app.
here is my code
public class BTS extends Activity {
/** Called when the activity is first created. */
int i=0;
int j=0;
String[] li={"1","2","3"};
getrate gr;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner combo = (Spinner)findViewById(R.id.widget30);
final Spinner combo2 = (Spinner)findViewById(R.id.widget32);
final TextView rate = (TextView) findViewById(R.id.widget31);
ArrayAdapter<String> a = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, li);
ArrayAdapter<String> b = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, li);
combo.setAdapter(a);
combo2.setAdapter(b);
combo.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
i = (int) combo.getSelectedItemId();
if(i==0||j==0){rate.setText("กรุณาเลือกสถานีต้นทางและปลายทาง");}
else {rate.setText("ราคาค่าโดยสาร : "+gr.get(i, j)+" บาท");}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
combo2.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
j = (int) combo2.getSelectedItemId();
if(i==0||j==0){rate.setText("กรุณาเลือกสถานีต้นทางและ开发者_运维知识库ปลายทาง");}
else {rate.setText("ราคาค่าโดยสาร : "+gr.get(i, j)+" บาท");}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}
class getrate {
private String[][] list;
int c2=0;
String[] ct={"1","2","3"};
getrate(){
}
public String get(int i,int j){
return ct[i];
}
}
Ps. Sry for my english ;D
I think you have to initialise first, then refer to it by using BTS.this.gr
That would be:
public void onCreate(Bundle savedInstanceState) {
this.gr=new getrate();
...
// in your anonymous class
else {rate.setText("ราคาค่าโดยสาร : "+BTS.this.gr.get(i, j)+" บาท");}
}
精彩评论