Remove all added views from ListView - java

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!

Related

How to add ExpandableListView inside a ListView?

I have added a expandableListView inside a list view but expandableListView have not expand and just only see first item of expand listview. Help me
use this site "https://www.journaldev.com/9942/android-expandablelistview-example-tutorial"
for generating ExpandableListView.

How recycler view Data bind?

How Recycler view Bind Data One By One or All in one time?
void onBindViewHolder (VH holder,int position,List<Object> payloads)
Called by RecyclerView to display the data at the specified position.
This method should update the contents of the itemView to reflect the
item at the given position.
So one by one. More here .
Your recycler view calls RecyclerView.Adapter when you set adapter to recycler view or
notify dataset changes to the adapter.
Recycler view calls OnBindViewHolder for each item in your recycler.
Hence, recyclerview binds data one by one.
You can clearly see this when you implement animation to the recycler view items.
RecyclerView is a list drawing library that essentially provides a fixed-size window to load a large dataset into. It recycles the views it created at the begining when the views go out of scope (window) and then if there is a need, reuses them to make it seem as if the views were never offloaded and were virtually present outside the window.It binds the rows which are visible to you and recycles the rest

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

How to update a listview within the custom adapter?

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

Categories

Resources