Question

I'm currently making an application that uses a dynamic layout. It's my first time doing this and I'm doing it solely for the purpose of learning it. I managed to get it to load most of the data and show it correctly, but I'm having issues with the first item. For some reason it skips it and start with the 2nd item. It loads it into a variable but for some reason it isn't shown. I believe it has something to do with layout parameters but I can not find a reason for it behaving that way.

This is how it looks (take note, it should start with 141- ... item. It is loaded into a variable tho but not shown)

enter image description here

This is the code I'm using to load the items according to the variables

for (int i = 0; i <= id_l; i++)
            {
                RelativeLayout container = (RelativeLayout)findViewById(R.id.popisLinija);
                View item_layout = getLayoutInflater().inflate(R.layout.item_layout, null);
                Integer l = id_l;
                Log.d("id length: ", l.toString());
                Log.d("ID: ", id[i].toString());
                Log.d("Linija br: ", broj_linije[i]);
                Integer i2 = i;
                Log.d("i= ", i2.toString());

                item_layout.setId(i);
                item_layout.setTag("View");

                final Integer h = item_layout.getId();

                // fill in any details dynamically here
                assert item_layout != null;
                TextView textView = (TextView) item_layout.findViewById(R.id.item_text);

                if(broj_linije[i].equals("000"))
                {
                    textView.setText(naziv_linije[i]);
                }
                else
                {
                    textView.setText(broj_linije[i] + " - " + naziv_linije[i]);
                }


                textView.setTag("Text");


                RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                if(i == 0)
                {
                    if (rlp != null) {
                        rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP, container.getId());
                    }
                }
                if(i > 0)
                {
                    if (rlp != null) {
                        rlp.addRule(RelativeLayout.BELOW, i-1);
                    }
                }


                FrameLayout item_button = (FrameLayout) item_layout.findViewById(R.id.item_button);
                item_button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View arg0)
                    {
                        Integer a =  h;
                        Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show();
                        Log.d("clicked", h.toString());
                    }
                });

                item_layout.setLayoutParams(rlp);
                container.addView(item_layout);
            }

I have checked if the item is loaded

> 05-02 07:09:00.116  21204-21204/com.example.app D/ID:﹕ 0 05-02 <<< ITEM 141 VARIABLE
> 07:09:00.116  21204-21204/com.example.app D/Linija br:﹕ 141 05-02 <<< ITEM 141 VARIABLE
> 07:09:00.116  21204-21204/com.example.app D/i=﹕ 0 05-02 07:09:00.166  <<< ITEM 141 VARIABLE
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.166 
> 21204-21204/com.example.app D/ID:﹕ 1 05-02 07:09:00.166 
> 21204-21204/com.example.app D/Linija br:﹕ 142 05-02 07:09:00.166 
> 21204-21204/com.example.app D/i=﹕ 1 05-02 07:09:00.176 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.176 
> 21204-21204/com.example.app D/ID:﹕ 2 05-02 07:09:00.176 
> 21204-21204/com.example.app D/Linija br:﹕ 143 05-02 07:09:00.176 
> 21204-21204/com.example.app D/i=﹕ 2 05-02 07:09:00.206 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.206 
> 21204-21204/com.example.app D/ID:﹕ 3 05-02 07:09:00.206 
> 21204-21204/com.example.app D/Linija br:﹕ 144 05-02 07:09:00.206 
> 21204-21204/com.example.app D/i=﹕ 3 05-02 07:09:00.216 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.216 
> 21204-21204/com.example.app D/ID:﹕ 4 05-02 07:09:00.216 
> 21204-21204/com.example.app D/Linija br:﹕ 145 05-02 07:09:00.216 
> 21204-21204/com.example.app D/i=﹕ 4 05-02 07:09:00.226 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.226 
> 21204-21204/com.example.app D/ID:﹕ 5 05-02 07:09:00.226 
> 21204-21204/com.example.app D/Linija br:﹕ 146 05-02 07:09:00.226 
> 21204-21204/com.example.app D/i=﹕ 5 05-02 07:09:00.246 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.246 
> 21204-21204/com.example.app D/ID:﹕ 6 05-02 07:09:00.246 
> 21204-21204/com.example.app D/Linija br:﹕ 147 05-02 07:09:00.246 
> 21204-21204/com.example.app D/i=﹕ 6 05-02 07:09:00.276 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.276 
> 21204-21204/com.example.app D/ID:﹕ 7 05-02 07:09:00.276 
> 21204-21204/com.example.app D/Linija br:﹕ 148 05-02 07:09:00.276 
> 21204-21204/com.example.app D/i=﹕ 7 05-02 07:09:00.316 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.316 
> 21204-21204/com.example.app D/ID:﹕ 8 05-02 07:09:00.316 
> 21204-21204/com.example.app D/Linija br:﹕ 149 05-02 07:09:00.316 
> 21204-21204/com.example.app D/i=﹕ 8 05-02 07:09:00.316 
> 21204-21208/com.example.app D/dalvikvm﹕ GC_CONCURRENT freed 949K, 37%
> free 7958K/12524K, paused 9ms+17ms, total 92ms 05-02 07:09:00.326 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.326 
> 21204-21204/com.example.app D/ID:﹕ 9 05-02 07:09:00.326 
> 21204-21204/com.example.app D/Linija br:﹕ 150 05-02 07:09:00.326 
> 21204-21204/com.example.app D/i=﹕ 9 05-02 07:09:00.336 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.336 
> 21204-21204/com.example.app D/ID:﹕ 10 05-02 07:09:00.336 
> 21204-21204/com.example.app D/Linija br:﹕ 150A 05-02 07:09:00.336 
> 21204-21204/com.example.app D/i=﹕ 10 05-02 07:09:00.346 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.346 
> 21204-21204/com.example.app D/ID:﹕ 11 05-02 07:09:00.346 
> 21204-21204/com.example.app D/Linija br:﹕ 151 05-02 07:09:00.346 
> 21204-21204/com.example.app D/i=﹕ 11 05-02 07:09:00.366 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.366 
> 21204-21204/com.example.app D/ID:﹕ 12 05-02 07:09:00.366 
> 21204-21204/com.example.app D/Linija br:﹕ 153 05-02 07:09:00.366 
> 21204-21204/com.example.app D/i=﹕ 12 05-02 07:09:00.376 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.376 
> 21204-21204/com.example.app D/ID:﹕ 13 05-02 07:09:00.376 
> 21204-21204/com.example.app D/Linija br:﹕ 155 05-02 07:09:00.376 
> 21204-21204/com.example.app D/i=﹕ 13 05-02 07:09:00.386 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.386 
> 21204-21204/com.example.app D/ID:﹕ 14 05-02 07:09:00.386 
> 21204-21204/com.example.app D/Linija br:﹕ 156 05-02 07:09:00.386 
> 21204-21204/com.example.app D/i=﹕ 14 05-02 07:09:00.396 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.396 
> 21204-21204/com.example.app D/ID:﹕ 15 05-02 07:09:00.396 
> 21204-21204/com.example.app D/Linija br:﹕ 157 05-02 07:09:00.396 
> 21204-21204/com.example.app D/i=﹕ 15 05-02 07:09:00.406 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.406 
> 21204-21204/com.example.app D/ID:﹕ 16 05-02 07:09:00.406 
> 21204-21204/com.example.app D/Linija br:﹕ 158 05-02 07:09:00.406 
> 21204-21204/com.example.app D/i=﹕ 16 05-02 07:09:00.416 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.416 
> 21204-21204/com.example.app D/ID:﹕ 17 05-02 07:09:00.416 
> 21204-21204/com.example.app D/Linija br:﹕ 159 05-02 07:09:00.416 
> 21204-21204/com.example.app D/i=﹕ 17 05-02 07:09:00.426 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.426 
> 21204-21204/com.example.app D/ID:﹕ 18 05-02 07:09:00.436 
> 21204-21204/com.example.app D/Linija br:﹕ 160 05-02 07:09:00.436 
> 21204-21204/com.example.app D/i=﹕ 18 05-02 07:09:00.436 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.436 
> 21204-21204/com.example.app D/ID:﹕ 19 05-02 07:09:00.436 
> 21204-21204/com.example.app D/Linija br:﹕ 162 05-02 07:09:00.436 
> 21204-21204/com.example.app D/i=﹕ 19 05-02 07:09:00.446 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.446 
> 21204-21204/com.example.app D/ID:﹕ 20 05-02 07:09:00.446 
> 21204-21204/com.example.app D/Linija br:﹕ 260 05-02 07:09:00.446 
> 21204-21204/com.example.app D/i=﹕ 20 05-02 07:09:00.456 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.456 
> 21204-21204/com.example.app D/ID:﹕ 21 05-02 07:09:00.456 
> 21204-21204/com.example.app D/Linija br:﹕ 260A 05-02 07:09:00.456 
> 21204-21204/com.example.app D/i=﹕ 21 05-02 07:09:00.476 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.476 
> 21204-21204/com.example.app D/ID:﹕ 22 05-02 07:09:00.476 
> 21204-21204/com.example.app D/Linija br:﹕ 261 05-02 07:09:00.476 
> 21204-21204/com.example.app D/i=﹕ 22 05-02 07:09:00.486 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.486 
> 21204-21204/com.example.app D/ID:﹕ 23 05-02 07:09:00.486 
> 21204-21204/com.example.app D/Linija br:﹕ 262 05-02 07:09:00.486 
> 21204-21204/com.example.app D/i=﹕ 23 05-02 07:09:00.496 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.496 
> 21204-21204/com.example.app D/ID:﹕ 24 05-02 07:09:00.496 
> 21204-21204/com.example.app D/Linija br:﹕ 263 05-02 07:09:00.496 
> 21204-21204/com.example.app D/i=﹕ 24 05-02 07:09:00.506 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.506 
> 21204-21204/com.example.app D/ID:﹕ 25 05-02 07:09:00.506 
> 21204-21204/com.example.app D/Linija br:﹕ 264 05-02 07:09:00.506 
> 21204-21204/com.example.app D/i=﹕ 25 05-02 07:09:00.516 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.516 
> 21204-21204/com.example.app D/ID:﹕ 26 05-02 07:09:00.516 
> 21204-21204/com.example.app D/Linija br:﹕ 265 05-02 07:09:00.516 
> 21204-21204/com.example.app D/i=﹕ 26 05-02 07:09:00.526 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.526 
> 21204-21204/com.example.app D/ID:﹕ 27 05-02 07:09:00.526 
> 21204-21204/com.example.app D/Linija br:﹕ 266 05-02 07:09:00.526 
> 21204-21204/com.example.app D/i=﹕ 27 05-02 07:09:00.536 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.536 
> 21204-21204/com.example.app D/ID:﹕ 28 05-02 07:09:00.536 
> 21204-21204/com.example.app D/Linija br:﹕ 267 05-02 07:09:00.536 
> 21204-21204/com.example.app D/i=﹕ 28 05-02 07:09:00.546 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.546 
> 21204-21204/com.example.app D/ID:﹕ 29 05-02 07:09:00.546 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.546 
> 21204-21204/com.example.app D/i=﹕ 29 05-02 07:09:00.556 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.556 
> 21204-21204/com.example.app D/ID:﹕ 30 05-02 07:09:00.556 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.556 
> 21204-21204/com.example.app D/i=﹕ 30 05-02 07:09:00.566 
> 21204-21204/com.example.app D/id length:﹕ 31 05-02 07:09:00.566 
> 21204-21204/com.example.app D/ID:﹕ 31 05-02 07:09:00.566 
> 21204-21204/com.example.app D/Linija br:﹕ 000 05-02 07:09:00.566 
> 21204-21204/com.example.app D/i=﹕ 31

Could anyone help me out on what am I doing wrong here? I would greatly appreciate it, I lost hours on this one trying all sorts of stuff with no result.

EDIT:

I have added my layout XML file

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

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="wrap_content"
    android:background="#ffffff"
    android:id="@+id/item_container"
    android:clickable="false"
    android:focusable="false">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:padding="2dp"
        android:background="#e8e8e8"
        android:id="@+id/item_body"
        android:clickable="false"
        android:focusable="false">

        <FrameLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@color/background"
            android:id="@+id/item_button"
            android:clickable="true"
            android:longClickable="true"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:minHeight="55dp">

                <FrameLayout
                    android:layout_width="3dp"
                    android:layout_height="fill_parent"
                    android:background="@android:color/transparent"
                    android:id="@+id/item_indicator"></FrameLayout>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="141-SAMOBOR-RAKOV POTOK-JAGNJIĆ DOL"
                    android:id="@+id/item_text"
                    android:layout_gravity="center_vertical"
                    android:layout_marginLeft="8dp"
                    android:layout_marginRight="30dp"
                    android:textAppearance="@android:style/TextAppearance.Small"
                    android:gravity="fill" />

                <ImageView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:id="@+id/item_icon"
                    android:layout_gravity="center_vertical|right"
                    android:background="@android:color/transparent"
                    android:src="@drawable/plus_t"
                    android:layout_marginRight="5dp"
                    android:contentDescription="Proširi" />

            </FrameLayout>
        </RelativeLayout>

        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:id="@+id/item_image_container"
            android:layout_below="@+id/item_body"
            android:layout_marginTop="5dp"
            android:background="#ffffff">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:id="@+id/item_image"
                android:background="@android:color/transparent"
                android:layout_centerHorizontal="true"
                android:scaleType="fitCenter" />
        </RelativeLayout>

</RelativeLayout>
Was it helpful?

Solution

try this way,hope this will help you...

XML code

activity_main

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="180dp"
        android:src="@drawable/ic_launcher"
        android:adjustViewBounds="true"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/lnrDynamicItem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

        </LinearLayout>
    </ScrollView>

</LinearLayout>

ACTIVITY code

MainActivity

  public class MainActivity extends Activity {

    private LinearLayout lnrDynamicItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        lnrDynamicItem = (LinearLayout) findViewById(R.id.lnrDynamicItem);

        for (int i = 0; i <= id_l; i++)
        {
            Integer l = id_l;
            Log.d("id length: ", l.toString());
            Log.d("ID: ", id[i].toString());
            Log.d("Linija br: ", broj_linije[i]);
            Integer i2 = i;
            Log.d("i= ", i2.toString());

            item_layout.setId(i);
            item_layout.setTag("View");


            TextView textView = new TextView(this);
            textView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            textView.setId(i+1);

            if(broj_linije[i].equals("000"))
            {
                textView.setText(naziv_linije[i]);
            }
            else
            {
                textView.setText(broj_linije[i] + " - " + naziv_linije[i]);
            }


            textView.setTag(textView.getId());



            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view)
                {
                    Integer a =  (Integer) view.getTag();
                    Toast.makeText(getApplicationContext(), "Clicked me", Toast.LENGTH_SHORT).show();
                    Log.d("clicked", h.toString());
                }
            });

            lnrDynamicItem.addView(textView);
        }

    }

 }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top