Reputation: 215
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);
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);
}
} catch (Exception e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
pDialog.dismiss();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
pDialog.dismiss();
}
}
) {
};
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Announcements..");
pDialog.show();
Volley.newRequestQueue(getApplication()).add(postRequest);
}
private void updateList() {
adapter = new SimpleAdapter(this, listArrayList,
R.layout.activity_lv_list, new String[]{TAG_ID, TAG_TITLE, TAG_CONTENT,
TAG_DATE}, new int[]{R.id.tv_id, R.id.tv_title, R.id.tv_content, R.id.tv_date});
setListAdapter(adapter);
ListView lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
String tv_id = ((TextView) view.findViewById(R.id.tv_id)).getText().toString();
Intent i = new Intent(getApplicationContext(), Announcement_Details.class);
i.putExtra("id", tv_id);
startActivity(i);
}
});
}
}
I tried logging the jsonResponse and the data is actually fetched. here's the jsondata
02-08 10:34:38.676 6258-6258/com.example.wackyroad.internannouncement D/JSONRESPONSE﹕ {"announcements":[{"announcement_title":"Sample Title Here","announcement_content":"Sample Content","announcement_id":"1","announcement_date":"2016-02-04"},{"announcement_title":"Sample Title","announcement_content":"Sample Content Again","announcement_id":"2","announcement_date":"2016-02-04"},{"announcement_title":"It's not working","announcement_content":"I really don't know","announcement_id":"3","announcement_date":"2016-02-08"}],"message":"Post Available!","success":1}
I think the problem is in the population of data in listview
Upvotes: 0
Views: 275
Reputation: 1048
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();
}
Upvotes: 0
Reputation: 338
getAnnouncement() is asynchronous operation. You have to call updateList() method after your request completed.
Upvotes: 4