android listview refresh data come from server issue -


i have activity have listview data come server.its working fine problem not refresh list automatically want listview update when data come server try not working.for me. please me how here activity code send , receive data server.

  public class datasendactivity extends activity {     private static final string tag = registeractivity.class.getsimplename();     private button button;     private button btnlinktologin;     private edittext edittext;     private edittext inputemail;     private edittext inputpassword;     private progressdialog pdialog;     private sessionmanager session;     private sqlitehandler db;     string rremail = null;      string myjson;      private static final string tag_results = "result";     private static final string tag_data = "data";     private static final string tag_created_at = "created_at";     private static final string tag_sender_email = "sender_email";     private static final string tag_reciver_email = "reciver_email";     private listadapter adapter;     jsonarray peoples = null;     int number_count = 1;     arraylist<hashmap<string, string>> personlist;     string semail, remail, rec, rcvemail,dataa,sender_name;     private listview list;     private textview e1, e2;     private textview rcdata;     private thread t;       @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         requestwindowfeature(window.feature_no_title);         setcontentview(r.layout.activity_data_send);          final listadapter adapter = new simpleadapter(                 datasendactivity.this, personlist, r.layout.layout_chat,                 new string[]{tag_data,tag_created_at},                 new int[]{r.id.data,r.id.created}          );          list.setadapter(adapter);         ((baseadapter)adapter).notifydatasetchanged();       /* final handler handler = new handler();         handler.postdelayed( new runnable() {              @override             public void run() {                     showlist();                 ((baseadapter)adapter).notifydatasetchanged();                 handler.postdelayed( this, 5000 );             }         }, 5000 );   */         db = new sqlitehandler(getapplicationcontext());         e1 = (textview) findviewbyid(r.id.textview);         e2 = (textview) findviewbyid(r.id.textview2);         e2.setvisibility(view.gone);          rcdata = (textview)findviewbyid(r.id.texdata);         rcdata.setvisibility(view.gone);         final bundle extras = getintent().getextras();         if (extras != null) {              remail = extras.getstring("email");             e1.settext(extras.getstring("email"));             rremail = extras.getstring("email");             rcvemail = extras.getstring("rcvemail");             //  e1.settext(extras.getstring("rcvemail"));             dataa = extras.getstring("dataaa");         }           personlist = new arraylist<hashmap<string, string>>();         getdata();        // getdata2();         //getdata3();         //  senddata("hiiiiii","fasial","malik");         list = (listview) findviewbyid(r.id.listview);         //textview = (textview)findviewbyid(r.id.textview);         db = new sqlitehandler(getapplicationcontext());         edittext = (edittext) findviewbyid(r.id.data);         //inputemail = (edittext) findviewbyid(r.id.email);         //inputpassword = (edittext) findviewbyid(r.id.password);         button = (button) findviewbyid(r.id.btns);         //btnlinktologin = (button) findviewbyid(r.id.btnlinktologinscreen);          // progress dialog         pdialog = new progressdialog(this);         pdialog.setcancelable(false);          // session manager         session = new sessionmanager(getapplicationcontext());          // sqlite database handler         db = new sqlitehandler(getapplicationcontext());          // check if user logged in or not         if (session.isloggedin()) {         }          hashmap<string, string> user = db.getuserdetails();          //string name = user.get("name");         semail = user.get("email");         sender_name = user.get("name");          // displaying user details on screen          e2.settext(semail);          button.setonclicklistener(new view.onclicklistener() {             public void onclick(view view) {                 string data = edittext.gettext().tostring().trim();                 final string reciver_email = e1.gettext().tostring().trim();                 final string sender_email = e2.gettext().tostring().trim();                  //edittext.settext(sender_email);                 if (edittext.gettext().tostring().matches("")) {                     edittext.seterror("please enter text");                     return;                 }                 else{                 senddata(sender_name+" "+":"+" "+data, sender_email, reciver_email);                 hashmap<string, string> user_data = db.getdatadetails();             }              }         });     }      private void senddata(final string data, final string sender_email, final string reciver_email) {         // tag used cancel request         string tag_string_req = "req_register";          //   pdialog.setmessage("sending...");         //  showdialog();          stringrequest strreq = new stringrequest(request.method.post,                 appconfig.url_send_data, new response.listener<string>() {              @override             public void onresponse(string response) {                 log.i(tag, "register response: " + response.tostring());                 hidedialog();                  try {                     jsonobject jobj = new jsonobject(response);                     boolean error = jobj.getboolean("error");                     if (!error) {                      } else {                          // error occurred in registration. error                         // message                         string errormsg = jobj.getstring("error_msg");                         toast.maketext(getapplicationcontext(),                                 errormsg, toast.length_long).show();                     }                 } catch (jsonexception e) {                     e.printstacktrace();                 }              }         }, new response.errorlistener() {              @override             public void onerrorresponse(volleyerror error) {                 log.e(tag, "registration error: " + error.getmessage());                 toast.maketext(getapplicationcontext(),                         error.getmessage(), toast.length_long).show();                 hidedialog();             }         }) {              @override             protected map<string, string> getparams() {                 // posting params register url                 map<string, string> params = new hashmap<string, string>();                 params.put("sender_email", sender_email);                 params.put("reciver_email", reciver_email);                 params.put("data", data);                 // toast.maketext(getapplicationcontext(), "message sent!", toast.length_long).show();                 return params;              }          };         // intent intent = new intent(this,datasendactivity.class);         // startactivity(intent);         // toast.maketext(getapplicationcontext(), "message sent!", toast.length_short).show();         // adding request request queue         appcontroller.getinstance().addtorequestqueue(strreq, tag_string_req);     }      private void showdialog() {         if (!pdialog.isshowing())             pdialog.show();     }      private void hidedialog() {         if (pdialog.isshowing())             pdialog.dismiss();         //  intent intent = new intent(this,datasendactivity.class);         //startactivity(intent);         // else{         final string reciver_email = e1.gettext().tostring().trim();         ////  intent intent = new intent(this,datasendactivity.class);          //   startactivity(intent);         //   this.finish();         toast.maketext(getapplicationcontext(), "message sent!", toast.length_short).show();         //}     }        protected void showlist() {         try {             jsonobject jsonobj = new jsonobject(myjson);             peoples = jsonobj.getjsonarray(tag_results);              (int = 0; < peoples.length(); i++) {                 jsonobject c = peoples.getjsonobject(i);                 string data = c.getstring(tag_data);                 string created_at = c.getstring(tag_created_at);                // final string dataaaa = rcdata.gettext().tostring().trim();                 hashmap<string, string> user_data = new hashmap<string, string>();                 user_data.put(tag_data, data);                 user_data.put(tag_created_at, created_at);                 personlist.add(user_data);             }         /*    final listadapter adapter = new simpleadapter(                     datasendactivity.this, personlist, r.layout.layout_chat,                     new string[]{tag_data,tag_created_at},                     new int[]{r.id.data,r.id.created}              );              list.setadapter(adapter);  */         } catch (jsonexception e) {             e.printstacktrace();         }      }      public void getdata() {         class getdatajson extends asynctask<string, void, string> {             //string recid = ;             //string userid = email;             @override             protected string doinbackground(string... params) {                 defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams());                 hashmap<string, string> user = db.getuserdetails();                  //string name = user.get("name");                // semail = user.get("email");                  string semail  = user.get("email");                 final string remail = rremail;                  httppost httppost = new httppost("http://samplechatapp.gear.host/myphpfile.php?sender_email="+semail+"&reciver_email="+remail+"&fatch_server_data=true");                  // depends on web service                 httppost.setheader("content-type", "application/json");                  inputstream inputstream = null;                 string result = null;                 log.i("","processing entity");                 try {                     httpresponse response = httpclient.execute(httppost);                     httpentity entity = response.getentity();                      inputstream = entity.getcontent();                     // json utf-8 default                     bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8);                     stringbuilder sb = new stringbuilder();                      string line = null;                     while ((line = reader.readline()) != null) {                         sb.append(line + "\n");                         log.i("",line);                     }                     result = sb.tostring();                     log.i("",result);                 } catch (exception e) {                     // oops                 } {                     try {                         if (inputstream != null) inputstream.close();                     } catch (exception squish) {                     }                 }                 return result;             }              @override             protected void onpostexecute(string result) {                 myjson = result;                 showlist();             }         }         getdatajson g = new getdatajson();         g.execute();     } 

i have try method app crash.

final handler handler = new handler();     handler.postdelayed( new runnable() {          @override         public void run() {                 showlist();             ((baseadapter)adapter).notifydatasetchanged();             handler.postdelayed( this, 5000 );         }     }, 5000 ); 

here logcat. enter image description here

try implement line of code in oncreate method

 adapter = new simpleadapter(                 datasendactivity.this, personlist, r.layout.layout_chat,                 new string[]{tag_data,tag_created_at},                 new int[]{r.id.data,r.id.created}          );          list.setadapter(adapter); 

and after getting result server write this

adapter.notifydatasetchanged(); 

and note notifydatasetchanged not need handler

and delcare list = (listview) findviewbyid(r.id.listview); before adapter declaration


Comments

Popular posts from this blog

scala - 'wrong top statement declaration' when using slick in IntelliJ -

c# - DevExpress.Wpf.Grid.InfiniteGridSizeException was unhandled -

PySide and Qt Properties: Connecting signals from Python to QML -