How to write Android PowerSpinner Adapter class and attach onItemClickListener on it

48 views Asked by At

I ran into a problem I can't solve, so I would be grateful if someone could help.

I have been using Android PowerSpinner component (https://medium.com/swlh/customizing-android-popup-spinner-dropdown-list-with-animations-4fef68110c53) which is basically a normal Spinner but you can customize it a bit more than the normal one.

What I wanted to achieve is to create 3 PowerSpinners, one next to each other and on tap, they would display a drop-down menus just like a normal Spinner, but the menu rows would be customized (3 TextViews). In order to customize them, I need an **adapter **and a **listener **for each item that I click in the drop-down menu. I don't know how to crate that custom adapter with listener for PowerSpinner item.

Anyways, here is my model HomeItemsModel.java:

public class HomeItemsModel implements Serializable {

    private String leftSideText;
    private String rightSideText;
    private int typeId;
    public HomeItemsModel(String leftSideText, String rightSideText, int typeId) {
        this.leftSideText = leftSideText;
        this.rightSideText = rightSideText;
        this.typeId = typeId;
    }

    public String getLeftSideText() {
        return leftSideText;
    }

    public String getRightSideText() {
        return rightSideText;
    }

    public int getTypeId() {
        return typeId;
    }
    public void setLeftSideText(String leftSideText) {
        this.leftSideText = leftSideText;
    }
    public void setRightSideText(String rightSideText) {
        this.rightSideText = rightSideText;
    }

    public void setTypeId(int typeId) {
        this.typeId = typeId;
    }
}

And here is my one item of PowerSpinner menu: home_screen_list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="wrap_content"
    android:layout_width="match_parent"
    android:orientation="horizontal">
        <TextView
            android:id="@+id/leftSideTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:fontFamily="sans-serif-condensed"
            android:gravity="center_vertical"
            android:textSize="17sp"
            android:textStyle="bold"
            android:textColor="@android:color/black"/>

        <TextView
            android:id="@+id/rightSideTextView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:fontFamily="sans-serif-condensed"
            android:gravity="center_vertical"
            android:textSize="17sp"
            android:textStyle="bold"
            android:textColor="@android:color/black"/>

         <TextView
            android:id="@+id/typeId"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:fontFamily="sans-serif-condensed"
            android:gravity="center_vertical"
            android:textSize="17sp"
            android:textStyle="bold"
            android:textColor="@android:color/black"/>
</LinearLayout>

And finally here how I organized my 3 PowerSpinners in a fragment: fragment_home_container.xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <com.skydoves.powerspinner.PowerSpinnerView
            android:id="@+id/spinnerViewLocation"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="@color/black"
            android:hint="Location"
            android:background="@drawable/rectangle"
            android:foreground="?attr/selectableItemBackground"
            android:textColorHint="@color/black"
            android:textSize="20sp"
            app:spinner_arrow_gravity="end"
            app:spinner_divider_color="@color/black"
            app:spinner_divider_show="true"
            app:spinner_divider_size="0.5dp"
            app:spinner_popup_animation="dropdown"
            app:spinner_popup_background="@color/light_grey"
            app:spinner_popup_elevation="14dp"/>

        <com.skydoves.powerspinner.PowerSpinnerView
            android:id="@+id/spinnerViewType"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="@color/black"
            android:hint="Type"
            android:background="@drawable/rectangle"
            android:foreground="?attr/selectableItemBackground"
            android:textColorHint="@color/black"
            android:textSize="20sp"
            app:spinner_arrow_gravity="end"
            app:spinner_divider_color="@color/black"
            app:spinner_divider_show="true"
            app:spinner_divider_size="0.5dp"
            app:spinner_popup_animation="dropdown"
            app:spinner_popup_background="@color/light_grey"
            app:spinner_popup_elevation="14dp"/>

        <com.skydoves.powerspinner.PowerSpinnerView
            android:id="@+id/spinnerViewDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="10dp"
            android:textColor="@color/black"
            android:hint="Date"
            android:background="@drawable/rectangle"
            android:foreground="?attr/selectableItemBackground"
            android:textColorHint="@color/black"
            android:textSize="20sp"
            app:spinner_arrow_gravity="end"
            app:spinner_divider_color="@color/black"
            app:spinner_divider_show="true"
            app:spinner_divider_size="0.5dp"
            app:spinner_popup_animation="dropdown"
            app:spinner_popup_background="@color/light_grey"
            app:spinner_popup_elevation="14dp"/>
    </LinearLayout>

Can someone please tell me how to implement HomeItemsAdapter which would take 2 String and 1 integer from the HomeItemsModel and map them to 3 TextViews using Custom Adapter written in Java or Kotlin? (Preferably Java)

There is already a part of implementation of custom adapter with listener in the link I left above at the beginning, but it is not clear enough for me.

Looking forward to your answers.

I tried adding the Adapter myself, but I end up crashing the application. The result I expect would be to successfully import list of Strings into my PowerSpinner using the Custom Adapter and to handle item clicks.

0

There are 0 answers