Is there a way to detect LongClick with Spinner on Android?

Asked 2 years ago, Updated 2 years ago, 45 views

It has been confirmed that onItemLongClick cannot be used with spinner.
Like this code, I would like to use Handler to write onTouch(), and if the button is pressed after 1000 msec, press and accept.
 Also, I would like to retrieve the data of the selected item in onItemSelected() and delete it from the database.
Thank you for your advice.

final Handler actionHandler=newHandler();
    final Runnable = new Runnable() {
        @ Override
        public void run() {
            Toast toast = Toast.makeText (LocationActivity.this, "Long click", Toast.LENGTH_SHORT);
            toast.show();
        }
    };
    Spinner spinner=(Spinner) findViewById (R.id.spinner);
    spinner.setOnTouchListener(newView.OnTouchListener(){
        @ Override
        public boolean onTouch (View v, MotionEvent event) {

            // Spinner spinner=(Spinner) parent;
            //  deleteitem=(String) spinner.getSelectedItem();

            if(event.getAction() == MotionEvent.ACTION_DOWN) {
                actionHandler.postDelayed(runnable, 1000);

                // If it's else if 1 second after pressing it, will you decide it's a long click and take action?

            } else if(event.getAction() == MotionEvent.ACTION_UP) {
                actionHandler.removeCallbacks(runnable);


            }
            return false;
        }
    });
    // -------------------------------------------------------------------------


    spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener(){
        public void onItemSelected(AdapterView<?>parent, View view, intpos, longid) {

            // These two lines are in the correct place
            Spinner spinner=(Spinner) parent;
            deleteitem=(String) spinner.getSelectedItem();
            // -------------------------------------------------------------------------------------------------------
            // -------------------------------------------------------------------------------------------
            LayoutInflater inflater=(LayoutInflater) LocationActivity.this.getSystemService(LAYOUT_INFLATER_SERVICE);
            finalView layout=inflater.inflate(R.layout.dialog_deleteplace_us, (ViewGroup) findViewById(R.id.layout_deleteplace));
            // Generate alarms and dialogs
            AlertDialog.Builder builder = new AlertDialog.Builder (LocationActivity.this);
            builder.setView (layout);
            //------------------------------------------------------------------------------------------------
            builder.setPositiveButton("Delete", newDialogInterface.OnClickListener(){
                public void onClick (DialogInterface dialog, int which) {
                    if(deleteitem.equals("GPS current location")}
                    } else if(deleteitem.equals("search by googlemap")}
                    } else{
                        String sql="delete from favorite where placename='"+deleteitem+""+
                                " and username = ' " + username + " '; "; ";
                        System.out.println(sql+"good,good,good,good");
                        System.out.println(deleteitem);
                        MyOpenHelper helper=new MyOpenHelper (LocationActivity.this);
                        SQLiteDatabase db=helper.getWritableDatabase();
                        Cursorc=db.rawQuery(sql, null);
                        c.moveToFirst();
                        onStart();
                    }
                }
            });
        }

        @ Override
        public void on NothingSelected (AdapterView <?>adapterView) {
        }
    });

java android

2022-09-30 16:41

1 Answers

Spinner has found that LongCLick is not possible.

The result was simple, but the purpose of the question has been satisfied, so we will resolve it


2022-09-30 16:41

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.