android - Listview won't update from realm -
after create or receive message send message save realm. afterwards need update threads listview on threads page , bring newest messages top. have thread list shows updated preview , updated date, stays in it's inital listview position. tried requery realm info , reorder lastupdated time, doesn't seem work. need wipe old thread list repopulate update?
i have update triggered on onresume()
@override protected void onresume() { super.onresume(); updatelistview = true; updatelist(); }
here's update
@uithread public void updatelist() { try { if (updatelistview) { thread_realm = realm.getinstance(this); results = thread_realm.where(ziplistmodel.class).findallsorted("zipupdated", realmresults.sort_order_descending); adapter = new ziplistadapter(this, results); threadslistview.setadapter(adapter); adapter.notifydatasetchanged(); if (results.size()==0){ createziphint.setvisibility(view.visible); } else { createziphint.setvisibility(view.invisible); } } } catch (exception e) { e.printstacktrace(); } }
adapter
package com.admin.zipline.adapters; import android.content.context; import android.graphics.color; import android.graphics.typeface; import android.support.v7.widget.cardview; import android.util.log; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.textview; import com.admin.zipline.r; import com.admin.zipline.activities.ziplistpage; import com.admin.zipline.model.ziplistmodel; import org.androidannotations.annotations.viewbyid; import java.util.arraylist; import java.util.date; import java.util.list; public class ziplistadapter extends arrayadapter<ziplistmodel> { list<ziplistmodel> items; context context; typeface semibold; typeface light; typeface regular; string[] months={}; public ziplistmodel ziplist; arraylist<string> ziplistnames,ziplistparticipantsaids; public ziplistadapter(context context, list<ziplistmodel> threadslist) { super(context,r.layout.zip_adapter_view,threadslist); this.context = context; this.items=threadslist; } @override public view getview(final int position, view view, viewgroup parent) { viewholder holder ; if (view == null) { holder =new viewholder(); layoutinflater inflater=(layoutinflater)context.getsystemservice(context.layout_inflater_service); view = inflater.inflate(r.layout.zip_adapter_view, parent, false); light = typeface.createfromasset(context.getassets(), "proximanova-light.otf"); regular = typeface.createfromasset(context.getassets(), "proximanova-regular.otf"); semibold = typeface.createfromasset(context.getassets(),"proximanova-semibold.otf"); //todo /*for showing author image * */ //holder.thread_image = (imageview)view.findviewbyid(r.id.author_avatar); holder.thread_text = (textview) view.findviewbyid(r.id.threadtext); holder.thread_name = (textview) view.findviewbyid(r.id.threadname); holder.last_updated = (textview) view.findviewbyid(r.id.lastupdated); holder.zip_members=(textview)view.findviewbyid(r.id.ziplist_members); holder.thread_text.settypeface(light); holder.thread_name.settypeface(semibold); holder.zip_members.settypeface(regular); view.settag(holder); }else{ holder =(viewholder)view.gettag(); } try{ ziplist = items.get(position); ziplistnames = new arraylist<string>(); ziplistparticipantsaids=new arraylist<>(); if (ziplist != null) { if (ziplist.getmessageslistsmodel().first().gettext()!=null){ holder.thread_text.settext(ziplist.getmessageslistsmodel().first().gettext()); } if (ziplist.getmessageslistsmodel().first().getcreatedat()!=null){ holder.last_updated.settext(getdate(ziplist.getmessageslistsmodel().first().getcreatedat())); } (int = 0; < ziplist.getparticipantsmodel().size(); i++) { ziplistnames.add(ziplist.getparticipantsmodel().get(i).getname()); ziplistparticipantsaids.add(ziplist.getparticipantsmodel().get(i).getparticipantid()); } string members=""; (int i=0;i<ziplistnames.size();i++){ members+=ziplist.getparticipantsmodel().get(i).getfirstname()+", "; } if (members.length() > 3){ members=members.substring(0,members.length()-2); } holder.zip_members.settext(members); if(ziplist.getzipname().isempty()){ holder.thread_name.settext(members); } else { holder.thread_name.settext(ziplist.getzipname()); } } } catch (exception e){ e.printstacktrace(); } view.setbackgroundcolor(color.parsecolor(ziplist.getzipcolor())); return view; } string getdate(date date) { try { date d = date; months=context.getresources().getstringarray(r.array.months); return months[d.getmonth()] + " " + (d.getdate()); } catch (exception e) { e.printstacktrace(); return ""; } } public class viewholder{ imageview thread_image; textview thread_text,thread_name,last_updated,zip_members; // cardview cardview; }
}
the question not quite clear me can suggest common ways this.
assume ziplistmodel defined this:
public class ziplistmodel extends realmobject { private string title; private date date; private string author; ... getters , setters... }
the easiest way show models in listview using realmbaseadapter. can find document here. , example.
eg.:
public class ziplistadapter extends realmbaseadapter<ziplistmodel> implements listadapter { public ziplistadapter(context context, int resid, realmresults<ziplistmodel> realmresults, boolean automaticupdate) { super(context, realmresults, automaticupdate); } ... @override public view getview(int position, view convertview, viewgroup parent) { // update views realmresults ... ziplistmodel zlm = realmresults.get(position); authortextview.settext(zlm.getauthor); ... } ... }
the benifit of using realmbaseadapter realmresults can updated automatically when realm transaction commited means in case, whenever update realm
thread_realm = realm.getinstance(this); thread_realm.begintransaction() // change in realm ... thread_realm.committransaction()
the ziplistadapter's notifydatasetchanged called automatically , listview updated.
btw, can still stay own adpater , update using realmchangelistener. see example .
Comments
Post a Comment