ItemAdded Event - ListView - android

Is there is any equivalent to ItemAdded event in android listview?
basically I want to check if the new item has some attributes or values then I will do some coloring on it.
I don't want to loop over all items once loading is done, even though I have no idea how to do it :)

You need to update the data that is used in the adapter in your listview.
After updating the data.. there is one method in adapter called notifyDataSetChanged() which will refresh the adapter.
Now in adapter's getView() method,
implement the logic that can change the color..
Hope it will help you..!!

Related

How to update a listview item via adapter

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

set text color for each custom adapter from a listView

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().

ListView always on top

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.

Android - updating the simpleCursorAdapter

I have a ListActivity which gets its data from a database query. I also have a custom adapter which extends simple cursor adapter.
To show the customised content, I have overridden the newView and bindView methods.
Each element of the view has:
TextView containing a title
ImageView containing number of stars - the image shown is changed based on a value obtained from the database
A button - the button text changes on clicks (Favourite/Make Favourite), and triggers a database update event.
My problem is this - when I scroll the ListView, the changes I made seem to disappear .. for instance, the first item is marked favourite, and the list is scrolled ... when I come back to the first item, the text reverts back to its previous value, although internally the db has been updated.
I read that the notifyDatasetChanged() is not right for this case as the adapter should be notified of the database changes. I am trying to use the reQuery() method, but do not know where to place it.
Should I place the reQuery() in the onClick method of the button? If not, where should it be placed?
What so ever Adapter is you are using must have getView Method.. Whenever a list is scrolled this getView Method got called for all the listItem in the view. Make Sure in the getView Method. That the view is getting generated dynamically.
It should check for clicked or changed value.
to update the list without scrolling your have to call listView.getAdapter().notifyDataSetChanged()
Check the following link. here i mentioned a complete code to use listview properly. Using this we can achieve any listview behavior. We can embed animation also.
Change ListView background - strange behaviour
Hope this help :)
If you have extended BaseAdapter ,
after data is changed call , listView.setAdapter() and in getView() , update the view's data too.
Use SetListAdapter with ArrayAdapter to display the list. Use ConvertView to avoid inflating View.
if (convertView==null)
{
//inflate your list
}

Android List adapter issue

How to remove item from a list view? how to reload the list after removal?
Here you can find all the informations.
Anyway you can call
mListAdapter.remove(x); // to remove an item
and
mListAdapter.invalidate(); // to refresh the content
or
mListAdapter.notifyDataSetChanged();
Please, there's no need to write with multiple question marks like that. Removing items from your ListView depends on how you put in the items in the beginning. Edit your question to provide some details on how you did that. Updating your list afterwards can be done with notifyDataSetChanged() called by your ListView adapter.
Remove from the adapter, and the list will refresh automatically.

Categories

Resources