My question is simple. I have a simple fragment which is not a list but simply 5 TextViews. Once this fragment runs, it fires a volley request to an API endpoint where its response arrives after the onCreateView method has already finished. Normally, one would use adapter.notifyDataSetChanged() so that the new data could be shown even if it arrives after the normal fragment lifecycles have ended. The problem is that since my fragment is not a list, it does not have an adapter. How could I use notifyDataSetChanged() in the absence of an adapter? What solution should I be looking for to solve this problem? Any feedback would be greatly appreciated.
Assuming you have a field of
private TextView textView1;
And somewhere you have a StringRequest
for Volley, then in the onResponse
for the Response.Listener
, just set the text of the TextView.
public void onResponse(String response) {
textView1.setText(response);
}
If you have a seprate class that does your Volley requests, then you should add a parameter for Response.Listener
to your method.
For example
MyServer.doLogin("http://myurl.com/login", "username:password", new Response<String>.Listener() {
public void onResponse(String response) {
// Do something here
}
};
Where MyServer.doLogin
could be something like this (note: this is an example, it may not compile)
public void doLogin(String url, String cred, Response<String>.Listener callback) {
StringRequest request = new StringRequest(
url,
Request.GET,
callback,
new Response.ErrorListener() { ... }
);
addToRequestQueue(request);
}