How to update a listview within the custom adapter? - java

I have a custom adapter which extends ArrayAdapter, it includes checkboxes for each item in the list.
Within the getview itself I have the onCheckedChanged listener that deletes a item from the database if checked. However the listview does not update, and I cant call the notifyDataSetChanged() as I am not in the main class. Any ideas how?

Post the code so that readers can understand your problem.
You dont have to set any listener in the class that is extending ArrayAdapter. In this class we only inflate the data to the views.
Remove the onCheckedChanagedListener from Adapter class and place it in the ListActivity or Fragment that is using this Adapter.

Call invalidate() on your ListView. That makes the list to be refreshed and displayed again. Maybe it will solve your problem.
And I don't understand why you can't call notifyDataSetChanged() ?

Related

Remove all added views from ListView

I am adding a view to my ListView with the following code:
((ViewGroup) myListView.getParent()).addView(createMyCustomView());
My question is how to clear all the added views? I've tried removeAllViews() but it's not working.
I need to add the view to my ListView so that I can set its empty view with:
myListView.setEmptyView(createMyCustomView());
Clear the items from the list you are using in your adapter, then call notifyDataSetChanged() on the adapter. Checkout this similar question.
Let me know if this helps, or if you need more information!

android list view adapter call getview method manually (to refresh list rows)

Is it posibble to call the getview method in the list view adapter to refresh the rows?
Or is there another method to refresh the item rows manually?
Thanks in advance
-Lukas
You should use the Adapter's notifyDataSetChanged() method for that. This will make the listview refresh it's views.

Update ListView or Activity?

I have a listView inside Activity inside tab layout. If I long press the item, I get the option to rename the item. Once the item is renamed, the change can't be seen until the activity is restarted.
I tried solving the problem by simply creating a new intent and re-opening activity, but since that activity is inside tablayout it doesn't work. I also tried re-opening tabLayout activity but then it automatically goes to tab 1, while I'm trying to refresh listView inside tab 2.
So then I tried solving it by creating updateListView() method:
public void updateListView(){
listAdapter.clear();
listAdapter.addAll(recordedFilesArray);
listAdapter.notifyDataSetChanged();
}
But that doesn't work either. When I'm using this method, it completely clears listView and then I have to restart the activity again to see the results.
So, anyone have any idea what I could do to see listView changes without restarting the activity? By the way, if it helps, I'm reading ListView (ArrayList) from a text file.
Try using notifyDataSetChanged() only without clearing your listAdapter
You can make a separate adapter class and implement its getView() function to manage this. Your activity will pass the array list to the adapter. The adapter's getView() will take care of displaying the contents from the list.
When the list gets modified, you need to reinitialize the adapter. So that way getView() always have the updated data to display on the screen.
For renaming the item, you can change your item object in the ArrayList,
and then call adapter.notifyDataSetChanged().
I found the problem. The ArrayList wasn't changed until the activity was restarted. I had to call arrayList.set() to change the element in the ArrayList. Now it works as it should.
My code:
recordedFilesArray.set(toDelete, input.getText().toString()); //toDelete is arg2 variable (onLongItemClickListener)
listAdapter.notifyDataSetChanged();

Updating ListView fragment when not focused

I'm working on a tablet application with a ListView fragment on the left, and some other views including a SearchView (essentially a TextView) on the right. When the SearchView is focused, I want to update the data of the listview, but I can't. If I notifyDataSetChanged on the UI thread, nothing happens. If I call setAdapter() again on my listview, the listview goes blank.
Only when I touch the listview again (it gets temporary focus again or something to cause getView() to be called on the adapter) does it get updated.
Anyone got an idea on how to fix this?
Min-Sdk and Target is 4.0.3.
Did you try to call your refresh routine using runOnUiThread() ?
Reform your objects list, bind to a new instance of your adapter, then call notifyDataSetChanged()and setAdapter() again in refresh routine would work.
You have to use a ViewHolder to handle updates in a ListView I think.
What now works for me is updating the data in the underlying adapter and then calling notifyDataSetChanged on that adapter, all from the UI thread.
I did not need to call setAdapter again on the listView.
Of course I had already tried all this before asking the question, so not sure what made it suddenly start working.
Call it inside the runOnUiThread method. Also add the new object to the adapter inside it.
Like that:
runOnUiThread(new Runnable() {
#Override
public void run() {
chatListAdapter.add(message);
chatListAdapter.notifyDataSetChanged();
}
});

Is it possible to make a dynamic mix of ListView and TextView?

I tried to found a method, but no results. I want a ListView like below, and when I click on an element, like "Word", it'll be like this picture :
Is it possible ?
What you described is what an ExpandableListView does.
Basically it's like a listview so you'll still have to create your own adapter, but it lets you click a row to inflate a bigger item that you can then stuff your text into.
I would recommend creating your own class that extends ArrayAdapter or BaseAdapter. Then you can make use of the getView() method that gets called every time the screen gets redrawn for the user. You can then design multiple views for each selection and choose which one to display in the getView() function. So when the user selects an item, you set a flag in your custom class, and then notifyDataSetChanged() and you're good to go!

Categories

Resources