I would like to set a footer to list view. but I get 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 setAdapter method :
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 it ?
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;
}
}
Remove the FrameLayout
parent from your footer XML. All you need is the TextView
. You can use a margin for the padding.
(Alternatively, just set the whole FrameLayout
as the footer view.)
The error happens because the footer
parent is a FrameLayout
and a ListView
needs to control the parent itself.
So, the 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 code:
View footer = getLayoutInflater().inflate(R.layout.footer_loadmore, null);
listview.addFooterView(footer);
This helped me, found somewhere in android docs. (example with JetPack)
Try to set adapter first, then add footer/header.
val binding = ProductDetailFragmentBinding.inflate(inflater, container, false)
val headerView = ProductListHeaderBinding.inflate(inflater, container, false)
val footerView = ProductListFooterBinding.inflate(inflater, container, false)
binding.productListView.adapter = adapter
binding.productListView.addHeaderView(headerView.root)
binding.productListView.addFooterView(footerView.root)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With