android - Error loading data into listvcoiew -
i tryinh load data listview remote server using custom baseadapter class.everything working fine except data not displayed on listview.no error generated on log cat. below code:
activity class:
public class myaccountactivity extends activity{ public static final string url="http://***.co.ke/ussd/get_shares.php"; private progressdialog progressdialog; private static final string tag_number = "share_number"; private static final string tag_name = "share_name"; arraylist sharelist; public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my_account); arraylist sharelist = update_shares(); final listview lv1 = (listview) findviewbyid(r.id.mylist); lv1.setadapter(new customsharelistadapter(this, sharelist)); //listview listview = getlistview(); } string data=""; public arraylist update_shares(){ inputstream is=null; string result=""; string key="admin"; arraylist results=new arraylist(); arraylist<namevaluepair>user_details=new arraylist<namevaluepair>(); user_details.add(new basicnamevaluepair("user",key)); try{ httpclient httpclient=new defaulthttpclient(); httppost httppost=new httppost(url); httppost.setentity(new urlencodedformentity(user_details)); httpresponse response=httpclient.execute(httppost); httpentity entity=response.getentity(); is=entity.getcontent(); } catch(exception e){ log.e("log tag","error in connection"+e.tostring()); } try{ bufferedreader reader=new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb=new stringbuilder(); string line=null; while((line=reader.readline()) != null){ sb.append(line + "\n"); } is.close(); result=sb.tostring(); } catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } try{ jsonarray jarray=new jsonarray(); for(int i=0;i<jarray.length();i++){ jsonobject json_data = jarray.getjsonobject(i); string share=json_data.getstring(tag_name); string number=json_data.getstring(tag_number); sharelist myshares=new sharelist(); myshares.set_share_name(share); myshares.set_share_number(number); results.add(myshares); } toast.maketext(getapplicationcontext(), "parsed ok",toast.length_short).show(); } catch(exception e){ log.e("log_tag", "error parsing data "+e.tostring()); } return results; } }
the custom adapter class:
public class customsharelistadapter extends baseadapter { private arraylist listdata; private layoutinflater layoutinflater; public customsharelistadapter(context context, arraylist listdata) { this.listdata = listdata; layoutinflater = layoutinflater.from(context); } @override public int getcount() { return listdata.size(); } @override public object getitem(int position) { return listdata.get(position); } @override public long getitemid(int position) { return position; } public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = layoutinflater.inflate(r.layout.share_list, null); holder = new viewholder(); holder.nameview = (textview) convertview.findviewbyid(r.id.company); holder.numberview = (textview) convertview.findviewbyid(r.id.share_number); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } sharelist sharelist = (sharelist)listdata.get(position); holder.nameview.settext(sharelist.get_share_name()); holder.numberview.settext(sharelist.get_share_number()); return convertview; } static class viewholder { textview nameview; textview numberview; } }
and sharelist class;
public class sharelist { private string share_name; private string share_number; public string get_share_name(){ return share_name; } public void set_share_name(string share_name){ this.share_name=share_name; } public string get_share_number(){ return share_number; } public void set_share_number(string share_number){ this.share_number=share_number; } public string to_string(){ return "[name="+share_name+",number="+share_number+"]"; } }
help highly appreciated.thank you.
import java.io.bufferedreader; import java.io.inputstream; import java.io.inputstreamreader; import java.util.arraylist; import org.apache.http.httpentity; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.httpclient; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.defaulthttpclient; import org.apache.http.message.basicnamevaluepair; import org.json.jsonarray; import org.json.jsonobject; import android.app.activity; import android.app.progressdialog; import android.os.bundle; import android.util.log; import android.widget.listview; import android.widget.toast; public class myaccountactivity extends activity{ public static final string url="http://***.co.ke/ussd/get_shares.php"; private progressdialog progressdialog; private static final string tag_number = "share_number"; private static final string tag_name = "share_name"; arraylist<sharelist> sharelist=new arraylist<sharelist>(); public void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.activity_my_account); sharelist = update_shares(); final listview lv1 = (listview) findviewbyid(r.id.mylist); lv1.setadapter(new customsharelistadapter(this, sharelist)); //listview listview = getlistview(); } string data=""; public arraylist<sharelist> update_shares(){ inputstream is=null; string result=""; string key="admin"; arraylist<namevaluepair>user_details=new arraylist<namevaluepair>(); user_details.add(new basicnamevaluepair("user",key)); try{ httpclient httpclient=new defaulthttpclient(); httppost httppost=new httppost(url); httppost.setentity(new urlencodedformentity(user_details)); httpresponse response=httpclient.execute(httppost); httpentity entity=response.getentity(); is=entity.getcontent(); } catch(exception e){ log.e("log tag","error in connection"+e.tostring()); } try{ bufferedreader reader=new bufferedreader(new inputstreamreader(is,"iso-8859-1"),8); stringbuilder sb=new stringbuilder(); string line=null; while((line=reader.readline()) != null){ sb.append(line + "\n"); } is.close(); result=sb.tostring(); } catch(exception e){ log.e("log_tag", "error converting result "+e.tostring()); } try{ jsonarray jarray=new jsonarray(); for(int i=0;i<jarray.length();i++){ jsonobject json_data = jarray.getjsonobject(i); string share=json_data.getstring(tag_name); string number=json_data.getstring(tag_number); sharelist.add(new sharelist(share, number)); } toast.maketext(getapplicationcontext(), "parsed ok",toast.length_short).show(); } catch(exception e){ log.e("log_tag", "error parsing data "+e.tostring()); } return sharelist; } }
change activity above
public class sharelist { private string share_name; private string share_number; public string getshare_name() { return share_name; } public void setshare_name(string share_name) { this.share_name = share_name; } public string getshare_number() { return share_number; } public void setshare_number(string share_number) { this.share_number = share_number; } public sharelist(string share_name, string share_number) { super(); this.share_name = share_name; this.share_number = share_number; } @override public string tostring() { return "sharelist [share_name=" + share_name + ", share_number=" + share_number + "]"; } }
change sharelist above works
Comments
Post a Comment