Adapter class cast exception when removing a Footer view?

前端 未结 4 1762
不思量自难忘°
不思量自难忘° 2021-01-02 13:51

I have an exception I never thought I would see. A class cast exception of the adapter when removing a footer view from a ListView (sic).

 java.lang.ClassCa         


        
相关标签:
4条回答
  • 2021-01-02 13:57

    Add your footer view to ListView before calling setAdapter() method.

    Added:

    public void addFooterView (View v)
    

    Since: API Level 1 Add a fixed view to appear at the bottom of the list. If addFooterView is called more than once, the views will appear in the order they were added. Views added using this call can take focus if they want.

    NOTE: Call this before calling setAdapter. This is so ListView can wrap the supplied cursor with one that will also account for header and footer views.

    Parameters v The view to add.

    Source

    Also you can check this interesting post.

    Hope this helps.

    0 讨论(0)
  • 2021-01-02 14:13

    The problem does not come from removeFooterView(), but from addFooterView(). If you read the documentation, it states that an wrapper will be added to your adapter:

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

    Thus you must use the getter for retrieving the wrapped adapter and cast it to your adapter. Like this:

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

    Hope this will help you with your issue.

    Best regards,

    0 讨论(0)
  • 2021-01-02 14:14

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

    I had to set a footerView (it's a loadingView in a listView with pagination) to my listView before setting it's adapter and then remove it. First I initialized my loadingView from a layout file in 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);
        }
    }
    
    0 讨论(0)
  • 2021-01-02 14:17

    Adding to the other answers, if you're adding/removing footers dynamically (such as if they reach the bottom of your list and then you're adding a footer view) the easiest thing is to Override setAdapter in your ListView and add a new View object as the footer there, this will ensure the adapter is wrapped in the HeaderViewListAdapter:

    @Override
    public void setAdapter(ListAdapter adapter) {
        addFooterView(new View(getContext()));
        super.setAdapter(adapter);
    }
    
    0 讨论(0)
提交回复
热议问题