ClassCastException ListView页脚

时间:2014-02-07 18:00:02

标签: android-listview

我正在尝试在Android列表视图上设置页脚视图。 listview使用自定义适配器,其中我显示两种类型的行,这两种行都符合java接口。设置页脚的代码如下所示:

    eventsList = (ListView)view.findViewById(R.id.events_list);

    // create view for footer
    LL = new LinearLayout(getSherlockActivity());
    LL.setOrientation(LinearLayout.VERTICAL);
    LayoutParams LLParams = new LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    LL.setLayoutParams(LLParams);

    eventsList.addFooterView(LL, null, false);

    // set up the array list for list rows and headers
    items = new ArrayList<ListItem>();

    loadEventsData();

    eventsAdapter = new TwoTypeArrayAdapter(getSherlockActivity(), items);

    eventsList.setAdapter(eventsAdapter);

我不断收到“java.lang.ClassCastException:android.app.ActionBar&amp; LayoutParams无法转换为android.widget.AbsListView $ LayoutParams”。该行引出错误:

 eventsList.setAdapter(eventsAdapter);

arrayAdapter的定义如下:

 public class TwoTypeArrayAdapter extends ArrayAdapter<ListItem> {

   LayoutInflater mInflater;

   public enum RowType {
    LIST_ITEM,
    HEADER_ITEM
   }

   public TwoTypeArrayAdapter(Context context, List<ListItem> items) {
    super(context, 0, items);

    mInflater = LayoutInflater.from(context);
   }

   @Override
   public int getViewTypeCount() {
       return RowType.values().length;
   }

   @Override
   public int getItemViewType(int position) {
       return getItem(position).getViewType();
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {

       return getItem(position).getView(mInflater, convertView);
   }
}

提前感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

非常简单。我刚刚在xml文件中创建了线性布局LL,然后将它的布局参数与其父级匹配:

LL.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT,ListView.LayoutParams.WRAP_CONTENT));