Search code examples
androidlistviewandroid-custom-view

get textview from custom item in listvew


I have a ListView and this custom row:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="TextView" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:id="@+id/lblMsg"></TextView>
    <TextView android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="TextView" android:layout_width="wrap_content"
        android:layout_height="wrap_content" android:textColor="#00f"
        android:id="@+id/lblNumber"></TextView>
</LinearLayout>

I want to get the TextView:

for (int i = 0; i < lvMsg.getCount(); i++) {
    v = lvMsg.getChildAt(i);
    TextView vs=(TextView)v.findViewById(R.id.lblMsg);
}

but it returns null.

How can I fix this?


Solution

  • Your question refers to a listview, your code does not have a listview. Because a listview can have many rows or items find view by id does not work. use findviewbytag. I assume the xml part of your code is a single row of your listview so you would find viewbytag to get the single row a linear layout in this case then findviewbyid