Reputation: 119
Am experiencing a strange problem here, I got the GET response using volley and loading the data to the recycler view; the data however not reflecting in view unless I lock and unlock the phone once, while the application is running. Then I tried the same response locally storing into asset folder,its working superfine. Am not getting any clue on this, the GET responses are showing in the logger; I've tried the same with Listview too, but the result was same always. Please help me on this....
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newsFeed = engine();
rv = (RecyclerView) findViewById(R.id.newsItems);
rv.setHasFixedSize(true);
llm = new LinearLayoutManager(this);
rv.setLayoutManager(llm);
initializeAdapter(newsFeed);
}
private void initializeAdapter(ArrayList<newsItem> newsFeed) {
context = getApplicationContext();
NewsViewAdapter adapter = new NewsViewAdapter(context, newsFeed);
rv.setAdapter(adapter);
}
private ArrayList<newsItem> engine() {
//String res=loadJSONFromAsset();
RequestQueue queue = Volley.newRequestQueue(this);
final ArrayList<newsItem> reqs = new ArrayList<>();
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"http://content.guardianapis.com/search?page=20&q=business%20OR%20sport&show-fields=thumbnail&api-key={mykey}",
null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// longInfo(response.toString());
JSONObject newsItems = response.getJSONObject("response");
JSONArray webNews = newsItems.getJSONArray("results");
int a = webNews.length();
for (int i = 0; i < a; i++) {
JSONObject temp = webNews.getJSONObject(i);
String title = temp.getString("webTitle");
Log.d("mytitile", title);
// String time = temp.getString("time");
String date = temp.getString("webPublicationDate");
String content = temp.getString("id");
Log.d("myContent", content);
String link = temp.getString("webUrl");
JSONObject temp_img = temp.getJSONObject("fields");
String image = temp_img.getString("thumbnail");
Log.d("imageUrl", image);
reqs.add(new newsItem(title, content, date, link, image));
}
} catch (JSONException e) {
Log.i("myTag", e.toString());
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("myTag", error.toString());
}
});
myReq.setRetryPolicy(new DefaultRetryPolicy(
10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
));
queue.add(myReq);
Upvotes: 1
Views: 718
Reputation: 2178
Use this:
in your fields:
private ArrayList<newsItem> items = new ArrayList<>();
private NewsViewAdapter adapter;
in your initializeAdapter:
adapter = new NewsViewAdapter(context, items);
and in your onResponse, instead of reqs.add
items.add(new newsItem(parameters..));
after your for loop in the onResponse, you call
adapter.notifyDataSetChanged();
and you're good to go
Upvotes: 4
Reputation: 86948
Your Adapter is unaware that the data set has changed, until you force it to check again by turning the screen on and off. (I'm assuming the data will update if you rotate your phone as well.)
One solution is to notify your Adapter that the data has changed manually by calling notifyDataSetChanged()
First make NewsViewAdapter adapter
a global variable then:
adapter.notifyDataSetChanged();
after you for
loop in onResponse()
Upvotes: 1