0

I am creating a linear layout programmatically but for some reason I am only able to see the first view. I can see that the space for the other views is being created and that this space is being created correctly. For example it has vertical orientation and holds 6 views. It is creating the space needed for the 6 views, and doing it vertically, but only showing the very first one.

I have seen a lot of answers for this but am unable to get it working. I have tried calling invalidate and postInvalidate on the linear layout and the parent layout but am not getting a refresh. I made sure the code is running on my Main UI thread. I have removed the view right before and then added it but to no avail. I am stuck on this one and can't seem to figure out where my linear layout is going wrong.

Here is some code showing how I've done this before and how I first tried:

     for (int i = 0; i < pdpComparisonAdapter.getCount(); i++) {
            final View item = pdpComparisonAdapter.getView(i, null, null);
            pdpComparisonsLinearLayout.addView(item);

    } 

Here is the Linear Layout xml:

        <LinearLayout
            android:layout_below="@+id/comparisons_title"
            android:id="@+id/comparisons_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:background="#ffffff"
            android:orientation="vertical"/>

Here is the view layout that I am trying to add:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <ImageView
        android:src="@drawable/notable_amenity_star"
        android:layout_marginLeft="10dp"
        android:layout_alignParentLeft="true"
        android:id="@+id/comparison_iv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />


    <LinearLayout
        android:layout_toRightOf="@+id/comparison_iv"
        android:layout_marginLeft="10dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <TextView
            android:id="@+id/comparison_value_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/comparison_label_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="24sp"/>

        <TextView
            android:id="@+id/comparison_explanation_tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</RelativeLayout>

I have also tried working the java code differently like these:

    for (int i = 0; i < pdpComparisonAdapter.getCount(); i++) {
        final View item = pdpComparisonAdapter.getView(i, null, null);

        item.setLayoutParams(new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        View oldView = pdpComparisonsLinearLayout.getChildAt(i);
        pdpComparisonsLinearLayout.removeView(oldView);
        pdpComparisonsLinearLayout.addView(item,i);
        pdpComparisonsLinearLayout.getChildAt(i).setVisibility(View.VISIBLE);
    }
    pdpComparisonsLinearLayout.invalidate();
SamIAmHarris
  • 2,158
  • 3
  • 16
  • 19
  • you are using an adapater incorrectly. The adapter is used to populate a listview or gridview. You should override getView in your own custom adapter rather than try to have the adapter return a view for you to manually place in a layout. Check out: http://developer.android.com/guide/topics/ui/layout/listview.html – Martin Oct 09 '14 at 17:54
  • That is a custom adapter (extends ArrayAdapter) and I overrode the getView method to return a custom built view. I thought it could be an issue with the adapter but I debugged the code and put some breakpoints in the adapter to see if that could be the issue but I didn't see a problem with it. I've done this before with a custom adapter and didn't see an issue. – SamIAmHarris Oct 09 '14 at 18:07

0 Answers0