android - Json array is not corretly showing in listview -
output:
i want show json array in fragment listview. have created class getuser
:
package com.gmakerorganisation.glocator.fragments; import android.app.progressdialog; import android.os.asynctask; import android.os.bundle; import android.support.v4.app.fragment; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.listadapter; import android.widget.listview; import android.widget.simpleadapter; import com.android.volley.requestqueue; import com.gmakerorganisation.glocator.config; import com.gmakerorganisation.glocator.getallusers; import com.gmakerorganisation.glocator.r; import org.json.jsonarray; import org.json.jsonexception; import org.json.jsonobject; import java.util.arraylist; import java.util.hashmap; public class sendrequest extends fragment { private requestqueue requestqueue; arraylist<hashmap<string, string>> userlist; private static final string tag_id = "id"; private static final string tag_username = "username"; private static final string tag_name = "name"; private static final string tag_phone = "phone"; private static final string tag_profile = "profile"; getallusers webreq = new getallusers(); listview listview; public sendrequest() { // required empty public constructor } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { // inflate layout fragment view v=inflater.inflate(r.layout.fragment_sendrequest, container, false); listview=(listview)v.findviewbyid(r.id.list); new getstudents().execute(); return v; } private class getstudents extends asynctask<void, void, void> { // hashmap listview progressdialog pdialog; @override protected void onpreexecute() { super.onpreexecute(); } @override protected void doinbackground(void... arg0) { // creating service handler class instance getallusers webreq = new getallusers(); // making request url , getting response string jsonstr = webreq.makewebservicecall(config.users_url, getallusers.get); log.d("response: ", "> " + jsonstr); userlist = parsejson(jsonstr); return null; } @override protected void onpostexecute(void result) { super.onpostexecute(result); // dismiss progress dialog /** * updating parsed json data listview li * */ listadapter adapter = new simpleadapter( getactivity().getapplicationcontext(), userlist, r.layout.list_item, new string[]{tag_name, tag_phone}, new int[]{r.id.name, r.id.mobile}); listview.setadapter(adapter); } } private arraylist<hashmap<string, string>> parsejson(string json) { if (json != null) { try { // hashmap listview arraylist<hashmap<string, string>> studentlist = new arraylist<hashmap<string, string>>(); // getting json array node jsonarray userrs = new jsonarray(json); // looping through students (int = 0 ; < userrs.length(); i++) { jsonobject c = userrs.getjsonobject(i); string id = c.getstring(tag_id); string username = c.getstring(tag_username); string name = c.getstring(tag_name); string phone = c.getstring(tag_phone); string profile = c.getstring(tag_profile); // tmp hashmap single student hashmap<string, string> user = new hashmap<string, string>(); // adding each child node hashmap key => value user.put(tag_id, id); user.put(tag_username, username); user.put(tag_name, name); user.put(tag_phone, phone); user.put(tag_profile, profile); // adding student students list studentlist.add(user); } return studentlist; } catch (jsonexception e) { e.printstacktrace(); return null; } } else { log.e("servicehandler", "couldn't data url"); return null; } } }
i getting correct data in userlist, think there problem in listview. r.layout.list_item
:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- name label --> <textview android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:paddingtop="6dip" android:text="name" android:textsize="16sp" android:textstyle="bold" /> <!-- mobile number label --> <textview android:id="@+id/mobile" android:layout_width="220dp" android:layout_height="wrap_content" android:gravity="left" android:text="phone: " /> </linearlayout>
and json this:
[{"id":"1","username":"gmaker","name":"shubham sharma","phone":"9711979977","profile":"http:\/\/glocator.esy.es\/profilepicture\/1."},{"id":"4","username":"somnath","name":"","phone":"9582223881","profile":""},{"id":"7","username":"shitij","name":"","phone":"9650154839","profile":""}]
use madapter.notifydatasetchanged()
when update list update adapter :
userlist = parsejson(jsonstr); madapter.notifydatasetchanged();
with adapter initialized :
madapter = new simpleadapter( getactivity().getapplicationcontext(), userlist, r.layout.list_item, new string[]{tag_name, tag_phone}, new int[]{r.id.name, r.id.mobile}); listview.setadapter(adapter);
Comments
Post a Comment