gonzobrains
gonzobrains

Reputation: 8036

Android: how to remove an item from a listView and arrayAdapter

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.

Upvotes: 16

Views: 86289

Answers (9)

Mujahid Khan
Mujahid Khan

Reputation: 1834

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

Upvotes: 1

BaiJiFeiLong
BaiJiFeiLong

Reputation: 4625

Remove by position:

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

Such as the last one:

mainAdapter.remove(mainAdapter.getItem(mainAdapter.getCount() - 1));

Upvotes: 1

Zelphir Kaltstahl
Zelphir Kaltstahl

Reputation: 6189

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.

Upvotes: 0

Amit Garg
Amit Garg

Reputation: 561

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

Upvotes: 4

ロン 産
ロン 産

Reputation: 51

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;
        }
}

Upvotes: 1

gonzobrains
gonzobrains

Reputation: 8036

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.

Upvotes: 7

Victor de Francisco
Victor de Francisco

Reputation: 71

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.

Upvotes: 7

Dmitriy
Dmitriy

Reputation: 21

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

Upvotes: 0

jcuenod
jcuenod

Reputation: 58405

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

Upvotes: 30

Related Questions