Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect when BaseAdapter.notifyDataSetChanged() finished

I want to know when BaseAdapter.notifyDataSetChanged() finish and then do sonething. My code:

BaseAdapter.notifyDataSetChanged();   mScroller.startScroll(oldX, 0, dx, 0); 

mScroller scrolls before notifyDataSetChanged() finishes. I want mScroller to scroll to oldX after notifyDataSetChanged() finishes.

Please help me, thank you.

like image 528
hiepnh Avatar asked Aug 22 '12 14:08

hiepnh


2 Answers

Instead of extending, BaseAdapter supports registering a DataSetObserver where you can listen for the onChanged event.

The notifyDataSetChanged method will cause this event to fire.

adapter.registerDataSetObserver(new DataSetObserver() {     @Override     public void onChanged() {         // ...     } });  ...  adapter.notifyDataSetChanged(); 
like image 51
Cord Rehn Avatar answered Oct 03 '22 19:10

Cord Rehn


I had the same exact problem, try this out it worked perfectly for me. You are Overriding the method notifyDataSetChanged(). You do this in your Adapter class. Just copy the code i posted and replace the line where I setSelection() with what ever you need done.

@Override public void notifyDataSetChanged() {     super.notifyDataSetChanged();      parentGlobal.setSelection(this.getCount() - 1);  //This is how we start Activity fully scrolled to bottom } 

With that said, in case you are wondering, "parentGlobal" is the ListView that I set the Adapter to in my Activity. In the constructor of the Adapter, I passed in the ListView and then made it "global". I apologize if "global" isn't a Java term, I come from a C++ world.

//Goes in Activity:

lv_main = (ListView) findViewById(R.id.listView_conversation);     adapter_main = new Adapter_Conversation(ConversationActivity.this, R.layout.layout_list_conversation,             list_main_UI, lv_main, main_list_item_margin_px);     lv_main.setAdapter(adapter_main); 

//Goes in Adapter

public Adapter_Conversation(AppCompatActivity activity, @LayoutRes int resource,                             ArrayList<Adapter_GetSet_Conversation> list_conversation, ListView lv_main,                             int item_margin_px) 
like image 27
JamisonMan111 Avatar answered Oct 03 '22 19:10

JamisonMan111