0

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 


            }
        });
9
  • Try v.getTag(); inside of your onClick Commented Aug 2, 2012 at 13:50
  • @SpK I'll have to set a tag in order to use getTag right? Commented Aug 2, 2012 at 13:52
  • Yes. If you set any tags for ImageView in your customAdapter class. Commented Aug 2, 2012 at 13:54
  • I don't know how to set the tag. I mean could you write that line for me which would take the String mrb.workLogID and pass it to delete as a tag? Commented Aug 2, 2012 at 13:56
  • setTag() and getTag() just allows you to set any java object on a View. The tag could be a String object. View.setTag(new String("Hello world")); Commented Aug 2, 2012 at 14:10

2 Answers 2

2

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

    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

Both the assumptions are right. However I was wondering how to get this ID in my activity where I delete the entry from database.
OH I see. I think you need your list view to have a reference back to the activity (then you can have the delete onClick to call the activity's delete method); or you have the onClick listener back in the activity and call v.getTag() (v should be your delete ImageView). (Answer updated)
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.