2

i inflate by this

linear = (LinearLayout) findViewById(R.id.layout_content);
    linear.setVisibility(View.VISIBLE);
LayoutInflater liInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist, null));
    linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslistfv, null));

This is my listview

btnmore = (Button)findViewById(R.id.btn_more);
getListView().addFooterView(btnmore);
lv.setAdapter(adapter);

I want to inflate second time but failed.

However i can inflate firsttime which was

linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist, null));

What is the problem why i get this error?

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
Alan Lai
  • 1,094
  • 7
  • 18
  • 41

3 Answers3

1

Try changing this:

linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist,
      null));

To this:

linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist,
      linear));
waqaslam
  • 67,549
  • 16
  • 165
  • 178
  • sorry for late reply i tried with this `linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist, linear)); linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslistfv, null));` it return me this error `Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. ` – Alan Lai May 07 '12 at 03:40
0

i think It's not because of the line you pointed out..

Did you call getLayoutParams() anywhere in your code?

Whenver you call getLayoutParams(), you show typecase to parent layoutparams.

Like, if your ListView's parent is LinearLayout then,

LinearLayout.LayoutParams params=(LinearLayout.LayoutParams) listView.getLayoutParams();

If your ListView's parent is RelativeLayout then,

RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams) listView.getLayoutParams();
Veer
  • 2,071
  • 19
  • 24
0

I had similar situation - there was exception "java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams" on list_view.setAdapter();

I have managed to workaround this problem by using same layout for both footer and list items. The code below demonstrates how to use layout "listview_row" in footer and in items.

This is content of "listview_row.xml":

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/hs_line" 
android:layout_alignParentLeft ="true"
android:layout_width="fill_parent" 
android:layout_height="wrap_content">

<!-- TextView for list items -->
<TextView android:id="@+id/hs_line_textview" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_alignParentLeft="true"       
    />

<!-- Button for footer -->
<Button android:id="@+id/hs_line_footer_action" 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:visibility="gone"
    android:layout_alignParentLeft="true"       
/>
</RelativeLayout>

Footer is initialized in such way:

View v = View.inflate(pager.Activity, R.layout.listview_row,  null);
View footer = (View) v.findViewById(R.id.listview_line);

//footer doesn't use TextView, it uses Button only. So, we hide TextView.
footer.findViewById(R.id.hs_line_textview).setVisibility(View.GONE); 
footer.findViewById(R.id.hs_line_footer_action).setVisibility(View.VISIBLE)
...
list_view.addFooterView(footer);

MyAdapter<Item> adapter = new MyAdapter(context, getListItems());
list_view.setAdapter(adapter);

Adapter:

public class MyAdapter<T> extends ArrayAdapter<T> {
private final ArrayList<T> _List;
private final LayoutInflater _Inflater;

public MyAdapter(Context context, ArrayList<T> srcList) {
    super(context, 0, srcList);
    _Inflater = LayoutInflater.from(context);
    _List = srcList;
}

    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;

        if (convertView == null) {    
            convertView = _Inflater.inflate(R.layout.listview_row, null);
            holder = new ViewHolder();

        //item doesn't use Button, it uses TextView only   
        //Button is hidden by default (see xml)
            holder.TextView = (TextView) convertView.findViewById(R.id.hs_line_textview);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        //item initialization 
        .....
   }

It's not ideal solution - only workaround, of course.

dvpublic
  • 657
  • 8
  • 8