Unable to set margin of item in ListView using custom Adapter

Patrick Dattilio picture Patrick Dattilio · Aug 14, 2011 · Viewed 10.1k times · Source

I am attempting to set variable left margins for items in my ListView. The problem occurs in my custom Adapter in the getView method:

    public View getView(int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            Comment c = comments.get(position);
            convertView = inflater.inflate(R.layout.list_item, null);               
            (TextView)convertView.findViewById(R.id.story)).setText(c.message);


        }
            RelativeLayout.LayoutParams lp = new  RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
            lp.setMargins(0, c.getDepth() * 5, 0, 0);               
            convertView.setLayoutParams(lp);

        return convertView;
    }

We inflate the list_item layout which is a RelativeLayout with some textViews inside. I then attempt to set the margins using a RelativeLayout.LayoutParams. I get a class cast exception on this, but the normal LayoutParams type has no method for setting the margins.

What is the best way to go about setting the margin?

Here is the error

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams
at android.widget.ListView.setupChild(ListView.java:1779)
at android.widget.ListView.makeAndAddView(ListView.java:1748)
at android.widget.ListView.fillDown(ListView.java:670)
at android.widget.ListView.fillFromTop(ListView.java:727)
at android.widget.ListView.layoutChildren(ListView.java:1598)
at android.widget.AbsListView.onLayout(AbsListView.java:1273)
at android.view.View.layout(View.java:7192)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7192)
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:7192)
at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
at android.view.View.layout(View.java:7192)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1145)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1865)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
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:847)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
at dalvik.system.NativeStart.main(Native Method)

Answer

Patrick Dattilio picture Patrick Dattilio · Sep 26, 2011

I found the simplest way to solve this problem was to simply wrap my item in a FrameLayout in the xml. I could then easily set the left padding as I wished thus giving me the exact look I was hoping for.