Skip to content Skip to sidebar Skip to footer

Confused On How To Make Variable In Class?

I have a class (public class SaveTheFeed extends AsyncTask {) and within the class I have a protected void that gets and interprets certain JSON info. I want to save different port

Solution 1:

Well @Fencer300,

Simply make a simple bean class (Model class with getter setter methd )

// make a city bean class
public class cityModel implements Serializable {
private String fajr;

// do for more same ass

public String getFajr() {
    return fajr;
}

public void setFajr(String fajr) {
    this.fajr= 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);
            // for(int z=0; z < cityObject.length(); z++) {
            cityObj.setFajr(""+cityObject.getString("fajr"))
            // do for more same as

        }
}

Note easily now you can use fajr value , do for same foe all


Solution 2:

Making the string the return value from the asynctask should make it available to the recursive class above. I know it partly defeats the reason for asynctask, but you may need to use

AsyncTask().execute(string, string).get();

to make class wait for return value, else I guess possible risks of race condition. I hope this helps.

Relevant Android docs, see get():

https://developer.android.com/reference/android/os/AsyncTask.html


Post a Comment for "Confused On How To Make Variable In Class?"