LinearLayout inside card view cannot retrieved

509 views Asked by At

I am using ListView with BaseAdapter. In tablets only, I cannot get the view of the LinearLayout(which is inside a CardView) in getView method of the BaseAdapter.

if(convertView == null) {
    convertView = activity.getLayoutInflater().inflate(layout, null);
}          

((TextView)convertView.findViewById(R.id.title)).setText(post.getTitle();

View view = convertView.findViewById(R.id.child_view);
if (view != null)
    Log.e("ssssssss","view != null");
else {
    Log.e("ssssssss","view == nullllllllllllllllll");
}

Its always gives null. I can get R.id.card_view. But, I can't get R.id.child_view & R.id.inner_child_view.

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        style="@style/CardViewNormal"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="4dp"
        card_view:cardCornerRadius="2dp">

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

            <LinearLayout
                android:id="@+id/inner_child_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="5dp"
                android:orientation="horizontal">

                <ImageView
                    android:id="@+id/calendar_icon"
                    android:layout_width="12dp"
                    android:layout_height="12dp"
                    android:layout_marginRight="3dp"
                    android:tint="#aaa"
                    android:layout_gravity="center_vertical"
                    android:src="@drawable/ic_event_black_18dp"/>

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:textStyle="bold"
                    android:textColor="#aaa"
                    android:text="2015-09-25"
                    android:textSize="12sp"
                    android:layout_toRightOf="@id/calendar_icon"
                    android:layout_gravity="center_vertical"
                    android:layout_height="wrap_content" />

            </LinearLayout>

            <TextView
                android:id="@+id/title"
                android:layout_width="wrap_content"
                android:paddingRight="10dp"
                android:textSize="16sp"
                android:textColor="@color/black"
                android:maxLines="2"
                android:ellipsize="end"
                android:layout_marginBottom="10dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:text="Expected Questions: Seating Arrangements 1234 1234567890123456123465789123456789"
                android:layout_height="wrap_content"/>

            <TextView
                android:id="@+id/category"
                android:text="Alerts"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingTop="2dp"
                android:paddingBottom="2dp"
                android:textSize="12sp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </LinearLayout>

    </android.support.v7.widget.CardView>
</LinearLayout>

CardView version I am using is v7:24.2.1

1

There are 1 answers

0
iDeveloper On

you can see the result here : enter image description here

I tested Your code : modified it a little bit (not much) but it works properly on tablet too. the response is : E/ssssssss: view != null

the code (your modified code) I used to test :

    Log.e("ssssssss","inside oncreate");
    View view = findViewById(R.id.child_view);
    if (view != null)
        Log.e("ssssssss","view != null");
    else {
        Log.e("ssssssss","view == nullllllllllllllllll");
    }

the xml :

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

    <android.support.v7.widget.CardView
        android:id="@+id/card_view"
        card_view:cardBackgroundColor="@android:color/white"
        card_view:cardPreventCornerOverlap="false"
        card_view:cardUseCompatPadding="true"
        card_view:cardElevation="4dp"
        card_view:cardCornerRadius="2dp"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

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

        <LinearLayout
            android:id="@+id/inner_child_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/calendar_icon"
                android:layout_width="12dp"
                android:layout_height="12dp"
                android:layout_marginRight="3dp"
                android:tint="#aaa"
                android:layout_gravity="center_vertical"
                android:src="@drawable/ic_cast_dark"/>

            <TextView
                android:id="@+id/date"
                android:layout_width="wrap_content"
                android:textStyle="bold"
                android:textColor="#aaa"
                android:text="2015-09-25"
                android:textSize="12sp"
                android:layout_toRightOf="@id/calendar_icon"
                android:layout_gravity="center_vertical"
                android:layout_height="wrap_content" />

        </LinearLayout>

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:paddingRight="10dp"
            android:textSize="16sp"
            android:textColor="@color/cardview_dark_background"
            android:maxLines="2"
            android:ellipsize="end"
            android:layout_marginBottom="10dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:text="Expected Questions: Seating Arrangements 1234 1234567890123456123465789123456789"
            android:layout_height="wrap_content"/>

        <TextView
            android:id="@+id/category"
            android:text="Alerts"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingTop="2dp"
            android:paddingBottom="2dp"
            android:textSize="12sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        </LinearLayout>
    </android.support.v7.widget.CardView>
</LinearLayout>