在onClick中调用公共类

时间:2017-07-07 11:27:36

标签: java android

我有一个onClick事件:

   @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mPrayerTime = (Button)findViewById(R.id.muteButton);
    mPrayerTime.setOnClickListener(new  View.OnClickListener() {

        @Override
        public void onClick(View v) {
            //call CityModelHere
        }
    });

我想调用这个类(并且特别是获取fajr变量而不返回null,因为如果我在onClick中实例化一个新的cityModel它会这样做):

 public class cityModel implements Serializable {
    private String fajr;



    public void setFajr(String fajr) {
        this.fajr= fajr;

    }
    public String getFajr() {

        return fajr;
    }
}

该类从以后的类中获取字符串以防万一:

        protected void outputTimings(JSONArray jsonArray) {
        String[] prayers = {"fajr", "shurooq", "dhuhr", "asr", "maghrib", "isha"};
        cityModel cityObj;
        try {
            cityObj= new cityModel();
            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject cityObject =
                        jsonArray.getJSONObject(i);
                result = result + "fajr" + " : "
                        + cityObject.getString("fajr") + "\n" + result + "shurooq" + " : "
                        + cityObject.getString("shurooq") + "\n" + result + "dhuhr" + " : "
                        + cityObject.getString("dhuhr") + "\n" + result + "asr" + " : "
                        + cityObject.getString("asr") + "\n" + result + "maghrib" + " : "
                        + cityObject.getString("maghrib") + "\n" + result + "isha" + " : "
                        + cityObject.getString("isha") + "\n";

                cityObj.setFajr(""+cityObject.getString("fajr"));


            }

        } catch (JSONException e1) {
            e1.printStackTrace();
        }

    }

1 个答案:

答案 0 :(得分:0)

将变量设为final,然后可以从setOnClickListener访问它。