listview - Android BaseAdapter notifyDataSetChanged() sometimes works and sometimes doesn't -



listview - Android BaseAdapter notifyDataSetChanged() sometimes works and sometimes doesn't -

before begin, checked other answers in stackoverflow.com. sorry bringing same old notifydatasetchanged()-doesn't-work stuff.

if find answer, reply own question.

hi, having difficulty in using listviewadapter extends baseadapter see below. there lot of talks on issue in stackoverflow.com or many other blogs. solution seems converge saying

1) don't forget set setlistadapter. 2) utilize notifydatasetchanged() when new info added. (some utilize notifydatasetinvalidate ... )

the tutorial have been studying

http://adilsoomro.blogspot.kr/2012/12/android-listview-with-speech-bubble.html

.

the problem works doesn't.

there no pattern.

since messages transmitted other system, thought due network problem. messages seamlessly coming app.

the reason have not used arraylist instance on listviewactivity point of scheme operations occur case arraylist instance within listviewadapter fails sync arraylist on listviewactivity. , i've confirmed there starts exist moment messages set list a(within listviewactivty) , other messages list b(within listviewadapter not sure if case). that's why removed arraylist within listviewactivty.

update done within onpostexecute part of asynctask, not using runuithread (there no difference bewteen using runuithread , not).

and i've found when list view not updated, getview() not called either, though message list in adapter had added info , getcount() returns exact number of refreshed info size.

i uncertainty operation within onpostexecute 1 time in while blocked unknown process.

when give ui event( example, entering text ) on app, gives late refresh of added info on screen should have been done when addnewmessage() called().

=======================================================================

1. asyctask in addnewmessage() called ( part of listviewactivity.java ) private class refreshlistview extends asynctask<argument, string, argument> { @override protected argument doinbackground(argument... _args) { argument result = new argument(); result.setmessagestring( _args[0].getmessagestring() ); result.setismine( _args[0].getismine() ); homecoming result; } @override public void onprogressupdate(string... v) { } @override protected void onpostexecute(argument _arg ) { addnewmessage(new message( _arg.getmessagestring(), _arg.getismine() ) ); } }

=======================================================================

2. point notifydatasetchanged() called. ( part of listviewactivity.java ) public void addnewmessage( final message _message ) { adapter.add( _message ); adapter.notifydatasetchanged(); getlistview().setselection( adapter.observemessagelist().size() - 1 ); }

=======================================================================

3. listviewactivity setting listviewadapter observer public void oncreate( bundle savedinstancestate ) { adapter = new listviewadapter( ); this.setlistadapter( adapter ); }

=======================================================================

4. listviewadapter.java has actual arraylist stores info item import java.util.arraylist; import java.util.iterator; import com.test.r; import android.content.context; import android.util.log; import android.view.gravity; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.linearlayout.layoutparams; import android.widget.textview; // http://stackoverflow.com/questions/3669325/notifydatasetchanged-example public class listviewadapter extends baseadapter { private context mcontext; public static arraylist<com.test.message> mmessages = new arraylist<com.test.message>(); public listviewadapter(context context ) { super(); this.mcontext = context; } public void clearmessages() { listviewadapter.mmessages.clear(); } public void add( message _message ) { listviewadapter.mmessages.add( _message ); } public arraylist<com.test.message> observemessagelist() { homecoming listviewadapter.mmessages; } @override public int getcount() { homecoming mmessages.size(); } @override public object getitem( int position ) { homecoming mmessages.get( position ); } @override public view getview( int position, view convertview, viewgroup parent ) { com.test.message message = (com.test.message) this.getitem(position); viewholder holder; if ( convertview == null ) { holder = new viewholder(); convertview = layoutinflater.from(mcontext).inflate(r.layout.sms_row, parent, false); holder.message = (textview) convertview.findviewbyid(r.id.message_text); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.message.settext( message.getmessage() ); layoutparams lp = (layoutparams) holder.message.getlayoutparams(); // check if status message remove background, , alter text color. if ( message.isstatusmessage() ) { holder.message.setbackgrounddrawable(null); lp.gravity = gravity.left; // holder.message.settextcolor( r.color.textfieldcolor ); } else { // check whether message mine show greenish background , align right if ( message.ismine() ) { holder.message.setbackgroundresource(r.drawable.speech_bubble_green); lp.gravity = gravity.right; } // if not mine sender show orange background , align left else { holder.message.setbackgroundresource(r.drawable.speech_bubble_orange); lp.gravity = gravity.left; } holder.message.setlayoutparams(lp); // holder.message.settextcolor( r.color.textcolor ); } homecoming convertview; } public static class viewholder { textview message; } @override // http://stackoverflow.com/questions/6711592/what-is-the-intent-of-the-methods-getitem-and-getitemid-in-the-android-class-bas public long getitemid(int position) { // unimplemented, because aren't using sqlite. // http://stackoverflow.com/questions/24223393/android-listview-custom-adapter-getview-never-called-up homecoming 0; // homecoming position; }

}

this not authenic way deal problem.

but have made workaround adding method below. called within separate thread per 1 second. irrespective of whether or not new info added, notifydatasetchanged() , if there items added failed seen on ui described above, refreshed upon user screen.

public void refresh() { this.runonuithread ( new runnable() { @override public void run() { adapter.notifydatasetchanged(); getlistview().setselection( adapter.observemessagelist().size() - 1 ); } }); }

if find fundamental reason why has been case, elaborate on issue.

android listview

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -