如何的Andr​​oid从与longclicklistener列表中删除项目 [英] Android how to delete items from list with longclicklistener

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

问题描述

我刚开始处理列表,所以这可能是一个愚蠢的问题,但我不知道该怎么做,我也查了很多类似的问题,但他们的信息是复杂的。我的名单中,我只有数将是只有1和10个项目的长,从的EditText框中输入用户,那么很简单,在显示我想要使用户能够通过长来删除此列表中的项目pressing商品行,我怎么做到这一点?

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?

这是我的code:

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("");
        }
    });

这是我声明我的列表,并添加项目:项目1是的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?

在此先感谢感谢帮助。

推荐答案

如果您想要的项目从列表中删除时,列表中的该项目的用户点击长,叫<一个href=\"http://developer.android.com/reference/android/widget/AdapterView.html#setOnItemLongClickListener%28android.widget.AdapterView.OnItemLongClickListener%29\"相对=nofollow> 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();
  }
)); 

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

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