开发者

Android adapter returning LinearLayout

I have a custom adapter for ListView and I need to return LinearLayout from getView(). The reason I'm doing it is that I have custom component which extends LinearLayout and need to be created from code. The problem is that if I return any LinearLayout from getView(), the application crashes. How can I get it working? I can't inflate the View from xml, everything has to be done via code.

Here's the getView():

@Override
public View getView(int position, View convertView, ViewGroup parent)
{
  LinearLayout lay = new LinearLayout(MyApplication.getContext()); // gets the context from extended Application class
  lay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
  return lay;
}

And log of what happens:

08-20 08:40:07.886: DEBUG/AndroidRuntime(13039): Shutting down VM
08-20 08:40:07.896: WARN/dalvikvm(13039): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
08-20 08:40:07.926: ERROR/AndroidRuntime(13039): FATAL EXCEPTION: main
        java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams
        at android.widget.ListView.setupChild(ListView.java:1870)
        at android.widget.ListView.makeAndAddView(ListView.java:1839)
        at android.widget.ListView.fillDown(ListView.java:670)
        at android.widget.ListView.fillFromTop(ListView.java:727)
        at android.widget.ListView.layoutChildren(ListView.java:1689)
        at android.widget.AbsListView.onLayout(AbsListView.java:1260)
        at android.view.View.layout(View.java:7278)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
        at android.view.View.layout(View.java:7278)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
        at android.view.View.layout(View.java:7278)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
        at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1243)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1049)
        at android.view.View.layout(View.java:7278)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
        at android.view.View.layout(View.java:7278)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
        at android.view.View.layout(View.java:7278)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
        at android.view.View.layout(View.java:7278)
        at android.view.ViewRoot.performTraversals(ViewRoot.java:1172)
        at android.view.ViewRoot.handleMessage(ViewRoot.java:1921)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:143)
        at android.app.ActivityThread.main(ActivityThread.java:4196)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:507)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        at dalvik.system.NativeStart.main(Na开发者_JAVA百科tive Method)

Thanks

// edit: If somebody could provide me with a working sample which creates LinearLayout via code and returns it from getView(), it would most likely solve this problem


Set AbsListView.LayoutParams instead of LinearLayout.LayoutParams.
LayoutParams are to be set for a child in ViewGroup. Since your layout is a child of AbsListView you should set its layoutparams.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜