2012-05-04 93 views
2

我這個addfooterview原因異常

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)); 

這是充氣的ListView我

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

我想擡高第二次但沒有成功。

但是我可以擡高firsttime這是

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

有什麼問題,爲什麼我得到這個錯誤?

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams 
+0

你可以用XML發佈總代碼嗎? – Veer

回答

1

嘗試修改此:

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

爲此:

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

對不起已故的答覆 我試着用這個 'linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslist,線性)); linear.addView(liInflater.inflate(R.layout.main_particularcategoryallnewslistfv,null));' 它返回我這個錯誤 '由於:java.lang.IllegalStateException:指定的子項已經有一個父。您必須先調用子對象的父對象的removeView()。 ' –

0

我想這是因爲你所指出的行不..

你叫getLayoutParams()在你的代碼的任何地方?

無論何時調用getLayoutParams(),都會顯示typecase父級layoutparams。

一樣,如果你的ListView的父的LinearLayout然後,

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

如果你的ListView的父RelativeLayout的話,

RelativeLayout.LayoutParams params=(RelativeLayout.LayoutParams) listView.getLayoutParams(); 
+0

我從來沒有叫getlayoutparam() –

0

我有類似的情況 - 在list_view.setAdapter()上出現異常「java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams」;

我已設法通過對頁腳和列表項使用相同的佈局來解決此問題。下面的代碼演示瞭如何在頁腳和項目中使用佈局「listview_row」。

這是 「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> 

頁腳以這樣的方式初始化:

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); 

適配器:

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 
     ..... 
    } 

這不是理想的解決方案 - 只解決方法,當然。