Class class exception while footer exception?

I have an exception that I never thought I'd see. The class excludes the adapter when removing the footer from the ListView (sic).

java.lang.ClassCastException: com.test.MyAdapter at android.widget.ListView.removeFooterView(ListView.java:381) 

How can this happen? What does footer removal do with cast class exception ??

A list is a multi-page adapter, perhaps because of this, but still a class exception for deleting a footer (sic).

+8
android
source share
4 answers

Add a footer call to the ListView before calling the setAdapter() method.

Added:

 public void addFooterView (View v) 

C: API Level 1 Add a fixed view that appears at the bottom of the list. If addFooterView is called more than once, views are displayed in the order in which they were added. Views added with this call can focus if they want to.

NOTE. Call this before calling setAdapter. This means that the ListView can wrap the supplied cursor with one that also takes into account the presentation of the header and footer.

Parameters v View to add.

A source

You can also check out this interesting post .

Hope this helps.

+12
source share

This is the code for the answer above, it worked in my case:

I needed to install footerView (this is a loadView on the paginatedView list) in my View list before setting up the adapter, and then uninstall it. First, I initialized my loadView from the layout file in the OnCreate method:

 LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); loadingView = layoutInflater.inflate(R.layout.loading_view, null); 

Then I used this workaround in the same method:

 this.setIsLoading(true); listView.setAdapter(adapter); this.setIsLoading(false); 

Where

 private void setIsLoading(boolean isLoading) { this.isLoading = isLoading; if (isLoading) { listView.addFooterView(loadingView); } else { listView.removeFooterView(loadingView); } } 
+9
source share

The problem arises not from removeFooterView() , but from addFooterView() . If you read the documentation, it indicates that a shell will be added to your adapter:

If the ListView adapter does not extend the HeaderViewListAdapter, it will be wrapped by a supporting instance of the WrapperListAdapter.

Therefore, you must use getter to retrieve the wrapped adapter and transfer it to your adapter. Like this:

 ((MyAdapter) ((HeaderViewListAdapter) listView.getAdapter()).getWrappedAdapter()) 

Hope this helps you solve your problem.

Yours faithfully,

+1
source share

Adding to the other answers, if you add / remove footers dynamically (for example, if they reach the bottom of your list and then you add the footer), then the easiest way is to override setAdapter in your ListView and add a new View object as the footer, this will ensure adapter transfer to HeaderViewListAdapter :

 @Override public void setAdapter(ListAdapter adapter) { addFooterView(new View(getContext())); super.setAdapter(adapter); } 
0
source share

All Articles