notifyDataSetChanged() not showing up for custom list adapter - android

I have a custom array adapter than extends ArrayAdapter, but the notifyDataSetChanged() method just plain doesn't show up(and does not work if you try it).
class matchAdapter extends ArrayAdapter<Match> {
public matchAdapter(Context context, List<Match> matches) {
super(context, R.layout.match_layout, matches);
}
#Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
The above is from my adapter class. Would notifyAll() do the same thing?

Your listAdapter is declared as having the type ListAdapter, which doesn't have the method you expect (notifyDataSetChanged). You need to make it at least BaseAdapter or cast it to a BaseAdapter(I am assuming your MatchAdapter is at least a BaseAdapter - if not, well, calling such a method would make no sense:-) )
((BaseAdapter)listAdapter).notifyDataSetChanged();

I'll write an example, it's not a real code but it's how you have to do this.
Create your List
ArrayList<matchList> matchList = new ArrayList<matchList>();
Create an Adapter
adapter = new matchAdapter(this, matchList);
Set the Adapter
YourListView.setAdapter(adapter);
You change the stuff on your List
matchList.add(Stuff for your list);
Notify now that List has changed doing :
adapter.notifyDataSetChanged();
Hope it helps to you, if it does not, just type a comment and I'll try to update my answer.
Edit
I'll give you some examples...
When you want to call notifyDataSetChanged() just do this :
List.clear();
List.addAll(YourStuff);
adapter.notifyDataSetChanged();
Other method could be, you are using an AsyncTask so your onBackGround() you could make it returns items for your list, then on your onPostExecute update the adapter.
Example :
#Override
protected List<matchList> doInBackground(String... params) {
List<matchList> items = loadUpdatedDataset(params);
return items;
}
#Override
protected void onPostExecute(List<matchList> itemschangeds) {
updateAdapterDataset(itemschangeds);
adapter.notifyDataSetChanged();
}

Have you tried compiling and running the app? If there are any syntax errors above your matchAdapter (like a missing ; or "), it'll screw the autocompletion up.

Related

How to call a method of RecyclerView adapter in a Fragment

I have a fragment with a recyclerview. Here, I'm using a custom method to update the adapter with new dataset.
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .ViewHolder>{
.....
public void addNewDataset(ArrayList<Integer> data) {
......
notifyDataSetChanged();
}
}
I have called the method like the following, in my Fragment.
myRecyclerAdapter.addNewDataset(data);
Please help me, Thank you in advance.
Did you try to cast recyclerView adpter than call method like this
((MyRecyclerAdapter)recyclerView.getAdapter()).addNewDataset(data);
Compiler search addNewDataset() method in default adapter
Make public method in adapter.
public void addNewDataset(ArrayList<Integer> data) {
......
notifyDataSetChanged();
}
Now make object of adapter in fragment like:
MyAdapter adapter = new MyAdapter(); //make object of adpater like this
adapter.addNewDartaset(data);
Before calling this addnewDataset please check data object reference is not having null value.
if(data!=null){
myRecyclerAdapter.addNewDataset(data);
}
public class MyRecyclerAdapter extends RecyclerView.Adapter<MyRecyclerAdapter .ViewHolder>{
private ArrayList<Integer> mData;
.....
public void addNewDataset(ArrayList<Integer> data) {
if(mData==null){
mData = new ArrayList<Integer>();
}
//If in case you are passing all new array list of integers
mData = data;
//If you want to add new one data to existing array list
mData.addAll(data);
//Use one condition of code from above on the basis of your requirement.
......
notifyDataSetChanged();
}
}

How to give RecyclerView Adapter new data from AsyncTaskLoader

My AsyncTaskLoader is loading data from a remote server. When new data arrives, naturally a call is made to onLoadFinished. At this point I don't know how to give the new data to the RecyclerView.Adapter. Calling notifyDataSetChanged does not give it the new data: it simply tells it there is new data. So any advice on how I might do this? Right now the best I can think of is to create my own setData method in my implementation of RecyclerView.Adapter as
public void setData(List<MyObject> data){
if(null != data && !data.isEmpty()){
synchronized(mItems){
mItems.clear();
mItems.addAll(data);
}
notifyDataSetChanged();
}
}
Is my idea the best there is? Or is there a more sound way of doing this?
Expose a public method in your adapter to update data.
For example, you could put it like this
public void updateItems(ArrayList<MyObject> myObjects) {
this.data = myObjects;
notifyDataSetChanged();
}
You have two options,
1. Re- instantiate your adapter with your new data and reset the adapter.
2. The way you do it.
I can not think of any other methods.
Ok, I had the same issue and here is the solution what I did.
1st I passed list object from onLoadFinished and in the RecyclerViewAdapter I have created method name setCardInfoList() and there I passed this object to global List object which I define in adapter class.
In onLoadFinished method..
#Override
public void onLoadFinished(android.content.Loader<List<Earthquake>> loader, List<Earthquake> earthquakes) {
if (earthquakes != null && !earthquakes.isEmpty()) {
adapter.setCardInfoList(earthquakes);
adapter.notifyDataSetChanged();
}else {
emptyView.setText("No Earthquake Found...");
}
}
Inside Adapter class
public void setCardInfoList(List<Earthquake> earthquakes){
this.earthquakeList = earthquakes;
}

How to use Notifydataset changed for list adapters in android

I Googled lot about this notifydataset change issue, still i m unable to find answer, i have a listview containing custom object.
My implementation looks like this
1) A list of custom objects
2) A Adapter to which i provide the custom object list
My Quesion:
When i delete any item from list, in backend i'm simply calling remove from the custom object list. and if i call notifyDatasetchanged, its not working :(..
Its not refreshing the list, i dont no where is it missing. Kindly help me what is the procedure to update list in this senarios
Take a look at my answer in this thread.
Let me know if you are still having problems.
cheers!
Use AsynkTask for Custom ListView Like this:-
InboxTask.execute(); will call asynktask
class InboxTask extends AsyncTask<Uri, Integer, ArrayList<InboxField>>
{
#Override
protected void onPreExecute()
{
pd=ProgressDialog.show(HomePage.this, "", "Please wail...",true,false);
super.onPreExecute();
}
#Override
protected ArrayList<InboxField> doInBackground(Uri... params)
{
return ArrayList<InboxField>
}
#Override
protected void onPostExecute(ArrayList<InboxField> result)
{
inboxAdapter=new InboxAdapter(HomePage.this,result);
list.setAdapter(inboxAdapter);
adapter.notifyDataSetChanged();
list.destroyDrawingCache();
pd.dismiss();
}
put adepter.notigyDataSetChanged in onPostExecute Method

Listview does not auto update itself

each time my activty receives a message (from some TCP listening thread), it does
mLstAdpChatScreen.add(line);
updateUI();
private void updateUI()
{
runOnUiThread(new Runnable()
{
public void run()
{
mLstAdpChatScreen.notifyDataSetChanged();
mLstAdpChatScreen.notifyDataSetInvalidated();
mLstVwChatScreen.requestLayout();
mLstVwChatScreen.invalidate();
}
});
}
While this approach works on most of my listviews and they do get updated, it does not for a certain listview. I must be missing something :-?
Thank you
That should work for most of the cases however I got the same problem when trying to update listview from database. I called adapter.notifyDataSetChanged(); too, but listview didn't update at all although I could confirmed the data was changed, and the ArrayList's size increased.
In the end, I implemented a method inside my custom Adapter extending from BaseAdapter just to call notifyDataSetChanged there, like
public class MyAdapter extends BaseAdapter{
public void updateData(){
this.notifyDataSetChanged();
}
}
Then in Activity, I just call adapter.updateData(). And this worked for me. Very weird.

update listview dynamically with adapter

This tutorial uses a SimpleAdapter which works fine, but I need to update the arrays in the adapter when new data is entered.
Could you please guide me on how to update a ListView using something other than a SimpleAdapter?
Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call adapter.notifyDataSetChanged().
If you create your own adapter, there is one notable abstract function:
public void registerDataSetObserver(DataSetObserver observer) {
...
}
You can use the given observers to notify the system to update:
private ArrayList<DataSetObserver> observers = new ArrayList<DataSetObserver>();
public void registerDataSetObserver(DataSetObserver observer) {
observers.add(observer);
}
public void notifyDataSetChanged(){
for (DataSetObserver observer: observers) {
observer.onChanged();
}
}
Though aren't you glad there are things like the SimpleAdapter and ArrayAdapter and you don't have to do all that?
SimpleListAdapter's are primarily used for static data! If you want to handle dynamic data, you're better off working with an ArrayAdapter, ListAdapter or with a CursorAdapter if your data is coming in from the database.
Here's a useful tutorial in understanding binding data in a ListAdapter
As referenced in this SO question
Most people recommend using notifyDataSetChanged(), but I found this link pretty useful. In fact using clear and add you can accomplish the same goal using less memory footprint, and more responsibe app.
For example:
notesListAdapter.clear();
notes = new ArrayList<Note>();
notesListAdapter.add(todayNote);
if (birthdayNote != null) notesListAdapter.add(birthdayNote);
/* no need to refresh, let the adaptor do its job */
I created a method just for that. I use it any time I need to manually update a ListView. Hopefully this gives you an idea of how to implement your own
public static void UpdateListView(List<SomeObject> SomeObjects, ListView ListVw)
{
if(ListVw != null)
{
final YourAdapter adapter = (YourAdapter) ListVw.getAdapter();
//You'll have to create this method in your adapter class. It's a simple setter.
adapter.SetList(SomeObjects);
adapter.notifyDataSetChanged();
}
}
I'm using an adapter that inherites from BaseAdapter. Should work for any other type of adapter.

Categories

Resources