Home > Blockchain >  Long Press doesn't work with OnLongClickListener
Long Press doesn't work with OnLongClickListener

Time:09-27

I want to make a long press on an item in the recyclerview and have the two options "Edit Data" and "Delete data" appear. I have the code below already written but I don't understand how it works.

The error appears in View.OnLongClickListener()

Error: Class 'Anonymous class derived from OnLongClickListener' must either be declared abstract or implement abstract method 'onLongClick(View)' in 'OnLongClickListener

recyclerView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                AlertDialog.Builder builder = new AlertDialog.Builder(view.getContext());
                ProgressDialog progressDialog = new ProgressDialog(view.getContext());

                CharSequence[] dialogItem = {"Edit Data","Delete Data"};
                builder.setTitle(sensorModels.get(position).getNome());
                builder.setItems(dialogItem, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int i) {

                        switch (i){
                            
                            case 0:
                                startActivity(new Intent(getApplicationContext(),CreateSensorActivity.class)
                                        .putExtra("position",position));

                                break;

                            case 1:

                                deleteData(sensorModels.get(position).getId());

                                break;

                        }

                    }
                });
            }
        });

CodePudding user response:

Class 'Anonymous class derived from OnLongClickListener' must either be declared abstract or implement abstract method 'onLongClick(View)' in 'OnLongClickListener

View.OnLongClickListener must need to override onLongClick for a long click to work. You are overriding onItemClick which is not an abstract method of OnLongClickListener. You have to override onLongClick instead of onItemClick.

I want to make a long press on an item in the recycler view

In your code it looks like you are setting a long click on the whole recycler view recyclerView.setOnLongClickListener, not on the single item.

For setting long click on each item of the recyclerview, you have to set onLongClickListener inside onBindViewHolder.

@Override
public void onBindViewHolder(@NonNull TaskViewHolder holder, int position) {
    View view = holder.findViewById(R.id.your_view);
    view.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        boolean onLongClick(v: View) {
        //do work
        return true
        }
}

CodePudding user response:

Please don't use onLongClickListener directly on recycler view. Use callback from the adapter and implement it in activity. I think this will definitely work.

Put this code in adapter and use it while item long press :

 interface OnItemLongClickListener {
        fun onItemLongClick(position: Int)
    }

implement OnItemLongClickListener in the activity by reference to the adapter and override onItemLongClick method in the activity.

  • Related