16

I have a collection of items in an ArrayList. I add them to a customer adapter as follows:

this.m_adapter = new MyAdapter(this, R.layout.myitem,
    itemCart.m_items);

I have a delete button for each of these items in my list, but I am not sure how to connect the delete button's onClick() with the original item in the ArrayList. Can someone please explain how to do this or point me to a tutorial where I can read up on this? Non-sarcastic/non-condescending responses are greatly appreciated.

Musa
  • 96,336
  • 17
  • 118
  • 137
gonzobrains
  • 7,856
  • 14
  • 81
  • 132

9 Answers9

30

You can call the remove() method on your ArrayList

itemCart.m_items.remove(<index of element to remove>);
this.m_adapter.notifyDataSetChanged();

And then you need to call notifyDataSetChanged(); on your adapter to update the ListView

jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • Yes, but my problem is figuring out how to specify the index for that item? I think I need to use setTag() originally then getTag() before removal but I'd like to see some sample code if possible. Thanks again. – gonzobrains Jan 15 '11 at 16:49
  • I have a context menu on a ListView with a delete button that I get the element from using what's essentially `item.getMenuInfo().id` (it's not that simple). Surely your buttons have indexes though. – jcuenod Jan 15 '11 at 18:54
7

You can get the index of the element by simply noticed that a list view is a collection of child views (the rows of the list).

You can do something like this in your code:

(inside the getView() method, for example)

row.setOnLongClickListener(new OnLongClickListener() 
{
    @Override
    public boolean onLongClick(View view) {
        remove(listView.indexOfChild(view));
        return true;
    }
}

That is, the solution is simply use indexOfChild(View) method to get index of child view that user (long) pressed.

7

Here's my solution so far:

In the getView() method I do something like this:

deleteButton.setTag(position);

It looks like getTag() returns an Object. So I converted the position int into an Integer object first. It appears to be working.

In the OnClickListener() I do the following:

items.remove(index.intValue());

So far, so good.

Bart
  • 19,692
  • 7
  • 68
  • 77
gonzobrains
  • 7,856
  • 14
  • 81
  • 132
  • So without the tags - you have no idea which button is clicked? It seems to me that you've overlooked some mechanism for figuring that out. Think about using the button's parent. – jcuenod Jan 17 '11 at 08:51
4

Following works for me:

/* Read values from resource into an array */
String[] strColorValues =  getResources().getStringArray(R.array.colors);

ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < strColorValues.length; i++) {
    list.add(strColorValues[i]);
}

ArrayAdapter adapterColors = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);

adapterColors.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinnerColors.setAdapter(adapterPermissionLevels);
spinnerColors.setOnItemSelectedListener(this);

/* Remove first element from the adapter and notify dataset changed. */
String item = spinnerColors.getItemAtPosition(0).toString();
adapterColors.remove(item);
adapterColors.notifyDataSetChanged();
Amit Garg
  • 541
  • 5
  • 12
1

Here's my Code.

  transfer.setItemPosition(position, items.get(position).getAddMode());

the transfer here is the instance of the main class. everytime i click the deletebutton, it then pass the position of the that item on the list in this line.

  public View getView(final int position, View convertView, ViewGroup parent) {
            View v = convertView;
          if (v == null) {
              final Context context = getContext();
                    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.listviewitem_layout, null);
          }

          ItemEntry item = items.get(position);
          if (item != null) {
                    TextView textViewName = (TextView) v.findViewById(R.id.textViewItemName);
                    ImageView imageViewDelete = (ImageView) v.findViewById(R.id.imageViewDeleteIcon);

                    imageViewDelete.setOnClickListener(new View.OnClickListener() {
                        public void onClick(View v) {                   
                                transfer.showDialog(4);                             
                                transfer.setItemPosition(position, items.get(position).getAddMode());
                        }
                  });

          if (textViewName != null) {
                    textViewName.setText(item.getItemName());
          }

          if(imageViewDelete != null) {
              imageViewDelete.setImageResource(R.drawable.delete);
          }
      }
      return v;
        }
}
ロン 産
  • 51
  • 7
1

Remove by position:

mainAdapter.remove(mainAdapter.getItem(position));

Such as the last one:

mainAdapter.remove(mainAdapter.getItem(mainAdapter.getCount() - 1));
BaiJiFeiLong
  • 3,716
  • 1
  • 30
  • 28
1

Try these codes of lines it was very helpful for me

holder.image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            list.remove(position);
            notifyItemRemoved(position);
            notifyItemRangeChanged(position, list.size());
        }
    });
Mujahid Khan
  • 1,712
  • 1
  • 18
  • 24
0

It seems that you can get the index (or position) of a clicked item in the ListView as follows:

listview.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        listview.remove(listview.getItem(position).toString());
    }
}

So you need to listen for clicks on Views and then take the index from that.

Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
0

If you use context menu, then you can get AdapterContextMenuInfo and this structure gives index and id of clicked element.

Dmitriy
  • 21
  • 2