Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Listview not updating

I have a ListView with a custom adapter, displaying information from a database. When I start the app, the data is read from the database, given to the adapter, which is then attached to the ListView. A LayoutAnimationController is run on the ListView, displaying the contents smoothly.

    this._lvList = (ListView)_v.findViewById(R.id.lvList);
    this._lvList.setAdapter(new TableAdapter(getActivity(),R.layout.tablerow,Category.getCategories()));
    LayoutAnimationController lac = new LayoutAnimationController(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_in_left));
    lac.setDelay(0.2f);
    this._lvList.setLayoutAnimation(lac);       
    this._lvList.startLayoutAnimation();

No problem at all.

Now when I click on any entry of the ListView, the entries which were not clicked disappear, the clicked entry becomes the one and only entry in the list and is displayed at the top, as expected.

View v;
for(int i = 0;i<_lvList.getChildCount();i++) {
    v = this._lvList.getChildAt(i);
    if(v!=clickedView) {
           Animation animSlideOut = AnimationUtils.loadAnimation(getActivity(), i%2==0?R.anim.slide_out_left:R.anim.slide_out_right);
           animSlideOut.setStartOffset(i*50);

           // some more stuff

    }
    v.startAnimation(animSlideOut);
}

This works as well, but now the problem, if I click again on that single list entry, I want the list to repopulate, displaying all items again.

I thought I could use the code from the start (the first snippet), as it works fine when starting the app, but...this time...it doesn't. No animation happening. The reason is, there are no views to animate in my ListView (except the one from the previous step).

Instead of creating a new Tableadapter I already tried to clear it, fill it new, called notifyDataSetChanged()... no use, as

_lvList.getChildCount();

stills returns 1 view, while the adapter holds all 18 entries. I as well tried

requestLayout();
forceLayout();
invalidate();
invalidateViews();

to force the ListView to generate its child views before the animation, but it's not working. So atm the ListView just appears instantly, somewhen after my call to the layout animation.

Summary : my ListView contains no child views before the start of the layout animation, how can I force it to generate them?

Thx in advance :)

like image 413
El Duderino Avatar asked Nov 13 '12 17:11

El Duderino


1 Answers

Hmm... have you done this yet? (below)

in your custom adapter, override the method:

@Override
public View getView(int position, View convertView, ViewGroup parent){
    //the position of the item, the convertView is the view layout of what that row is

}

to change how the listview is updated. this getView method gets called by Android every once a while to refresh the view (you don't call it manually)

also, i think you only need to call

listView.notifiyDataSetChanged() and listView.invalidate() to force the update, after you repopulate the list

you can read more on listviews here:

http://www.vogella.com/articles/AndroidListView/article.html

http://developer.android.com/guide/topics/ui/layout/listview.html

like image 148
David T. Avatar answered Nov 03 '22 02:11

David T.