Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use item to realize the function of long press deletion in Android

Shulou Source: shulou.com Published: 2022-05-31 23:51:16 09月21日 Update

In this article Xiaobian for you to introduce in detail "how to use item to achieve long press delete function in Android", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "how to use item to achieve long press deletion function in Android" can help you solve your doubts.

It is usually called after writing.

Public void removeItem (int pos) {this.mDatas.remove (pos); notifyItemRemoved (pos);}

But if you write in this way, if you delete it many times, it will be messed up and will not be refreshed.

So write it like this.

Public void removeItem (int pos) {this.mDatas.remove (pos); notifyItemRemoved (pos); if (pos! = mDatas.size ()) {/ / if the last one is removed, ignore notifyItemRangeChanged (pos, mDatas.size ()-pos);}}

PS: let's take a look at android long press to delete the item of listview.

First of all, inherit OnItemLongClickListener.

Public class Set_Music extends ListActivity implements OnItemLongClickListener {

Then set permissions:

GetListView () setOnItemLongClickListener (this)

Rewriting method

Public boolean onItemLongClick (AdapterView arg0, View arg1, int arg2,long arg3) {String str = list.get (arg2) .get ("name")

Delete the long press item

List.remove (arg2)

Update listview dynamically

Adapter.notifyDataSetChanged (): read here, this article "how to use item to achieve long press deletion function in Android" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it before you can understand it. If you want to know more about related articles, you are welcome to follow the industry information channel.

Tags: Functions articles content proper dynamic ideas new knowledge methods more permissions appearance steps knowledge knowledge points articles details industry information information channels follow Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Xiaomi Shulou Information Redmi vpn