Alvin Calagos February 2016

Android ListView not showing but the data from json is there

The problem is sometimes the list is not showing, my network connection is fine. I tried running the app several times. 1/10 runs, the list is there and i can see the data from the database. i really don't know the problem I'm using volley to get data

package com.example.wackyroad.internannouncement;


public class MainActivity extends ListActivity {

private static final String GET_URL = "http://XXX";
private static final String TAG_ID = "id";
private static final String TAG_TITLE = "title";
private static final String TAG_CONTENT = "content";
private static final String TAG_DATE = "date";
private ArrayList<HashMap<String, String>> listArrayList = new ArrayList<HashMap<String, String>>();
private ProgressDialog pDialog;
private ListAdapter adapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    getAnnouncement();
    updateList();

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.refresh) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}


private void getAnnouncement() {
    StringRequest postRequest = new StringRequest(Request.Method.POST, GET_URL,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {

                    try {
                        JSONObject jsonResponse = new JSONObject(response);

        

Answers


ersinyildiz February 2016

getAnnouncement() is asynchronous operation. You have to call updateList() method after your request completed.


Mobile Apps Expert February 2016

call updateList() after updating hashmap like

                          try {
                            JSONArray data = jsonResponse.getJSONArray("announcements");

                            for (int i = 0; i < data.length(); i++) {
                                JSONObject c = data.getJSONObject(i);

                                HashMap<String, String> map = new HashMap<String, String>();
                                map.put(TAG_ID, c.getString("announcement_id"));
                                map.put(TAG_TITLE,  c.getString("announcement_title"));
                                map.put(TAG_CONTENT, c.getString("announcement_content"));
                                map.put(TAG_DATE, c.getString("announcement_date"));
                                listArrayList.add(map);
                            }
                            updateList();
                        } catch (Exception e) {
                            e.printStackTrace();

                        }

Post Status

Asked in February 2016
Viewed 2,219 times
Voted 4
Answered 2 times

Search




Leave an answer