I'm developing an application that shows stock market information. In my application I use listactivity and my own adapter is being used.
The listView used to show the stocks is working fine and is updated with correct data. The only problem is, although I use
adapter.notifyDataSetChanged();
the list isn't updated until scrolling and items subjected to change, are scrolled out from the screen. When they are scrolled in, they appear with new data.
I need to change the data as soon as I notify the adapter.
I have face same problem
but finally got solution
And its solution is
listView.invalidateViews();
I think for that you have to take the help of the Lazy ListView or Lazy Loader:
Check out the below link :
Lazy load of images in ListView
It will help you.
Are you calling notifyDataSetChanged in UI thread? If not: you can do the following:
runOnUiThread(new Runnable() {
public void run() {
adapter.notifyDataSetChanged();
}
});
Related
I searched for hours but still have no explanation (even not in official docu) what is the right way to use a RecyclerView.Adapters' notify-methods. Already read a lot of similar question on SO, but no could solve my simple problem. I try to put down a short, simplified example.
class MyAdapterClass extends RecyclerView.Adapter<MyAdapterClass.MyViewHolderClass> {
...
List myItemList = new ArrayList<MyItemType>();
...
}
RecyclerView myRecView = (RecyclerView) findViewById(R.id.myRecView);
myRecView.setAdapter(new MyAdapterClass(...)));
Even when inserting the first item, the view is not refreshed.
myAdapter.myItemList.add(newItem);
myAdapter.notifyItemInserted(myAdapter.myItemList.size() - 1);
If I then tightly touch (try to scroll) the 1-item-listview, the item appears. So it seams to be a refreshing issue. But why? I have an emtpy list, then I insert one item, then I notify the adapter about the insertion. Whats wrong with my expectation?
By the way, even if I additionally call:
myAdapter.notifyItemChanged(myAdapter.myItemList.size() - 1);
it gets not automatically refreshed.
EDIT: And of course, I dont want to use notifyDataSetChanged() :-) only insert, update, remove on an existing list.
Try to add this code inside your adapter class :
#Override
public int getItemCount() {
return myItemList.size();
}
i hope you define in adpter getItemCount() in list size. then used blow code for notify adapter.
myadapter.notifyItemRangeChanged(0, userDataArrayList.size());
I have a listview with 150+ items, I need to make one visible from code. I currently use smoothscrolltoposition but when the desired item is far away from the current visible item it takes several seconds to arrive.
Is there anyway to simply get rid of the smooth scrolling and simply make the item visible directly?
Thanks,
Ignacio
You can use postdelayed for smooth scroll
listview.postDelayed(new Runnable() {
#Override
public void run() {
// smoothscrolltoposition
}
}, 100);
After several testing and reading the thread suggested by audi , I got this solution:
Strangely, the trick is in reassign the adapter to the listview, not even it is needed to recreate it, just reassign.
listView.Adapter = adapter;
listView.FastScrollEnabled = true;
listView.SetSelection(index);
adapter.NotifyDataSetChanged();
i have 3 class. MainActivity,OneFragment,TwoFragment, I have listview which i fill using by Sqlite on OneFragment.And I add record from TwoFragment (with CreateData function using by my SqlHelper class) but new record dont showing in the listview.
How can i solve this?
If you want to see codes i can upload.
I highly recommend you to use RecyclerView instead of ListView (as Google says:
The RecyclerView widget is a more advanced and flexible version of ListView.
), but Both of them you need some refresh data may be like this on your OneFragment:
#Override
public void onResume() {
super.onResume();
items.clear();
items = dbHelper.getItems(); // reload the items from database
adapter.notifyDataSetChanged();
}
this question may help you with your problem Android ListView not refreshing after notifyDataSetChanged
if you do not understand send code
I have a list of 4 items, I have used listview. I want to change a string dynamically on recieving internal event. I see that when I receive the event I am setting the string correctly but and then calling
mAdapter.notifyDataSetInvalidated();
mAdapter.notifyDataSetChanged();
but the list is not getting updated.
I've had the same experience. The cause was that the list adapter was updating on the wrong thread i.e. not the UI thread. This is easily solved by changing the adapter data on the UI thread through (as I found on other posts):
runOnUiThread(new Runnable() {
public void run() {
// code that changes the list adapter data
}
});
Of course you can always create a (inner) class that implements Runnable that is provided with the list adapter and data to add, insert etc.
Note: calling notifyDataSetInvalidated() or notifyDataSetChanged() will not be necessary as it is called by default, unless you turned it off explicitly with setNotifyOnChange(false);
I think that notifyDataSetChanged only works if you use the add (or insert), remove or clear functions on adapter.
You can rebuilt the list adapter for force to refresh the listView.
Excuse me for my bad english
I'm using a ListView with an ArrayAdapter, and code executing after ListView.setAdapter(ArrayAdapter,...) is seeing ListView.getChildCount()==0.
Is there a way t wait for the UI thread to finish filling the ListView before continuing?
Thanks in advance,
Lenny
ListView.getChildCount() is inherited from ViewGroup and is not related to the items managed by the adapter but the child views managed by the ListView.
To get the number of items displayed by the ListView use getCount() which returns the number of items managed by the adapter.
I had a similar problem that I solved creating a thread that has while loops that can only go forward after the list is loaded.
new Thread(new Runnable() {
public void run() {
while(listView.getCount() == 0) ;
}
}).start();
This approach can be done cuz I garantee that at this point there is something in the list. Be carefull using this approach. It is not the best one but it was the one that I found.