嗨,我的代码有误,我不知道为什么
我的程序有2个微调框和1个textview。
当我在两个微调器中都选择某个东西时,应该从getrate内部类中调用方法
返回一些值到textview。
这个getrate类别适用于我的Java应用程式,但不适用于android应用程式。
这是我的代码
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];
}
}
附言抱歉我的英语; D
最佳答案
我认为您必须先进行初始化,然后使用BTS.this.gr
进行引用
那将是:
public void onCreate(Bundle savedInstanceState) {
this.gr=new getrate();
...
// in your anonymous class
else {rate.setText("ราคาค่าโดยสาร : "+BTS.this.gr.get(i, j)+" บาท");}
}