When you create a custom adapter extending ArrayAdapter<T>, it has usually the form:
public class ListAdapter extends ArrayAdapter<Item> {
private List<Item> mData;
public ListAdapter(Context context, int resource, List<Item> data) {
super(context, resource, data);
mData = data;
}
}
The data is initially saved in a private member mData, but also the ArrayAdapter saves the data in its own member mObjects. I am pretty sure those are not actual copies, but references to the same list.
Now, and this is my question, if during the ListView processing, for some reason, you have to replace your own list with a fresh new List, I think you should also do:
mData = new List<Item>();
super.clear();
super.addAll(mData);
otherwise there will be no consistency in ListView, and methods like getFilter().filter() will not work.
Am I correct?
I think, when you say mData = data; it only copies pointer of the data array, because when you execute that;
ListAdapter adapter = new ListAdapter(context, resource, data);
data.clear();
adapter.notifyDataSetChanged();
it changes list. So it keeps pointer of your source array,
Second, I think (not sure) you cannot use filter function of adapter, at least I couldn't use and write my own filter function. I filter elements from sqlite(I take my elements from database). and use notifyDataSetChanged function of adapter.
You are right. Your ListAdapter doesn't make a deep copy of the provided list of Items. This means that changing an Item instance 'outside' the ListAdapter will put the ListAdapter in an invalid state.
However, you can 'fix' this by calling notifyDataSetChanged on the ListAdapter.
List<Item> itemList = ....
....
....
ListAdapter adapter = new ListAdapter(this, R.layout.somelayout, itemList);
....
Now, if you change an item 'outside' the ListAdapter, you can still make your ListAdapter be in sync with the change:
itemList.get(idx).changeSomethingInItem("Hello"); // Changes the Item at index 'idx'.
adapter.notifyDataSetChanged(); // Notify adapter about this change.
you really needn't pretty sure whether it actual copies or not ,just extend BaseAdapter
Related
In Android, you can provide an ArrayList when creating an ArrayAdapter for a ListView. I need to update a number of items in the ArrayList.
The ususal way is to just call notifyDatasetChanged. What I prefer to do is reload the entire data into a new ArrayList from my database and then apply this new ArrayList to the existing ArrayAdapter but without creating a new adapter. Creating a new adapter will cause the ListView to go blank and start with position zero. This would also be obvious if the user were scrolling and I suddenly recreated a new adapter.
Is it possible to apply a completely new ArrayList to the existing adapter? The primary reason I want to do this is because it is very fast to just reload a new ArrayList with all the data than having to go through an existing ArrayList and inserting, deleting or updating existing items.
1) First of all. You need to use custom ArrayAdapter (or SimpleAdapter or RecycleViewAdapter)
2) Than create function in your custom adapter :
// Initialize your list;
private ArrayList<Model> arrayList;
.....
//Create constructor and past starting ArrayList
public MyAdapter (ArrayList<Model> array) {
this.arrayList = array;
}
......
public void updateMyData (ArrayList array) {
clear();
// Or you can use arrayList.addAll(array); - just add new items
this.arrayList = array;
notifyDataSetChange();
}
.....
3) In your Activity (or where you initialize adapter)
just use code like this:
MyAdapter myAdapter = new MyAdapter(startArratListData);
// And Than for updating data:
myAdapter.updateMyData(newArrayListData);
This question already has answers here:
How to update RecyclerView Adapter Data
(16 answers)
Closed 5 years ago.
When I have to use a classic adapter with a ListView, I update my data in the ListView like this:
myAdapter.swapArray(data);
public swapArray(List<Data> data) {
clear();
addAll(data);
notifyDataSetChanged();
}
I would like to know what is the best practice for a RecyclerView. Because in a RecyclerView adapter you can't do a clear and addAll as in ListView.
So I tried just with a notifyDataSetChanged, but it didn't work.
Then I tried with a swapAdapter on my view:
List<Data> data = newData;
MyRecyclerAdapter adapter = new MyRecyclerAdapter(data);
// swapAdapter on my recyclerView (instead of a .setAdapter like with a classic listView).
recyclerViewList.swapAdapter(adapter, false);
But with this last solution, I still have to create a new instance of my adapter and I feel like it's not the best solution. I should be able just to change my data without a new MyRecyclerAdapter.
RecyclerView's Adapter doesn't come with many methods otherwise available in ListView's adapter. But your swap can be implemented quite simply as:
class MyRecyclerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
List<Data> data;
...
public void swap(ArrayList<Data> datas)
{
data.clear();
data.addAll(datas);
notifyDataSetChanged();
}
}
Also there is a difference between
list.clear();
list.add(data);
and
list = newList;
The first is reusing the same list object. The other is dereferencing and referencing the list. The old list object which can no longer be reached will be garbage collected but not without first piling up heap memory. This would be the same as initializing new adapter everytime you want to swap data.
#inmyth's answer is correct, just modify the code a bit, to handle empty list.
public class NewsAdapter extends RecyclerView.Adapter<...> {
...
private static List mFeedsList;
...
public void swap(List list){
if (mFeedsList != null) {
mFeedsList.clear();
mFeedsList.addAll(list);
}
else {
mFeedsList = list;
}
notifyDataSetChanged();
}
I am using Retrofit to fetch the list, on Retrofit's onResponse() use,
adapter.swap(feedList);
DiffUtil can the best choice for updating the data in the RecyclerView Adapter which you can find in the android framework. DiffUtil is a utility class that can calculate the difference between two lists and output a list of update operations that converts the first list into the second one.
Most of the time our list changes completely and we set new list to RecyclerView Adapter. And we call notifyDataSetChanged to update adapter. NotifyDataSetChanged is costly. DiffUtil class solves that problem now. It does its job perfectly!
Found following solution working for my similar problem:
private ExtendedHashMap mData = new ExtendedHashMap();
private String[] mKeys;
public void setNewData(ExtendedHashMap data) {
mData.putAll(data);
mKeys = data.keySet().toArray(new String[data.size()]);
notifyDataSetChanged();
}
Using the clear-command
mData.clear()
is not nessescary
I am using listview in my app.I am adding items to list with this line:
conversationsAdapter.add(user);
and this initializes list
conversationsAdapter=new ArrayAdapter<JsonObject>(this,0) {
#Override
public View getView(int c_position,View c_convertView,ViewGroup c_parent) {
if (c_convertView == null) {
c_convertView=getLayoutInflater().inflate(R.layout.random_bars,null);
}
JsonObject user=getItem(c_position);
String name=user.get("name").getAsString();
String image_url="http://domain.com/photos/profile/thumb/"+user.get("photo").getAsString();
TextView nameView=(TextView)c_convertView.findViewById(R.id.tweet);
nameView.setText(name);
ImageView imageView=(ImageView)c_convertView.findViewById(R.id.image);
Ion.with(imageView)
.placeholder(R.drawable.twitter)
.load(image_url);
return c_convertView;
}
};
ListView conversationsListView = (ListView)findViewById(R.id.conversationList);
conversationsListView.setAdapter(conversationsAdapter);
conversationsListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
startChat(conversationsAdapter.getItem(position));
}
});
My list view is looking like this:
I want to update an item in the list.How can I do this ?
Example:We can write a method like: changeName when this method calls,method sets name "Tolgay Toklar" to "Tolgay Toklar Test" so I want to update custom listview item attributes.
I totally disagree with tyczj. You never want to externally modify an ArrayAdapter's list and yes it's possible to update just an individual item. Lets start with updating an individual item.
You can just invoke getItem() and directly modify the object and call notifyDataSetChanged(). Example:
JSONObject object = conversationAdapter.getItem(position);
object.put("name", data);
conversationAdapter.notifyDataSetChanged();
Why does this work? Because the adapter will feed you the same object reference used internally, allowing you to modify it and update the adapter. No problem. Of course, I'd recommend instead building your own custom adapter to perform this directly on the adapter's internal list. As an alternative, I highly recommend using the ArrayBaseAdapter instead. It already provides that ability for you while fixing some other major bugs with Android's ArrayAdapter.
So why is tyczj wrong about modifying the external list? Simple. There's no guarantee that your external list is the same as the adapters. Once you perform a filter on the ArrayAdapter, your external list and the adapters are no longer the same. You can get into a dangerous scenario where (for example) index 5 no longer represents position 5 in the adapter because you later added an item to the adapter. I suggest reading Problems with ArrayAdapter's Constructors for a little more insight.
Update: How External List Fails
Lets say you create a List of objects to pass into an ArrayAdapter. Eg:
List<Data> mList = new ArrayList<Data>();
//...Load list with data
ArrayAdapter<Data> adapter = new ArrayAdapter<Data>(context, resource, mList);
mListView.setAdapter(adapter);
So far so good. You have your external list, you have an adapter instantiated with it and assigned to listview. Now lets say at some later point, the adapter is filtered and cleared.
adapter.filter("test");
//...later cleared
adapter.filter("");
Now at this point mList is NOT the same as the adapter. So if the adapter is modified:
adapter.add(newDataObject);
You'll find that mList does not contain that new data object. Hence why external lists like this can be dangerous as the filter creates a NEW ArrayList instance. It won't continue to use your mList referenced one. You could even try adding items to mList at this point and it won't be reflected in the adapter.
If you change the data in your list you need to call notifyDatasetCanged on the adapter to notify the list that the underlying data has changed needs to be updated and.
Example
List<MyData> data = new ArrayList<MyData>();
private void changeUserName(String name){
//find the one you need to change from the list here
.
.
.
data.set(myUpdatedData);
notifyDatasetChanged()
}
In my Android app, I have a listview that initially displays a number of items. I then want to reload the listview with a new set of items. Typically, I do this to initially create the listview items:
adapterDirectories = new DirectoryAdapter(context, R.layout.shop_directory_row, directories);
lvDirectory.setAdapter(adapterDirectories);
My adapter looks like this:
private class DirectoryAdapter extends ArrayAdapter<ShopDirectoryInfo>
{
#SuppressWarnings("unchecked")
public DirectoryAdapter(Context context, int textViewResourceId, #SuppressWarnings("rawtypes") ArrayList items)
{
super(context, textViewResourceId, items);
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
When I want to update the items I do this:
directories = getDirectories();
ArrayAdapter<ShopDirectoryInfo> adapter = (ArrayAdapter<ShopDirectoryInfo>) lvDirectory.getAdapter();
adapter.notifyDataSetChanged();
What this ends up doing though is adding the new items to the existing ones, even though getDirectories returns a completely different set of data. I have no idea why the new items are prepended to the existing set instead of replacing the old set. The only way to get the new data to show up without the old data is to recreate the adapter instead of using notifyDataSetChanged. Is there a way to get the update without calling notifyDataSetChanged?
Please try this
adapterDirectories.notifyDataSetChanged();
lvDirectory.invalidate();
This works:
ArrayAdapter<ShopDirectoryInfo> adapter = (ArrayAdapter<ShopDirectoryInfo>) lvDirectory.getAdapter();
adapter.clear();
directories = getDirectories();
adapter.addAll(directories);
adapter.notifyDataSetChanged();
The key is to use addAll after clearing.
Before setting the adapter again, you should clear the previous items like this.
if(adapter!= null) {
adapter.clear();
adapter.notifyDataSetChanged();
}
I have created a listview that sits inside a larger activity (the list view fills half the screen, with inputs/buttons above it). This listview uses a custom adapter to draw the row views.
Inside the row view is a button, which when clicked/tapped i want the activity to handle, and not the adapter. However i don't know how inside the adapter class, i can tell it to use the activity. Since it's OOP, im assuming i have to pass some sort of reference to the activity when setting up the adapter (rather than hardcoding the parent activity into the adapter).
I had a simlier issue with sharing a dataset between the activity and adapter (i wanted the adapter to use an arraylist from the activity), however i couldn't solve that one either so i ended up passing the arraylist through as a duplicate into the adapter. So i would hope working out the click listener, will also mean i can get rid of that duplicate data having to be created?
All the code is pretty simple, but here's a rough outline:
Setting the list adapter & declaring dataset (this is the activity dataset, not the adapter)
numbersListAdapter = new NumberListAdapter(this);
numbersList.setAdapter(numbersListAdapter);
this.selectedContacts = new HashMap<Long, HashMap<String, String>>();
Adding entry into activity data set and adding to the adapter dataset
HashMap<String, String> tempa = new HashMap<String,String>();
tempa.put("name", name);
tempa.put("number", number);
this.selectedContacts.put(contactID, tempa);
this.numbersListAdapter.addEntry(contactID, tempa);
The adapters add entry
public void addEntry(Long id, HashMap<String, String> entry) {
entry.put("contactID", id.toString());
this.selectedNumbers.add(entry);
this.notifyDataSetChanged();
}
The adapters constructor
public NumberListAdapter(Context context) {
selectedNumbers = new ArrayList<HashMap<String, String>>();
mInflater = LayoutInflater.from(context);
}
Please note: this is my first attempt at this stuff. I've never done android, java and very, very little OO programming. So i already know the code is most likely inefficient and quite terrible. But i've got to learn somehow :)
EDIT
Ok so i realised ive been a little silly and i just needed to use the context passed into the adapter to reference the parent activity. However i still ain't getting it. Heres the code:
The constructor for the adapter
numbersListAdapter = new NumberListAdapter(this);
The variable declaration and constructor method
public class NumberListAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<HashMap<String, String>> selectedNumbers;
private Context parentActivity;
public NumberListAdapter(Context context) {
parentActivity = (Context) context;
selectedNumbers = new ArrayList<HashMap<String, String>>();
mInflater = LayoutInflater.from(context);
}
The listener
Button theBtn = (Button)rowView.findViewById(R.id.actionRowBtn);
theBtn.setOnClickListener(this.parentActivity);
I get two messages from eclipse, the first occurs when i comment out the listener and i get
The value of the field NumberListAdapter.parentActivity is not used
Once i add the listener in i get
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Context)
Obviously im doing something wrong. Probably quite a silly mistake again too
If you need to get callback when row of the listview is clicked you can use
numbersListAdapter.setOnitemitemClickLiistener
But if you need a button inside each row to be clicked you will have to override the
getView function of your adapter and then set the onclicklistener of the button individually
Edit:
Typecast the Context to Activity
theBtn.setOnClickListener((YourActivity)this.parentActivity);