Scenario: I am using a listview with custom Adapter. I am fetching data every 5 seconds and then displaying it in the listview.
Problem: After every refresh the list scrolls at the top. I would want the list to stay as is from the scroll standpoint and just the underlying data to be refreshed.
Current code: This is called every time I fetch a new data.
mListAdapter = new CustomListAdapter(this, mListData);
mList.setAdapter(mListAdapter);
Instead of your code please try to put every time you fetch data this one:
adapter.notifyDataSetChanged();
Instead of calling this, you should set the new data to the adapter and then call notifyDataSetChanged.
Hope this helps!
As you get new mListData then just invalidate the adapter not re-initialized the adapter you can call like this
mListAdapter.notifyDataSetChanged();
You can use
mList.setSelection(mListData.size() - CONSTANT.CONTENT_SIZE);
This way the listview will not refresh to the top.
Or you can also use
adapter.notifydatasetchanged
by updating the binded list.
This Worked For Me:
adapter.setData(list);
adapter.notifyDataSetChanged();
Related
I used recyclerView in my android app and filled it by a list with 120 items. my problem is that it takes a couple of time to load recyclerView. it seems that setAdapter() method is very slow . what can I do to fix it?
list.addAll(db.getKarevanMembers());
KarevanMembersAdapter adapter = new KarevanMembersAdapter(list);
binding.membersLayout.karevanMembersList.setAdapter(adapter);
Instead of setting new adapter change data of current adapter and call adapter.notifyDataSetChanged().
If you want to change/remove only some elements in the list, use the methods notifyItemInserted, notifyItemRangeInserted, notifyItemChanged, notifyItemRangeChanged, notifyItemRangeRemoved, notifyItemRemoved to update adapter (see https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html).
I have an ViewPagerAdapter, and a List with ViewPager items inside of it. In some moments, I have to add items to my list. I've add they by List.addAll(Collection), call notifyDataSetChanged(), and all is working well. But sometimes I need to add an items to start of my List. I can add they, but notifyDataSetChanged() isn't working in case when start of my list is chanegd.
Any ideas?
Try this code
List items = new ArrayList();
//some fictitious objectList where we're populating data
items.add(0, obj);
listAdapter.notifyDataSetChanged();
This may work. You need not to use Loop and Notify List again and again
Add new items to the start of List
use following code for adding the item to List in onCreate() or where you want.
listName.add("item-1");
listName.add("item-2");
.
.
listName.add("item-n");
notify adapter
after that, use notifyDataSetChanged() to intimate to listAdapter for new Items added in list,
listAdapter.notifyDataSetChanged();
hope this will helps you.
I've read many of the previous posts on this topic, but i'm not getting it right.
I have an adapter which has a private values of the items in the list.
When i update the values(add a new items), i watch the value in debugger and the "getView" func and see that the value is correct.
BUT the actual rowView i see is just the first item in the list.
I have no clue what may cause this.
This listview is on the same activity while i show a different layout and hide the listview to add a new item.
Can there be a connection while the listview visibility is "GONE"?
When i remove items from it it updates listview fine(that is done when listview is visible).
private void updateAdapter() {
this.values.clear();
this.values.addAll(staticlistIndifferentclass);
notifyDataSetChanged();
}
~~~~UPDATE~~~~
Ok,
So i discovered the cause of the problem, though i'm not sure why it is.
The code was fine the way it was with regular Listview but the bug is on:
com.baoyz.swipemenulistview.SwipeMenuListView
Try use ListView.invalidateViews
This should cause views rebuilding
Use new Adapter object like this :
listView.setAdapter(new yourCustomAdapter());
When you delete all dataSet items ,it is better to bind new Adapter object .
I used notifyDataSetChanged for adapter but my List didn't updated. Here is part of my code.
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,userNames);
list = (ListView) findViewById(R.id.listViewUsers);
list.setAdapter(adapter);
SO every time I'm adding item to userNames , I'm calling adapter.notifyDataSetChanged(); method, but there are no any updates. I tried many things with cleaning , clearing userNames but it's not working.
Can anyone help me?
Thanks
EDIT:
I'm calling these two functions where I'm adding new user.
userNames.add(user.getUsername());
adapter.notifyDataSetChanged();
Also I have User class, but I fill in ListView with users' usernames.
Try add username directly to adapter instead of adding to userNames. Maybe when you create a new instance of ArrayAdapter, it will create a new private List inside adapter and you add username to old instance of userNames :
adapter.add(user.getUsername());
adapter.notifyDataSetChanged();
For an ArrayAdapter, notifyDataSetChanged() only works if you use the add, insert, remove, and clear functions on the Adapter.
When an ArrayAdapter is constructed, it holds the reference for the List that was passed in. If you were to pass in a List that was a member of an Activity, and change that Activity member later, the ArrayAdapter is still holding a reference to the original List. The Adapter does not know that you changed the List in the Activity.
Try this:
userNames.add(user.getUsername());
adapter.clear();
adapter.addAll(userNames);
adapter.notifyDataSetChanged();
I am having a situation where I want to update my Custom List View using BaseAdapter whenever my Database is updated. I have tried calling invalidate() on this Custom List but it didn't work, similarly I even tried having a timer to update my list after sometime, that didn't work either. Please let me know of possible solution.
Update:
This is how I am making my custom list view
li= (ListView)findViewById(R.id.id_lv_row);
ColorDrawable divcolor = new ColorDrawable(Color.DKGRAY);
registerForContextMenu(li);
li.setDivider(divcolor);
li.setDividerHeight(2);
li.setAdapter(new FriendsPositionAdapter(this));
BaseAdapter.notifyDataSetChanged() should do the trick as long as the data behind the adapter actually changed. That's all you need to do to refresh the list.
Invalidate is for repainting views only, you have to tell to the List adapter (BaseAdapter) that dataset has changed.
When the data changes, asign the new dataset to the adapter, and later call notifyDataSetChanged()...
in order to make functional notifyDataSetChanged() the adapter data must be changed. Remember that the original data that change is not reflected automatically to the adapter.
//here i retrieve the new list, named "beans"
lista = (BeanList) result.getDataObject();
Vector<Bean>beans = list.getBeanList();
((BeanListAdapter)listAdapter).syncData(beans);
((BeanListAdapter)listAdapter).notifyDataSetChanged();
//now the syncData method
public void syncData( List<PINPropiedad> newData ){
for(Object o : newData){
add(o);
}
}