android - ListView with custom arrayadapter won't update properly after call to onDestroy() then onCreate() -
i have listview custom array adapter:
public class smsmessageadapter extends arrayadapter<contactmessage> { private arraylist<contactmessage> items; public smsmessageadapter(context context, int textviewresourceid, arraylist<contactmessage> items) { super(context, textviewresourceid, items); this.items = items; } @override public view getview(int position, view convertview, viewgroup parent) { view v = convertview; if(v == null) { layoutinflater vi = (layoutinflater)getcontext().getsystemservice(context.layout_inflater_service); v = vi.inflate(r.layout.message, null); } contactmessage c = items.get(position); if(c != null) { string name = c.getcontact(); if(name.equals("me")) v.setbackgroundresource(r.drawable.sms_history_outgoing_background_gradient); else v.setbackgroundresource(r.drawable.sms_history_incoming_background_gradient); textview contactname = (textview) v.findviewbyid(r.id.message_contact_name); textview body = (textview) v.findviewbyid(r.id.message_body); textview date = (textview) v.findviewbyid(r.id.message_date_time); if(contactname != null) { contactname.settext(name); } if(body != null) { body.settext(c.getbody()); } if(date != null) { date.settext(c.getformattedtimestring()); } } return v; } }
i have method in main activity updates listview adding items onto array list associated custom array adapter:
private void displaycontactsmshistory(string phonenumber) { contact c = new contact(); for(contact contact : mcontactsarraylist) { if(getonlynumerics(phonenumber).equals(getonlynumerics(contact.getphonenumber()))) { c = contact; } } hashmap<string, boolean> unreadpositions = mcontactsadapter.getunreadmap(); unreadpositions.put(getonlynumerics(phonenumber), false); mcontactsadapter.setunreadmap(unreadpositions); hashmap<string, boolean> selectedposition = mcontactsadapter.getselectedmap(); for(string key : selectedposition.keyset()) { selectedposition.put(key, false); } selectedposition.put(getonlynumerics(phonenumber), true); mcontactsadapter.setselectedmap(selectedposition); string contactname; if(c.hasname()) contactname = c.getname(); else contactname = phonenumber; mconversationarrayadapter.clear(); if(!db.isopen()) db = db.open(); cursor smshistory = db.getsmsforcontact(phonenumber); startmanagingcursor(smshistory); int bodyindex = smshistory.getcolumnindex(dbadapter.key_body); int dateindex = smshistory.getcolumnindex(dbadapter.key_date); int typeindex = smshistory.getcolumnindex(dbadapter.key_type); if(smshistory.movetofirst()) { while(!smshistory.isafterlast()) { string body = smshistory.getstring(bodyindex); string datestring = smshistory.getstring(dateindex); dateformat df = dateformat.getdatetimeinstance(dateformat.long, dateformat.long); date date; try { date = df.parse(datestring); } catch (parseexception e) { log.e(tag, "error parsing date string while displaying contact info:", e); date = new date(0); } if(smshistory.getint(typeindex) == dbadapter.received) { smshistoryarraylist.add(new contactmessage(date, contactname, body)); } else { smshistoryarraylist.add(new contactmessage(date, null, body)); } smshistory.movetonext(); } } stopmanagingcursor(smshistory); if(db.isopen()) db.close(); }
the update method can called 2 different places in code; 1) if user selects item listview, , 2) if call method directly in situations. works find , dandy, except in situation user presses button (which initiates call ondestroy()
), returns app (causing new call oncreate()
). if happens, listview update 1 place in code (if user selects item in other listview). other situations update method called, code executed listview not update. i've run through debug tool, watching variables inside update method , can't see reason why listview not updating.
also note, if user presses home button, instead of button, , returns app, works fine. in situation ondestroy()
, oncreate()
never called, onstop()
, onstart()
.
if necessary can post oncreate()
, ondestroy()
, can there nothing in 2 methods believe causing error (oncreate()
loads preferences , sets layout, , ondestroy()
closes database).
there several way achieve this.
- make arraylist variable local (declare in side function).
- clear arraylist variable
i hope helpful you
Comments
Post a Comment