title: How to add an onClickListener to different items in a ListView row
body: <p>So I'm making an app where one of the features is you are able to set alerts. Below are 2 example alerts that I used an adapter to put into a ListView. Each item has an ImageView, a TextView, and another ImageView.</p>
  
  <p><a href="https://i.stack.imgur.com/6f1pH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6f1pH.png" alt="enter image description here"></a></p>
  
  <p>My question is how would I turn on a <code>onClickListener</code> for the different images and maybe even the text depending how I do things. </p>
  
  <p>For example, when you click the 'x' it will delete that alert from the list (showing me how to do and letting the adapter know a alert was deleted would be cool) or clicking the alert picture would activate/deactivate the alert (which would switch between the two images seen). </p>
  
  <p>This screen comes to be by a <code>MainActivity</code> inflating an <code>AlertFragment</code>. In the past I've had problems with Fragments implementing inner classes but the way I have it set up now is (I think) with Fragments implementing inner classes. This is what I currently have which I know is wrong:</p>
  
  <pre><code>public class AlertsFragment extends Fragment {
  
      private ListView mListView;
      private List&lt;Alert&gt; alerts = new ArrayList&lt;&gt;();
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
      }
  
      @Nullable
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_alerts, container, false);
  
          mListView = (ListView) rootView.findViewById(R.id.alerts_list_view);
  
          alerts.add(new Alert(1, 5400, true));
          alerts.add(new Alert(2, 120, false));
  
          AlertsAdapter adapter = new AlertsAdapter(getActivity(), R.layout.alert_item_row, alerts);
          mListView.setAdapter(adapter);
  
          getActivity().setTitle(new AlertsItem().getLabel());
  
          mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; parent, View view, final int position, long id) {
                  Log.d("matt", "list_item");
                  ImageView img = (ImageView) getActivity().findViewById(R.id.active_image);
                  img.setOnClickListener(new View.OnClickListener() {
                      public void onClick(View v) {
                          Log.d("matt", "image");
                          alerts.get(position).setActive(!alerts.get(position).isActive());
                      }
                  });
              }
          });
          return rootView;
      }
  }
  </code></pre>
  
  <p>Any help you guys can give would be appreciated, thanks.</p>
  
  <p>--------------------------------------------------------- EDIT ---------------------------------------------------------</p>
  
  <p>As some have requested, here is my ArrayAdapter code:</p>
  
  <pre><code>/**
   * Adapter for adding Alerts to ListView
   */
  public class AlertsAdapter extends ArrayAdapter {
  
      private int resource;
      List&lt;Alert&gt; objects;
  
      public AlertsAdapter(Context context, int resource, List&lt;Alert&gt; objects) {
          super(context, resource, objects);
          this.resource = resource;
          this.objects = objects;
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          View row = convertView;
          AlertHolder holder = null;
  
          if (row == null) {
              LayoutInflater inflater = ((Activity)getContext()).getLayoutInflater();
              row = inflater.inflate(resource, parent, false);
  
              holder = new AlertHolder();
                  holder.imgActive = (ImageView)row.findViewById(R.id.active_image);
              holder.txtTitle = (TextView)row.findViewById(R.id.txt_title);
              holder.txtTime = (TextView)row.findViewById(R.id.txt_time);
  
              row.setTag(holder);
          }
          else {
              holder = (AlertHolder)row.getTag();
          }
  
          Alert alert = (Alert) getItem(position);
          int image;
          if (alert.isActive()) {
              image = R.drawable.ic_alert_active_medium;
          }
          else {
              image = R.drawable.ic_alert_inactive_medium;
          }
  
          holder.imgActive.setImageResource(image);
          holder.txtTitle.setText("Route " + Integer.toString(alert.getRoute()));
  
          int min = alert.getTime() / 60;
          int hr = min / 60;
          min = min % 60;
  
          holder.txtTime.setText(((hr == 0) ? "" : (Integer.toString(hr) + " hr ")) + Integer.toString(min) + " min ");
  
          return row;
      }
  
      private static class AlertHolder {
          ImageView imgActive;
          TextView txtTitle;
          TextView txtTime;
      }
  }
  </code></pre>