Spinner setOnItemSelectedListener ไม่ทำงานกับอะแดปเตอร์แบบกำหนดเอง

ฉันได้สร้างสปินเนอร์แบบกำหนดเองเพื่อเติมรายการรูปภาพและข้อความที่มาจาก mysql DB เมื่อฉันพยายามเลือกรายการจากสปินเนอร์ มันไม่ทำงาน แม้ว่าฉันจะดีบักและใส่จุดพักภายใน Listener มันก็ไม่ทำงานก็ตาม ไม่เข้าไป อะแดปเตอร์ของฉัน:

 public class SpinnerShopAdapter extends ArrayAdapter<supermarketspinnerGS> {
    private Context mcontext;
    ArrayList<supermarketspinnerGS> SavedListContent;
    public SpinnerShopAdapter(Context context, ArrayList<supermarketspinnerGS> SavedListContent) {
        super(context, 0, SavedListContent);
        this.mcontext = context;
        this.SavedListContent = SavedListContent;
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return initView(position, convertView, parent);
    }

    @Override
    public View getDropDownView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        return initView(position, convertView, parent);
    }

    private View initView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(
                    R.layout.spinner_supermarket_row, parent, false
            );
        }

        ImageView imvSupermaket = convertView.findViewById(R.id.spinner_supermarket_imageView);
        TextView tvSupamrketname = convertView.findViewById(R.id.spinner_supermarket_name);
        TextView tvIdShop = convertView.findViewById(R.id.spinner_supermarket_idshop);

        supermarketspinnerGS currentItem = getItem(position);

        if (currentItem != null) {
            Glide.with(context).load(currentItem.getImage()).into(imvSupermaket);
            tvSupamrketname.setText(currentItem.getName());
            tvIdShop.setText(currentItem.getIdshop());
        }

        return convertView;
    }
}

ในการสร้างโค้ด:

 spinnerStore = (Spinner) findViewById(R.id.saveditemlist_spinner_storeshead);
    adapterHead = new SpinnerShopAdapter(ActivitySavedListItem.this, getListHeadArray);
 spinnerStore.setAdapter(adapterHead);
    spinnerStore.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                    supermarketspinnerGS clickedItem = (supermarketspinnerGS) parent.getItemAtPosition(position);
                    String clickedshopName = clickedItem.getIdshop();
                  selectedItemText=getListHeadArray.get(spinnerStore.getSelectedItemPosition()).getIdshop();
                    Toast.makeText(ActivitySavedListItem.this, clickedshopName + " selected", Toast.LENGTH_SHORT).show();

            }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

ฉันไม่รู้จริงๆว่ามีอะไรผิดปกติและรอคอยความช่วยเหลือจากคุณ หากคุณต้องการรายละเอียดเพิ่มเติม โปรดอย่าลังเลที่จะถามฉัน ฉันยินดีที่จะอัปเดตโพสต์ของฉัน

แก้ไข: XML SPINNER

 <LinearLayout
                android:layout_width="200dp"
                android:layout_height="30dp"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:background="@drawable/spinner_market_shape"
                app:layout_constraintStart_toEndOf="@+id/textView13"
                app:layout_constraintTop_toBottomOf="@+id/textView10">

                <Spinner
                    android:id="@+id/saveditemlist_spinner_storeshead"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingLeft="15dp"
                    android:paddingRight="15dp" />
            </LinearLayout>

spinner_supermarket_row.xml

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

<ImageView
    android:id="@+id/spinner_supermarket_imageView"
    android:layout_width="75dp"
    android:layout_height="25dp"
    android:layout_marginStart="4dp"
    android:layout_marginTop="2dp"
    android:layout_marginBottom="2dp"
    android:clickable="false"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    android:id="@+id/spinner_supermarket_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:visibility="invisible"
     />

<TextView
    android:id="@+id/spinner_supermarket_idshop"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:visibility="invisible"
    />
</RelativeLayout >

person Hass    schedule 25.08.2019    source แหล่งที่มา
comment
วาง xml ของคุณด้วย   -  person Paresh Mayani    schedule 25.08.2019


คำตอบ (2)


ฉันไม่แน่ใจแต่

ฉันคิดว่าคุณต้องตั้งค่ารายการสปินเนอร์คลิก Listener ในอะแดปเตอร์แบบกำหนดเองของสปินเนอร์ในฟังก์ชัน initView

แบบนี้:

 private View initView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(
                R.layout.spinner_supermarket_row, parent, false
        );
    }

    ImageView imvSupermaket = convertView.findViewById(R.id.spinner_supermarket_imageView);
    TextView tvSupamrketname = convertView.findViewById(R.id.spinner_supermarket_name);
    TextView tvIdShop = convertView.findViewById(R.id.spinner_supermarket_idshop);

    supermarketspinnerGS currentItem = getItem(position);

    if (currentItem != null) {
        Glide.with(context).load(currentItem.getImage()).into(imvSupermaket);
        tvSupamrketname.setText(currentItem.getName());
        tvIdShop.setText(currentItem.getIdshop());
    }

    parent.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {

        if(position==CUSTOM_POSITION){ /*do something*/}                

        }
    });

    return convertView;
}

ฉันหวังว่าจะทำงานให้คุณ

person A.Hosein    schedule 25.08.2019
comment
น่าเสียดายที่มันไม่ได้ - person Hass; 25.08.2019

พยายามเพิ่ม clickable=false ให้กับองค์ประกอบทั้งหมดของคุณในแถวสปินเนอร์ xml ของคุณ

person Achref ArShavin    schedule 25.08.2019
comment
คุณได้เพิ่ม clickable=false ให้กับองค์ประกอบทั้งหมดของคุณคาดหวัง RelativeLayout ของคุณหรือไม่ - person Achref ArShavin; 27.08.2019
comment
ใช่ครับ ผมทำแบบนั้นแล้วไม่สามารถตามทันเหตุการณ์ได้ - person Hass; 27.08.2019