android - How to refresh SMS Listview automatically ? -
i creating sms app. sending , receiving sms application , can show them in listview. listview doesn't updated send or receive sms. have press button , after if again go listview activity new sms shown.
how can make listview refresh automatically sms arrives or send ?
code :
public class chatactivity extends listactivity { private mylistadapter adapter; arraylist<string> item_id = new arraylist<string>(); arraylist<string> item_phone_num = new arraylist<string>(); arraylist<string> item_msg_body = new arraylist<string>(); arraylist<string> item_time = new arraylist<string>(); arraylist<string> item_flag = new arraylist<string>(); arraylist<string> items = new arraylist<string>(); private button btn_send; dbmanager manager; cursor cursor; viewholder holder12; string contact_for_chat; string contact_no; string message_body = ""; calendar c; simpledateformat sdf; string time; edittext et_chat; string flag; string msg = ""; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); bundle bundle = getintent().getextras(); contact_for_chat = bundle.getstring("contact_name"); contact_for_chat = contact_for_chat.replace(" ", ""); contact_no = util.getcontactnumber(contact_for_chat, chatactivity.this); toast.maketext(getapplicationcontext(), contact_no, toast.length_long).show(); final viewholder holder = new viewholder(); manager = new dbmanager(this); cursor = manager.return_all(contact_no); showevents(cursor); c = calendar.getinstance(); sdf = new simpledateformat("dd:mmmm:yyyy hh:mm:ss a"); time = sdf.format(c.gettime()); setactionbar(); findviewsbyid(); adapter = new mylistadapter(this); adapter.notifydatasetchanged(); setlistadapter(adapter); btn_send = (button) findviewbyid(r.id.button1); btn_send.setonclicklistener(new onclicklistener() { public void onclick(view v) { sendsms(); } }); } protected void sendsms() { smsmanager sms_manager = smsmanager.getdefault(); message_body = et_chat.gettext().tostring(); arraylist<string> parts = sms_manager.dividemessage(message_body); sms_manager.sendmultiparttextmessage(contact_no, null, parts, null, null); flag = "1"; manager.insert_sms_data(time, contact_no, message_body,flag); msg+= "sms :" + contact_for_chat + " \n"; msg += "having number:" + contact_no + " \n"; msg += "as" +message_body + " \n"; msg += "at"+ time + " \n"; toast.maketext(getapplicationcontext(), ""+msg , toast.length_long).show(); } private void setactionbar() { actionbar mactionbar = getactionbar(); mactionbar.setdisplayhomeasupenabled(false); mactionbar.setdisplayshowtitleenabled(false); layoutinflater minflater = layoutinflater.from(this); view mcustomview = minflater.inflate(r.layout.actionbar_chat, null); textview tv_chat = (textview)mcustomview.findviewbyid(r.id.title_text); tv_chat.settext(contact_for_chat); colordrawable colordaawable = new colordrawable(color.parsecolor("#cfcfc4")); mactionbar.setbackgrounddrawable(colordaawable); mactionbar.setlogo(r.drawable.ic_launcher); mactionbar.setdisplayhomeasupenabled(true); mactionbar.setcustomview(mcustomview); mactionbar.setdisplayshowcustomenabled(true); } private void findviewsbyid() { et_chat = (edittext)findviewbyid(r.id.et_chat); btn_send = (button)findviewbyid(r.id.btn_send); } private void showevents(cursor cursor) { item_id = new arraylist<string>(cursor.getcount()); item_phone_num = new arraylist<string>(cursor.getcount()); item_msg_body = new arraylist<string>(cursor.getcount()); item_time = new arraylist<string>(cursor.getcount()); item_flag = new arraylist<string>(cursor.getcount()); int i=0; while (cursor.movetonext()) { item_id.add(i+""); item_time.add(cursor.getstring(1)); item_msg_body.add(cursor.getstring(3)); item_phone_num.add(cursor.getstring(2)); item_flag.add(cursor.getstring(4)); i++; } } public class mylistadapter extends baseadapter { context con; private layoutinflater layoutinf; arraylist<boolean> itemchecked = new arraylist<boolean>(); arraylist<string> items_ = new arraylist<string>(); public mylistadapter(chatactivity context) { con = context; } public int getcount() { return item_id.size(); } public object getitem(int position) { return item_id.size(); } public long getitemid(int position) { return item_id.get(position).hashcode(); } public view getview(final int position, view arg1, viewgroup arg2) { view v = arg1; viewholder holder = null; if (v == null) { layoutinf = (layoutinflater) getsystemservice(context.layout_inflater_service); v = layoutinf.inflate(r.layout.row_chat, null); holder = new viewholder(); holder.tv_contact = (textview) v.findviewbyid(r.id.phone_num); holder.tv_sms_body = (textview) v.findviewbyid(r.id.msg_body); holder.tv_time = (textview) v.findviewbyid(r.id.time); v.settag(holder); } else { holder = (viewholder) v.gettag(); } if(item_flag.get(position).equals("1")) { holder.tv_sms_body.setbackgroundresource(r.drawable.bubble_green); } else { holder.tv_sms_body.setbackgroundresource(r.drawable.bubble_yellow); } holder.tv_contact.settext("" + item_phone_num.get(position)); holder.tv_sms_body.settext(item_msg_body.get(position)); holder.tv_time.settext(item_time.get(position)); return v; } } public class viewholder { private textview tv_contact; private textview tv_sms_body; private textview tv_time; } }
you have use notifydatasetchanged
. check out following link. https://stackoverflow.com/a/12229903/2930834
Comments
Post a Comment