FrameLayout cannot be added to android.widget.AbsListView

I would like to set a footer for a list view. but i am getting below errors:

 >     : FATAL EXCEPTION: main
 >     08-19 07:26:53.225: E/AndroidRuntime(1176): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams
 > cannot be cast to android.widget.AbsListView$LayoutParams
 >     08-19 07:26:53.225: E/AndroidRuntime(1176):  at android.widget.ListView.clearRecycledState(ListView.java:513)
 >     08-19 07:26:53.225: E/AndroidRuntime(1176):  at android.widget.ListView.resetList(ListView.java:500)
 >     08-19 07:26:53.225: E/AndroidRuntime(1176):  at android.widget.ListView.setAdapter(ListView.java:442)
 >     08-19 07:26:53.225: E/AndroidRuntime(1176):  at .SimpeSearch.onClick(SimpeSearch.java:91)

      


line 91 is set by the adapter:

FrameLayout footerLayout = (FrameLayout) getLayoutInflater().inflate(R.layout.footer_loadmore,null);
TextView footer = (TextView) footerLayout.findViewById(R.id.footer);



listview.addFooterView(footer);

        AdapterSimpleSearch ad = new AdapterSimpleSearch(this,R.layout.row_list_simple_search,10, footer);
listview.setAdapter(ad); 

      

footer_loadmore:

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="6dp"
>

<TextView android:id="@+id/footer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:text="Loading..."
    />

</FrameLayout>

      

constructor:

public AdapterSimpleSearch(Context context,int layoutResourceId,int pageSize, TextView footer){
    super(context,layoutResourceId);

      

how can i fix this?

updated:

my adapter:

public AdapterSimpleSearch(Context context,int layoutResourceId,int pageSize, TextView footer){
    super(context,layoutResourceId);
    helperbooks = new ArrayList<>(); 
    this.context            = context ;
    //this.helperbooks      = hbooks ;
    this.mFooter = footer ;
    inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.mPageSize = pageSize ;

}

public void notifyNoMoreItems(){
    mHasMoreItems = false;
    mFooter.setText("No more Items");
}


@Override
public int getCount() {
    // TODO Auto-generated method stub
    return this.helperbooks.size();
}


@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}


public static class ViewHolder{

    TextView txt_view_title,txt_view_author,txt_view_publisher ;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub

    if(position == getCount() - 1 && mHasMoreItems){
        LoaderTaskSimpleSearch t = new LoaderTaskSimpleSearch(position + 1, position + 1 + mPageSize, context,this,"book");
        t.execute();
        mFooter.setText("Loading . . .");
    }


    ViewHolder viewholder ;

    if(convertView == null){
        convertView = this.inflater.inflate(R.layout.row_list_simple_search, null);
        viewholder = new ViewHolder();

        viewholder.txt_view_title       = (TextView)convertView.findViewById(R.id.txt_view_title_list_simple_search);
        viewholder.txt_view_author      = (TextView)convertView.findViewById(R.id.txt_view_author_list_simple_search);
        viewholder.txt_view_publisher   = (TextView)convertView.findViewById(R.id.txt_view_publisher_list_simple_search);
        convertView.setTag(viewholder);

    }else

        viewholder = (ViewHolder)convertView.getTag();

    viewholder.txt_view_title.setText(helperbooks.get(position).getTitle());
    viewholder.txt_view_author.setText(helperbooks.get(position).getAuthor());
    viewholder.txt_view_publisher.setText(helperbooks.get(position).getPublisher());

    return convertView;
}

      

}

+3


source to share


2 answers


Remove the parent FrameLayout

from the XML footer. All you need is this TextView

. You can use the fields to fill.

(Alternatively, just set for everything FrameLayout

as a footer.)

The error occurs because the parent footer

is FrameLayout

, and a ListView

must control the parent itself.

So XML:



<?xml version="1.0" encoding="utf-8"?>

<TextView android:id="@+id/footer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_margin="6dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textAppearance="@android:style/TextAppearance.Medium"
    android:text="Loading..."
    />

      

and the code:

View footer = getLayoutInflater().inflate(R.layout.footer_loadmore, null);
listview.addFooterView(footer);

      

+5


source


Use this one.

View footerLayout = ((LayoutInflater) this.getSystemService(
            Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_loadmore,
            null, false);
listview.addFooterView(footerLayout);

      



Also change height

and width

your FrameLayout

on wrap_content.

0


source







All Articles