title: Using a custom ListItemAdapter (with a delete button) on more than one activity
body: <p>I've learned how to create a custom ListItem and add a clickable ImageButton to it, thanks in part to this answer:</p>
  
  <p><a href="https://stackoverflow.com/questions/11591501/how-to-use-onclicklistener-method-of-button-on-a-listview">how to use onClickListener method of Button on a ListView</a></p>
  
  <p>However, I have two separate activities that contain ArrayLists where the user may want to delete data. Is it possible for me to add code to each of these activities that detects when a particular ListItem's "delete" button is pressed, or would I have to define that method in the ListItemAdapter and try to find a way to detect which activity is currently running? (So far, my app doesn't require any special permissions, but I'm certain that will change in later editions.)</p>
  
  <p>For reference, here's the code to my ListItem.java:</p>
  
  <pre><code>package net.player1diary.pocketscribe;
  
  // custom class to be used for an advanced List Item display
  public class ListItem {
  
      private int listIcon; // resource ID of the icon
      private String listText; // the text
      private String listDetail; // details about the list item (optional)
      private boolean deleteEnabled; // whether or not the list item contains a "delete" button
  
      // constructors
      public ListItem() {};
  
      public ListItem(int resource, String t, String d, boolean del) {
          this.listIcon = resource;
          this.listText = t;
          this.listDetail = d;
          this.deleteEnabled = del;
      }
  
      // functions for displaying variables
      public int getListIcon() {
          return this.listIcon;
      }
  
      public String getListText() {
          return this.listText;
      }
  
      public String getListDetail() {
          return this.listDetail;
      }
  
      public boolean getDeleteListItemEnabled() {
          return this.deleteEnabled;
      }
  
      // functions for altering variables
      public void setListIcon(int newIcon) {
          this.listIcon = newIcon;
      }
  
      public void setListText(String newText) {
          this.listText = newText;
      }
  
      public void setListDetail(String newDetail) {
          this.listDetail = newDetail;
      }
  
      public void setDeleteListItemEnabled(boolean newStatus) {
          this.deleteEnabled = newStatus;
      }
  }
  </code></pre>
  
  <p>And the code to ListItemAdapter.java:</p>
  
  <pre><code>package net.player1diary.pocketscribe;
  
  import java.util.List;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.view.ViewGroup;
  import android.widget.ArrayAdapter;
  import android.widget.ImageButton;
  import android.widget.ImageView;
  import android.widget.TextView;
  import android.widget.Toast;
  
  // custom advanced List Adapter
  public class ListItemAdapter extends ArrayAdapter&lt;ListItem&gt; {
  
      private List&lt;ListItem&gt; objects;
  
      public ListItemAdapter(Context context, List&lt;ListItem&gt; objects) {
          super(context, R.layout.list_item, objects);
          this.objects = objects;
      }
  
      // show the customized list items
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View v = convertView;
  
          if (v == null) {
              LayoutInflater vi;
              vi = LayoutInflater.from(getContext());
              v = vi.inflate(R.layout.list_item, null);
          }
  
          ListItem p = objects.get(position);
  
          // display each list item
          if (p != null) {
              ImageView img = (ImageView) v.findViewById(R.id.imgListIcon);
              final TextView text = (TextView) v.findViewById(R.id.txtListText);
              final TextView detail = (TextView) v.findViewById(R.id.txtListDetail);
              ImageButton deleteButton = (ImageButton) v.findViewById(R.id.btnListDeleteItem);
  
              // prepare the delete button, if it exists
              deleteButton.setOnClickListener(new OnClickListener() {
  
                  @Override
                  public void onClick(View v) {
                      // TODO take action, depending on context
                      Toast.makeText(getContext(), "Are you sure you want to delete " + text.getText().toString() + "?", Toast.LENGTH_SHORT).show();
                  }
              });
  
              if (img != null) {
                  img.setImageResource(p.getListIcon());
              }
  
              if (text != null) {
                  text.setText(p.getListText());
              }
  
              if (detail != null) {
                  detail.setText(p.getListDetail());
              }
  
              // hide the detail from view if there is no text there
              if (detail.getText().length() == 0) {
                  detail.setVisibility(View.INVISIBLE);
              }
  
              // hide the delete button from view if it is disabled
              if (!p.getDeleteListItemEnabled()) {
                  deleteButton.setVisibility(View.GONE);
              }
          }
  
          return v;
      }
  }
  </code></pre>
  
  <p>And finally, the layout for list_item.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" &gt;
  
      &lt;ImageView
          android:id="@+id/imgListIcon"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentLeft="true"
          android:layout_alignParentTop="true"
          android:contentDescription="@string/icon"
          android:maxHeight="@dimen/list_icon"
          android:maxWidth="@dimen/list_icon"
          android:minHeight="32dp"
          android:minWidth="32dp"
          android:src="@drawable/ic_book" /&gt;
  
      &lt;TextView
          android:id="@+id/txtListDetail"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignLeft="@+id/txtListText"
          android:layout_below="@+id/txtListText"
          android:hint="@string/list_item_detail"
          android:textColor="@color/list_detail"
          android:textIsSelectable="false" /&gt;
  
      &lt;TextView
          android:id="@+id/txtListText"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentTop="true"
          android:layout_toRightOf="@+id/imgListIcon"
          android:ellipsize="end"
          android:hint="@string/list_item"
          android:textColor="@color/list_text"
          android:textIsSelectable="false"
          android:textSize="@dimen/list_item"
          android:textStyle="bold" /&gt;
  
      &lt;ImageButton
          android:id="@+id/btnListDeleteItem"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentRight="true"
          android:layout_alignParentTop="true"
          android:src="@drawable/ic_delete" android:contentDescription="@string/delete"/&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>