androidlistviewbuttonandroid-cursoradapter

Handle Button Clicks in CursorAdapter


I want to achieve drop down and sum up for a View in my ListView Data is being fetched from the database and am using a cursor adapter, I have tried to add onClickListner for the button in onItemClickListner But no Success.
The problem I am facing is When button DROPDOWN is clicked it will make relative layout Visible but when clicked again it won't relative layout visibility to GONE

Heres my Code for CursorAdapter

public class ProductViewCursorAdapter extends CursorAdapter {
    private CardView cv_singleItem;
    private RelativeLayout infoLayout;
    private Button dropDown;
    public ProductViewCursorAdapter(Context context, Cursor c) {
        super(context, c, 0);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.custom_product_view_listview,parent,false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        cv_singleItem = view.findViewById(R.id.cv_custom_single_product);
        infoLayout = view.findViewById(R.id.layout_dropdown_relative);
        dropDown = view.findViewById(R.id.expand_user_info_listView);
        dropDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (infoLayout.getVisibility() == View.GONE){
                    TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
                    infoLayout.setVisibility(View.VISIBLE);
                    dropDown.setBackgroundResource(R.drawable.ic_sumup);
                }else{
                    Log.i("THIS IS TEST","DEMO VIEW "+ infoLayout.getVisibility());
                    TransitionManager.beginDelayedTransition(cv_singleItem,new AutoTransition());
                    infoLayout.setVisibility(View.GONE);
                    dropDown.setBackgroundResource(R.drawable.ic_dropdown);
                }
            }
        });

    }
}

ListView single View XML

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

    <androidx.cardview.widget.CardView
        android:id="@+id/cv_custom_single_product"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        style="@style/TextAppearance.AppCompat.Title">
        <LinearLayout
            android:background="#D5000000"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <RelativeLayout
                android:id="@+id/image_layout_main"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <ImageView
                    android:id="@+id/iv_product_image_listView"
                    android:layout_width="match_parent"
                    android:scaleType="fitCenter"
                    android:src="@drawable/selimage"
                    android:layout_height="200dp"/>
                <TextView
                    android:id="@+id/tv_no_image_available"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="No Image Available"
                    android:textAlignment="center"
                    android:paddingTop="5dp"
                    android:textSize="15sp"
                    android:textStyle="bold|italic"
                    android:textColor="@color/white"/>

                <TextView
                    android:id="@+id/tv_product_name_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Kurti"
                    android:textSize="18sp"
                    android:textColor="#FFFFFF"
                    android:fontFamily="sans-serif-black"
                    android:textStyle="italic"
                    android:layout_marginLeft="20dp"
                    android:layout_below="@id/iv_product_image_listView"
                    android:layout_marginTop="-25dp"/>
                <Button
                    android:id="@+id/expand_user_info_listView"
                    android:layout_alignParentEnd="true"
                    android:layout_below="@id/iv_product_image_listView"
                    android:focusable="false"
                    android:layout_marginTop="-49dp"
                    android:layout_marginEnd="8dp"
                    android:layout_width="50dp"
                    android:layout_height="50dp"
                    android:background="@drawable/ic_dropdown"/>
            </RelativeLayout>
            <RelativeLayout
                android:id="@+id/layout_dropdown_relative"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:visibility="gone"
                android:layout_marginStart="15dp"
                android:layout_marginEnd="15dp"
                android:layout_marginTop="10dp"
                android:layout_marginBottom="10dp">
                <TextView
                    android:id="@+id/tv_date_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="2020-12-14"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"/>
                <TextView
                    android:id="@+id/tv_profit_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Profit 500"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_alignParentEnd="true"/>

                <TextView
                    android:id="@+id/tv_sellingPrice_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Selling Price: 800"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_below="@id/tv_date_dropdown_listView"
                    android:paddingTop="3dp"/>

                <TextView
                    android:id="@+id/tv_actualPrice_dropDown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Actual Price: 850"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#E2FFFFFF"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_alignParentRight="true"
                    android:layout_below="@id/tv_profit_dropdown_listView"
                    android:paddingTop="3dp"/>

                <TextView
                    android:id="@+id/tv_pending_dropdown_listView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Pending: 0"
                    android:textAppearance="?android:textAppearanceMedium"
                    android:textColor="#F50057"
                    android:fontFamily="sans-serif-condensed"
                    android:textStyle="bold"
                    android:layout_marginStart="4dp"
                    android:layout_below="@id/tv_sellingPrice_dropdown_listView"
                    android:paddingTop="3dp"/>

                <Button
                    android:id="@+id/btn_update_single_item"
                    android:layout_width="30dp"
                    android:layout_height="30dp"
                    android:layout_below="@id/tv_actualPrice_dropDown_listView"
                    android:layout_alignParentEnd="true"
                    android:background="@drawable/ic_update"/>
            </RelativeLayout>
        </LinearLayout>

    </androidx.cardview.widget.CardView>

</RelativeLayout>

Solution

  • You are using the same infoLayout for every list view row. Every time bindView() gets called you replace the infoLayout with new layout. This of course causes the problem that when you use infoLayout.setVisibility(View.VISIBLE); the infoLayout it's not the same layout it was when the list view row was created because you have replaced it.

    So replace,

    infoLayout = view.findViewById(R.id.layout_dropdown_relative);

    with

    final RelativeLayout infoLayout = view.findViewById(R.id.layout_dropdown_relative);