Reputation: 99
I am trying to create a simple android app, where I get data from JSON and add it custom rows in a ListView.
I do get the data from the JSON but none of the rows are showing. I figured out that is because the rows are "created" before the JSON has finished downloading. How do I make the ListView update after the JSON has downloaded?
public class MainActivity extends AppCompatActivity {
private RequestQueue mQueue;
private static final String TAG = "MainActivity";
String isDoneLoading = "false";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "LOG STARTED");
mQueue = Volley.newRequestQueue(this);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<News> newsList = new ArrayList<>();
// Create news objects
jsonParse(newsList);
NewsListAdapter adapter = new NewsListAdapter(this, R.layout.row, newsList);
listView.setAdapter(adapter);
Log.d(TAG, "RELOADING LISTVIEW");
}
private void jsonParse(final ArrayList<News> newsArray) {
String url = "https://api.myjson.com/bins/kp9wz";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("employees");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject news_feed = jsonArray.getJSONObject(i);
String headline = news_feed.getString("firstname");
String category = news_feed.getString("mail");
Log.i(TAG, headline);
News newsData = new News(headline, category);
newsArray.add(newsData);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}
Upvotes: 0
Views: 85
Reputation: 2217
Currently you are not updating the Adapter after adding the new elements. Pass the adapter variable to the jsonParse method too. The content of the listview changes only when the adapter is notified of those changes in the arraylist used in the adapter. I your onCreate method
NewsListAdapter adapter = new NewsListAdapter(this, R.layout.row, newsList);
listView.setAdapter(adapter);
jsonParse(adapter, newsList);
Then in your onResponse method inside jsonParse method
try {
JSONArray jsonArray = response.getJSONArray("employees");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject news_feed = jsonArray.getJSONObject(i);
String headline = news_feed.getString("firstname");
String category = news_feed.getString("mail");
Log.i(TAG, headline);
News newsData = new News(headline, category);
newsArray.add(newsData);
}
// Notify the adapter after the for loop
adapter.notifyDataSetChanged()
} catch (JSONException e) {
e.printStackTrace();
}
Upvotes: 0
Reputation: 385
Try with this changes
private RequestQueue mQueue;
private static final String TAG = "MainActivity";
String isDoneLoading = "false";
private NewsListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d(TAG, "LOG STARTED");
mQueue = Volley.newRequestQueue(this);
ListView listView = (ListView) findViewById(R.id.listView);
ArrayList<News> newsList = new ArrayList<>();
// Create news objects
jsonParse(newsList);
adapter = new NewsListAdapter(this, R.layout.row, newsList);
listView.setAdapter(adapter);
Log.d(TAG, "RELOADING LISTVIEW");
}
private void jsonParse(final ArrayList<News> newsArray) {
String url = "https://api.myjson.com/bins/kp9wz";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("employees");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject news_feed = jsonArray.getJSONObject(i);
String headline = news_feed.getString("firstname");
String category = news_feed.getString("mail");
Log.i(TAG, headline);
News newsData = new News(headline, category);
newsArray.add(newsData);
}
updateList();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
private void updateList(){
if(adapter != null){
adapter.notifyDataSetChanged();
}
}
Upvotes: 2