Android 如何使用 longclicklistener 从列表中删除项目 [英] Android how to delete items from list with longclicklistener

查看:51
本文介绍了Android 如何使用 longclicklistener 从列表中删除项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始使用列表,所以这可能是一个愚蠢的问题,但我不知道该怎么做,我检查了很多类似的问题,但它们的信息很复杂.我只有数字的列表将只有 1 到 10 个项目由用户从 EditText 框输入,所以很简单,在显示时我想让用户能够从这个列表中删除项目很长按项目行,我该如何实现?

I just started working with lists, so this might be a stupid question but I don't know how to do it, I have checked a lot of similar questions but their information is complex. My list in which I have only numbers is going to be only between 1 and 10 items long enter by the user from a EditText box, so is simple, at display I want to make the user be able to delete items from this list by long pressing the item row, how do I accomplish this?

这是我的代码:

final ArrayList<Double> individuallist = new ArrayList<Double>();
final ArrayAdapter<Double> bb;
bb = new ArrayAdapter<Double>(this,android.R.layout.simple_list_item_1 ,individuallist);
listView.setAdapter(bb);

   // add button
    
    Button addbutton = (Button) findViewById(R.id.btnnext);
    addbutton.setOnClickListener(new View.OnClickListener() {
        
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
                           iamount = amountText.getText().toString();
                       it = Double.valueOf(iamount);
            individuallist.add(0, it);
            bb.notifyDataSetChanged();
            item1.setText("");
        }
    });

这就是我声明我的列表并向其中添加项目的方式: item1 是一个 EditText 框,我可以在其中获取用户数量.如何删除列表中的单个项目?

This is how I declare my list and add items to it: item1 is a EditText box where I get the user amount. How can I delete individual items in my list?

推荐答案

如果您希望在用户长按列表中的项目时从列表中删除该项目,请调用 setOnItemLongClickListener() 在您的列表视图中.

If you want the item to be deleted from the list when the user long clicks on the item in the list, call setOnItemLongClickListener() on your listview.

从您的 OnItemLongClickListener 实现中,您将获得位置.然后您可以更新您的 ListAdapter.它可能看起来像,

From your implementation of OnItemLongClickListener, you will get the position. You can then update your ListAdapter. It might looking something like,

listView.setOnItemLongClickListener(new OnItemLongClickListener() {
  @Override
  public void onItemLongClick((AdapterView<?> parent, View view, int position, long id) {
    MyAdapter adapter = (MyAdapter)listView.getAdapter();
    myAdapter.removeItemAt(pos); // you need to implement this method
    myAdapter.notifyDataSetChanged();
  }
)); 

这篇关于Android 如何使用 longclicklistener 从列表中删除项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆