during getView()
I set the layout to each item in the list.
However there are some items I want to remove from my ArrayList and also not draw in the list.
My code goes like:
getView() {
if (condition a)
{
myArrayList.remove(currentIndex);
return;
}
doMoreLayouting();
}
Is this the right way?
Does it do any hurt when you remove an item from the collection during list drawing?
Does it do any hurt when you remove an item from the collection during
list drawing?
Yes it will, the main problem is that first the item in the collection will get deleted but what if the item is already called by the getView and you are deleting it after it is drawn then there is your first problem it wont get deleted, because you need to call notifyDataSetChanged to call the getView all over again to delete it from the listView.
Second is that what if each item in your list need to delete somewhere in your array then it will call an call getView again and again which is not wise in performance of you application.
I would recommend deleting it from the method of your adapter and make sure that you gather all the data that needs to be delete and call notifyDataSetChanged only once for performance wise.
Related
It's easy to remove, add and insert an item from the listview adapter object, but I don't think I see a direct method to update an item in the listview adapter. Obviously, I could simply replace via remove/add, but I want to make sure I'm not missing something obvious.
Right now, I'm simply updating the underlying Item Array and refresh the entire listview by recreating/assigning the adapter (probably also not very elegant).
I hope the question is clear enough.
If you are using ArrayList in adapter class for update In ArrayList there is method called set() which update the item in arraylist after updation call notifyItemchanged() which update an item in listview
Check this link
I want to make a diable switch that can instantly gray out and disable a list of options from a listView, but I am not sure how to acchieve this with a CustomAdapter for each single row. With only textview it should be easy to set it with a onclick method, but can I also do that with a CustomAdapter?
Your question is kind of abstract because it doesn't have any code samples and no specific problem as well. So my answer will be abstract as well.
To achieve this set a listener on your switch. Once it's clicked, update the model that is used for displaying your adapter items.
If you need to update all items in the list, then you can add a boolean variable to the adapter class. Use this variable in your getView() method to decide whether the item should be grayed out or not. When the value is changed, call notifyDataSetChanged() on the adapter object. It will trigger redrawing all items in the list.
If you need to update only specific items, then add this boolean variable to the item model itself. Iterate over collection and set this flag where needed. All other logic is the same - use this variable in getView() and call notifyDataSetChanged().
I'm wondering right now how to implement following functionality in best and most reusable way.
Whe then user clicks an item in the ListView, he goes to DetailsActivity, but when he comes back to ListActivity item that he clicked before is shown as first in the list.
I'm wondering should I implement some kind of sorting in listview or in adapter. Which way would be most reusable and generic eg. clicked item is excluded?
You can use the insert method from ArrayAdapter. When coming back from DetailsActivity, delete the clicked item from your adapter using the remove method, then insert the element at the first position. Of course, you also need to call notifyDataSetChanged when you're done.
To identify when you're coming back from DetailsActivity, you could launch it with startActivityForResult and modify the adapter in onActivityResult. More on that here.
The most simplest thing I could suggest for it is
Remove the item from the list and the add the item to the list on calling on activity result.
Set the data to the adapter (may be using a public method in adapter class)
Call notify data set changed.
Get the item position clicked from onItemClicklistener and delete the item at that position in arraylist or array and add it to 0th position. So while calling DetailsActivity instead of writing startActivity(Intenet) use startActivityForResult(Intent) and in OnActivityResult method do the above said operation and call notifydatasetchanged().
I have 3 activities containing listviews with custom adapters, an item selected at the first one drives the user to the second one, and so on...
At these listviews, some items are highlighted as "new itens" (each time adapter's getView is called, I check at a database if the current item should be highlighted). Once user has reached the 3rd listview, I mark these items as "checked", and want to propagate this change back to the other listviews...
That means, when user comes back, pulling the 2nd and 1st listviews up from the stack, I want the viewed items not to be highlighted anymore.
I've tried this answer on SO, without success. When executing notifyDataSetChanged() from onResume(), my listview simply didn't shows up. And I'd prefer not to use startActivityforResult()...
here's my code to refresh the listview at onResume():
#Override
public void onResume(){
super.onResume();
//I have basically the same code at onCreate()
adapter = new ListingsAdapter(getApplicationContext(), this);
list.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
here's another answer that sounds promising, but I wasn't sure how can I retrieve an specific view from the adapter... I want to check all visible items with adapter.getView(), but it asks for a convertView and a parent ViewGroup and I couldn't get that
Thanks in advance for any hint on this
Why don't you wanna use startActivityforResult()?
You don't need to create a new adapter every time in onResume(). You only need to change the underlying data (set the checked flag for the visited item) and then call adapter.notifyDataSetChanged(). This will trigger the adapters getView() method for all the visible list elements.
I use below code to update listview.
setListAdapter(MyAdapter);
MyAdapter.notifyDataSetChanged();
getListView().setEmptyView(empty);
But each time update, the list always show top one.
I want to let it only update data, but not focus on top.
How to do it?
Use a ArrayAdapter backed by an ArrayList. To change the data, just update the data in the list and call
MyAdapter.notifyDataSetChanged().
Although you can call notifyDataSetChanged() on SimpleAdapter too.
You need to add listview.setSelection(your_collection.size());. This will make the last item in the ListView as selected when you refresh the ListView. Also call this after refereshing the ListView.