Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report