How to delete item by click in item of Recycleview for a long time (maybe 2 seconds) ? And when click in item for long time it give dialog : "Are you Sure To Delete?".
Asked
Active
Viewed 733 times
0
-
Here is your [answer](https://stackoverflow.com/a/26310638/8956604) – Kasım Özdemir Apr 18 '20 at 12:43
-
How about add your code, so we see where we can help. Instead of pasting for you a code in general. – Hasan Bou Taam Apr 18 '20 at 13:17
-
It looks like you want us to write some code for you. While many users are willing to produce code for a coder in distress, they usually only help when the asker has already tried to solve the problem on their own. A good way to demonstrate this effort is to include the code you've written so far, example input (if any), the expected output/behavior, and the output you actually get ([logcat logs](//developer.android.com/studio/debug/am-logcat), [stack traces](//stackoverflow.com/a/23353174), etc.). The more detail you provide, the more answers you are likely to get. Check the [FAQ] and [ask]. – Ryan M Apr 20 '20 at 02:55
2 Answers
1
Adapter class
OnLongItemClickListener longClickListener;
@Override
public void onBindViewHolder(final DocumentViewHolder viewHolder, int position) {
...
viewHolder.itemView.setLongClickable(true);
...
}
public void setLongClickListener(OnLongItemClickListener longClickListener) {
this.longClickListener = longClickListener;
}
OnLongItemClickListener
interface OnLongItemClickListener{
void longClick(Item item)
}
After in your activity / fragment implements OnLongClickListener after call adapter.setLongClickListener (this) in the method that appears, call alertDialog, when you click the ok button, remove item from the list and call
adapter.notifydatasetchanged()

Destroyer
- 785
- 8
- 20
1
You can add below code in onBindViewHolder method..
holder.itemView.setOnLongClickListener {
val builder = AlertDialog.Builder(this)
builder.setTitle("Delete")
builder.setMessage("Are you Sure To Delete?")
builder.setPositiveButton(android.R.string.yes) { dialog, which ->
Toast.makeText(applicationContext,
android.R.string.yes, Toast.LENGTH_SHORT).show()
}
builder.setNegativeButton(android.R.string.no) { dialog, which ->
Toast.makeText(applicationContext,
android.R.string.no, Toast.LENGTH_SHORT).show()
}
builder.setNeutralButton("Maybe") { dialog, which ->
Toast.makeText(applicationContext,
"Maybe", Toast.LENGTH_SHORT).show()
}
builder.show()
false
}

Jaimil Patel
- 1,301
- 6
- 13