user1401072
user1401072

Reputation:

findViewById in android doesn't return child layout

I inflate a view. Using the newly created layout, I find a child to set a property. But the child I get (mealName here) is not the good one. Having my mealList containing multiple mealName, it's always the first one that is returned.

RelativeLayout mealNameLayout = (RelativeLayout) inflater.inflate(R.layout.planner_list_item_meal_title, mealList);
TextView mealName = (TextView) mealNameLayout.findViewById(R.id.mealName);

//Set meal name.
mealName.setText(meal.name);

As you can see, mealNameLayout has only one child with the "mealName" id.

Any idea on what's going on?

The list inflated :

<LinearLayout
        android:padding="3dip"
        android:id="@+id/mealList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

    </LinearLayout>

and the item added :

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/mealName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_vertical"
        android:text="meatballs"
        android:textColorLink="@android:color/darker_gray"
        android:textSize="16sp" />

</LinearLayout>

Upvotes: 0

Views: 201

Answers (1)

Maxim Efimov
Maxim Efimov

Reputation: 2737

Looks like using findViewById when having multiple items with the same id is not a good idea.

Yes it is a bad idea. Consider redesigning your layouts to avoid multiple equal ids in one view.

Upvotes: 1

Related Questions