Harsh
Harsh

Reputation: 487

Remove ListView element with a delete button

This question may have been answered on SO several times but it didn't cover what I'm looking for.

I have a listview with a custom adapter. The listview has a textview and a delete ImageView attached to it's row. I want to extract the value of a textview from an item when it's clicked in order to delete that item also from database. I also want to update the list item and I'm using listview.setOnClickListener for that purpose. So I couldn't use the same for delete. I've read about using setTag() and getTag() methods but not sure how to do that exactly. I want to set the textview or rather the string value of the texview as a tag to the delete imageview inside the adapter. Then use getTag() inside the delete.setOnClickListener inside my activity. Could anyone please help me out with this?

Relevant adapter code:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;
    MyStringReaderHolder holder;


    if(row==null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent,false);

        holder= new MyStringReaderHolder();

        holder.workLogID = (TextView)row.findViewById(R.id.worklog_id);
        holder.delete = (ImageView) row.findViewById(R.id.delete_entry);

        row.setTag(holder);
    }
    else
    {
        holder=(MyStringReaderHolder) row.getTag();
    }        

    ViewWorkEntryBean mrb = data.elementAt(position);


    holder.workLogID.setText(mrb.workLogID); 
    // mrb.workLogID contains the desired string which I want to pass to delete as a tag      
    // How do I set the tag?     


    return row;
}

 static class MyStringReaderHolder
 {
String billable;
 TextView workLogID;
 ImageView delete;

 }

And this is onClickListener inside activity:

ImageView deleteButton = (ImageView) findViewById(R.id.delete_entry);

        deleteButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
            String ID = null;
                            // how do I get the value of the tag into the string ID 


            }
        });

Upvotes: 0

Views: 2050

Answers (2)

Rohit
Rohit

Reputation: 1550

Here is what I did.

Created one interface with one method.

public interface ItemRemovedListener
{
public void ItemRemoved(OrderDetails orderDetails);
}

Added a callback of the class implementing the interface when preparing the adapter.

adapter = new CartListItemAdapter(this, R.id.listView_cart, orderDetailsList,this);

Called the listener whenever an item is being removed. (Inside the click event of remove button in adapter class)

  itemRemovedListener.ItemRemoved(orderDetails);

Handled the item remove event in the class which is implementing the my interface.

@Override
public void ItemRemoved(OrderDetails orderDetails) {
    adapter.remove(orderDetails);
    adapter.notifyDataSetChanged();
}

This solved my problem. I want to mention that my click event listener for the remove button is in my adapter class. So I created a call back for item remove event.

Hope this helps.

Upvotes: 0

xbakesx
xbakesx

Reputation: 13490

Alright, I am going to make a few assumptions and you can tell me which ones are wrong:

  • R.id.delete_entry is the delete button in the row layout
  • R.id.worklog_id is the text view in the row layout

If that's true you want your getView code to look something like this:

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent,false);
    }

    final TextView label = (TextView)row.findViewById(R.id.worklog_id);
    final ImageView delete = (ImageView) row.findViewById(R.id.delete_entry);

    ViewWorkEntryBean mrb = data.elementAt(position);

    // set tag here
    delete.setTag(mrb.workLogID);
    label.setText(mrb.workLogID /* or whatever */);

    delete.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // You can now get your tag value here
            String ID = delete.getTag();

        }
    });

    return row;
}

I didn't actually run this code... so hopefully I didn't make too many bone-head mistakes.

EDIT:

You can then have code that looks really similar to where you started:

/** This is in your ListView class */
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View row = convertView;

    if(row == null)
    {
        LayoutInflater inflater = ((Activity)context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent,false);
    }

    final TextView label = (TextView)row.findViewById(R.id.worklog_id);
    final ImageView delete = (ImageView) row.findViewById(R.id.delete_entry);

    ViewWorkEntryBean mrb = data.elementAt(position);

    // set tag here
    delete.setTag(mrb.workLogID);
    label.setText(mrb.workLogID /* or whatever */);

    return row;
}

Then in your activity:

/** This is in your Activity class */
ImageView delete = /* However you were getting the current row's delete button */;
delete.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        // You can now get your tag value here
        String ID = v.getTag();

    }
});

Upvotes: 2

Related Questions