使用Slide ExpandableListAdapter的ClassCastException

时间:2013-08-01 04:59:00

标签: android expandablelistview android-listfragment expandablelistadapter

我正在从Tjerk Wolternik实现Slide ExpandableListAdapter,但是当我正在使用它的列表被调用时,我得到了一个ClassCastException。我希望有人可以帮我弄清楚为什么会这样。

我在自定义数组适配器上使用它,如下所示:

public class ResultsAdapter extends ArrayAdapter<String> {

private final LayoutInflater layoutInflater;

public ResultsAdapter(Context context, int textViewResourceId) {
       super(context, textViewResourceId);
       layoutInflater = LayoutInflater.from(context);
}

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

    if(convertView == null){            
        convertView = layoutInflater.inflate(R.layout.row, null); //must be overall layout  
    }


    TextView venName = (TextView) convertView.findViewById(R.id.rowName);
    TextView venAddress = (TextView) convertView.findViewById(R.id.rowAdd);

    venName.setText(VenueList.getVenueName(position));
    venAddress.setText(VenueList.getVenueAddress(position));

    return convertView;
}

}

我正在使用的ListView是ListFragment的扩展,它就在这里:

public class ListFragmentClickable extends ListFragment{

  private OnItemSelectedListener listener;

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
      View view = inflater.inflate(R.layout.activity_results, container, false);
      return view;
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
      updateDetail(position); //see bottom
  } 

  public interface OnItemSelectedListener {
      public void onItemSelected(String name);
  }


  @Override
  public void onAttach(Activity activity) {
      super.onAttach(activity);
      if (activity instanceof OnItemSelectedListener) {
        listener = (OnItemSelectedListener) activity;
      } else {
        throw new ClassCastException(activity.toString()
            + " must implement ListFragmentClickable.OnItemSelectedListener");
      }
  }

  public void updateDetail(int position) {
      String name = VenueList.getVenueName(position);
      listener.onItemSelected(name);
  }

}

logcat指向库部分代码(具体为final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)target.getLayoutParams();):

    private void updateExpandable(View target, int position) {

    final LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)target.getLayoutParams();
    if(openItems.get(position)) {
        target.setVisibility(View.VISIBLE);
        params.bottomMargin = 0;
    } else {
        target.setVisibility(View.GONE);
        params.bottomMargin = 0-viewHeights.get(position);
    }
}

这是我的logcat:

07-31 23:42:20.931: E/AndroidRuntime(3557): FATAL EXCEPTION: main
07-31 23:42:20.931: E/AndroidRuntime(3557): java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
07-31 23:42:20.931: E/AndroidRuntime(3557):     at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.updateExpandable(AbstractSlideExpandableListAdapter.java:222)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.enableFor(AbstractSlideExpandableListAdapter.java:159)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.enableFor(AbstractSlideExpandableListAdapter.java:142)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at com.tjerkw.slideexpandable.library.AbstractSlideExpandableListAdapter.getView(AbstractSlideExpandableListAdapter.java:64)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.AbsListView.obtainView(AbsListView.java:1449)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.ListView.onMeasure(ListView.java:1103)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:683)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:311)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:531)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:309)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3261)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:250)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.View.measure(View.java:8532)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewRoot.performTraversals(ViewRoot.java:915)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.os.Looper.loop(Looper.java:150)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at android.app.ActivityThread.main(ActivityThread.java:4358)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at java.lang.reflect.Method.invokeNative(Native Method)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at java.lang.reflect.Method.invoke(Method.java:507)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
07-31 23:42:20.931: E/AndroidRuntime(3557):     at dalvik.system.NativeStart.main(Native Method)

任何人都可以向我解释发生了什么事吗?我在布局中使用了线性布局,所以我不确定播放问题是什么。我的布局完全遵循github链接中的内容。谢谢你的帮助。

编辑:这里也是row.xml:

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

<TextView
    android:id="@+id/rowName"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textStyle="bold"
    android:textSize="16dp"
    android:focusable="false"
    android:paddingLeft="5dp" />

<TextView
    android:id="@+id/rowAdd"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@id/rowName"
    android:focusable="false"
    android:paddingTop="2dp"
    android:paddingLeft="5dp"/>

<TextView
    android:id="@+id/rowFit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:paddingTop="5dp"
    android:paddingRight="10dp"
    android:focusable="false"
    android:textStyle="bold"
    android:textSize="18dp" />

<TextView
    android:id="@+id/rowDist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:paddingBottom="5dp"
    android:paddingRight="10dp"
    android:focusable="false"/> 

<!-- below this is xml for slide expandablelistview -->
<Button
    android:id="@+id/expandable_toggle_button"
    android:text="More"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/text"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/text"/>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/expandable"
    android:background="#000000">

    <!-- put whatever you want in the expandable view -->
    <TextView
        android:id="@+id/testtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:text="testtestest"
        android:paddingRight="10dp"
        android:focusable="false"
        android:textStyle="bold"
        android:textSize="18dp" />

</LinearLayout>

<!-- slide expandablelistview xml ends here -->

1 个答案:

答案 0 :(得分:0)

你的row.xml看起来并不完整。我相信您正在使用的库期望row.xml是一个包含2个子元素的单线性布局。第一个孩子是未展开的视图,第二个孩子是扩展视图。像下面的东西(我还没有验证这是有效的布局)。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:slide="http://schemas.android.com/apk/res/com.tjerkw.slideexpandable.library"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical">
<RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:id="@+id/item">
<TextView
    android:id="@+id/rowName"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:textStyle="bold"
    android:textSize="16dp"
    android:focusable="false"
    android:paddingLeft="5dp" />

<TextView
    android:id="@+id/rowAdd"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@id/rowName"
    android:focusable="false"
    android:paddingTop="2dp"
    android:paddingLeft="5dp"/>

<TextView
    android:id="@+id/rowFit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:paddingTop="5dp"
    android:paddingRight="10dp"
    android:focusable="false"
    android:textStyle="bold"
    android:textSize="18dp" />

<TextView
    android:id="@+id/rowDist"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:paddingBottom="5dp"
    android:paddingRight="10dp"
    android:focusable="false"/> 

<!-- below this is xml for slide expandablelistview -->
<Button
    android:id="@+id/expandable_toggle_button"
    android:text="More"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignBottom="@+id/text"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@id/text"/>
</RelativeLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal"
    android:id="@+id/expandable"
    android:background="#000000">

    <!-- put whatever you want in the expandable view -->
    <TextView
        android:id="@+id/testtext"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:text="testtestest"
        android:paddingRight="10dp"
        android:focusable="false"
        android:textStyle="bold"
        android:textSize="18dp" />

</LinearLayout>
</LinearLayout>

<!-- slide expandablelistview xml ends here -->

您还可以查看示例expandable_list_item.xml