如何在Android中使用另一个类中的变量?

时间:2014-07-05 00:27:02

标签: java android class variables

我想使用我在CollegeList类的MainActivity类中创建的一些变量。我试着通过互联网寻找答案,但我仍然不确定该怎么做。

MainActivity类:

double scoreDouble;
TextView score;
EditText gpa;
EditText sat;
EditText act;
Button calc;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

gpa = (EditText) findViewById(R.id.gpa);

sat = (EditText) findViewById(R.id.sat);

act = (EditText) findViewById(R.id.act);

score = (TextView) findViewById(R.id.score);
calc = (Button) findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        String gpaString = gpa.getText().toString();
        if (gpaString.equals("")) {
            gpaString = "0";
        }
        double gpaDouble = Double.parseDouble(gpaString);

        String satString = sat.getText().toString();
        if (satString.equals("")) {
            satString = "0";
        }
        int satInt = Integer.parseInt(satString);

        String actString = act.getText().toString();
        if (actString.equals("")) {
            actString = "0";
        }
        int actInt = Integer.parseInt(actString);
        if (actInt / 36.0 < satInt / 2400.0) {
            scoreDouble = (0.6 * gpaDouble * 25)
                    + (0.4 * ((double) satInt / 2400.0) * 100.0);
        } else {
            scoreDouble = (0.6 * gpaDouble * 25)
                    + (0.4 * ((double) actInt / 36.0) * 100.0);
        }

        }

    }
);
}

}

CollegeList课程:

public class CollegeList extends ListActivity {

String[] colleges = new String[100];

private double gpa;
private int act;
private int sat;
private String name;
private String location;
private double score;

public CollegeList(double gpa, int act, int sat, String name, String location){
    this.gpa = gpa;
    this.act = act;
    this.sat = sat;
    this.name = name;
    this.location = location;
    if(act/36.0>sat/2400.0){
        score = 0.6*gpa*25.0+0.4*(act/36.0)*100.0;
    }else{
        score = 0.6*gpa*25.0+0.4*(sat/2400.0)*100.0;
    }
}

public double getGpa(){
    return this.gpa;
}   
public int getAct(){
    return this.act;
}
public int getSat(){
    return this.sat;
}
public String getName(){
    return this.name;
}
public String getLocation(){
    return this.location;
}





@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(CollegeList.this, android.R.layout.simple_list_item_1, colleges));



    }

}

例如,我想使用第二堂课第一堂课的得分,但我不确定如何做到这一点。

1 个答案:

答案 0 :(得分:1)

如果您的CollegeList活动是从MAinActivity到Intent启动的,您可以使用Intent传递数据。