java - Android ListView not showing but the data from json is there -
the problem list not showing, network connection fine. tried running app several times. 1/10 runs, list there , can see data database. don't know problem i'm using volley 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 menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify 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"); (int = 0; < 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 = new intent(getapplicationcontext(), announcement_details.class); i.putextra("id", tv_id); startactivity(i); } }); } }
i tried logging jsonresponse , data fetched. here's 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 don't know","announcement_id":"3","announcement_date":"2016-02-08"}],"message":"post available!","success":1}
i think problem in population of data in listview
getannouncement() asynchronous operation. have call updatelist() method after request completed.
Comments
Post a Comment