I am trying to learn about MVVM so i am building an app based on this principle, I understand that i need to separate functionalities under different packages, such as model, view, viewmodel. What i am trying to do here is populate my HighSchoolDataModel, from my class HighSchoolDataModleProvider. Issue, i am having is i do not know how to instantiate volley, in this class. I know ordinarily you would so something such as RequestQueue queue = Volley.newRequestQueue(this);
But when trying to do this here i get a message that reads cannot resolve constructor, can someone help me out with this?
public class HighSchoolModelDataProvider {
private ArrayList<HighSchoolModel> highSchoolModelArrayList = new ArrayList<>();
private HighSchoolModel highSchoolModel;
private JSONObject highSchoolJsonData;
private RequestQueue requestQueue = new RequestQueue(this);
// Initialize a new JsonArrayRequest instance
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(
Request.Method.GET,
"",
null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
highSchoolJsonData = response.getJSONObject(i);
highSchoolModel = new HighSchoolModel(highSchoolJsonData.getString("school_name"), highSchoolJsonData.getString("overview_paragraph"), highSchoolJsonData.getString("location"));
highSchoolModelArrayList.add(highSchoolModel);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("error", error.toString());
}
}
);
// Add JsonArrayRequest to the RequestQueue
requestQueue.add(jsonArrayRequest);
}
Yo, you made a small mistake.
private RequestQueue requestQueue = new RequestQueue(this);
this
in above code represents context
of component from which you are making requestQueue
object. context
object is present in activity/fragment etc. You need to pass it to constructor.
If you got the answer you want please accept my answer by giving green check. If I haven't got your question then give me more details in comment section of this answer. Love from India.❤️
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.