java - How to display the data from mySQL in listview in android? -
i can insert data database face problem cannot retrieve data database , display in listview in android. here coding part retrieve data. hope can solve this, thank you.
package com.example.iuum; private progressdialog pdialog; private static final string read_comments_url = "http://10.19.229.212/webservice/comments.php"; private static final string tag_success = "success"; private static final string tag_title = "title"; private static final string tag_posts = "posts"; private static final string tag_post_id = "post_id"; private static final string tag_username = "username"; private static final string tag_message = "message"; private jsonarray mcomments = null; private arraylist<hashmap<string, string>> mcommentlist; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_forum1a); } @override protected void onresume() { super.onresume(); new loadcomments().execute(); } public void clickbtnwrite(view v) { intent = new intent(forum1a.this, addforum.class); startactivity(i); } /** * retrieves recent post data server. */ public void updatejsondata() { mcommentlist = new arraylist<hashmap<string, string>>(); jsonparser jparser = new jsonparser(); jsonobject json = jparser.getjsonfromurl(read_comments_url); try { mcomments = json.getjsonarray(tag_posts); (int = 0; < mcomments.length(); i++) { jsonobject c = mcomments.getjsonobject(i); // gets content of each tag string title = c.getstring(tag_title); string content = c.getstring(tag_message); string username = c.getstring(tag_username); hashmap<string, string> map = new hashmap<string, string>(); map.put(tag_title, title); map.put(tag_message, content); map.put(tag_username, username); mcommentlist.add(map); } } catch (jsonexception e) { e.printstacktrace(); } } /** * inserts parsed data listview. */ private void updatelist() { listadapter adapter = new simpleadapter(this, mcommentlist, r.layout.singelpost, new string[] { tag_title, tag_message, tag_username }, new int[] { r.id.title, r.id.message, r.id.username }); setlistadapter(adapter); listview lv = getlistview(); lv.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { } }); } public class loadcomments extends asynctask<void, void, boolean> { @override protected void onpreexecute() { super.onpreexecute(); pdialog = new progressdialog(forum1a.this); pdialog.setmessage("loading comments..."); pdialog.setindeterminate(false); pdialog.setcancelable(true); pdialog.show(); } @override protected boolean doinbackground(void... arg0) { updatejsondata(); return null; } @override protected void onpostexecute(boolean result) { super.onpostexecute(result); pdialog.dismiss(); updatelist(); } } }
add listview in activity_forum1a.xml
<listview android:id="@+id/list_view" android:layout_width="match_parent" android:layout_height="wrap_content"></listview>
take listview reference
private listview mlist;
initialize in oncreate
mlist = (listview) findviewbyid(r.id.list_view);
take listadapter class refernce
private listadapter listadapter;
add setlistadapter method
public void setlistadapter(listadapter listadapter) { this.listadapter = listadapter; mlist.setadapter(listadapter); }
change updatelist method to
/** * inserts parsed data listview. */ private void updatelist() { listadapter adapter = new simpleadapter(this, mcommentlist, r.layout.test, new string[] { tag_title, tag_message, tag_username }, new int[] { r.id.title, r.id.message, r.id.username }); setlistadapter(adapter); }
and perform item click operation outside method, implementing onitemclicklistener
Comments
Post a Comment