关于从Android内部类获取价值的问题

时间:2010-03-19 11:51:46

标签: java android

嗨,我的代码出了点问题,我不知道为什么 我的程序有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

1 个答案:

答案 0 :(得分:1)

我认为你必须首先进行初始化,然后使用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)+" บาท");}

}