title: onClick listener to a ListView Image - Android
body: <p>I have a <code>ListView</code> with an image over the right hand side. and I wanted to perform a <code>onClick</code> listener event by clicking the image on the <code>ListView</code>. Please see the image for reference.</p>
  
  <p><img src="https://i.stack.imgur.com/j889e.jpg" alt="enter image description here"> </p>
  
  <p>I know basic <code>OnClick</code> listener Implementations, but this seems to be a little tricky to me :P</p>
  
  <p>Forgot to mention, by clicking the actual <code>ListView</code> will shootup a new activity, so I need to maintain both the schemas.</p>
  
  <pre><code> listView.setOnItemClickListener(new OnItemClickListener() {
  
          @Override
          public void onItemClick(AdapterView&lt;?&gt; parent, View view, int position,
                  long id) {
              eventsData.remove(id);
              cursor.requery(); 
          }
  
      });   
  </code></pre>
  
  <p>The code above perform a deletion by clicking on any list element <code>eventsData.remove(id);</code> is a database helper for executing this task. like I said now I need a method to perform this same process juts by clicking the image, not the entire list element, I want the list element to do some other action later on.</p>
  
  <p>I hope now I'm clear a bit.</p>
  
  <hr>
  
  <p><strong>The Solution:</strong></p>
  
  <p><em>If anybody come across the same sort of situation then here is the complete code for the adapter.</em></p>
  
  <pre><code> class CustomAdapter extends ArrayAdapter&lt;String&gt; {
   CustomAdapter() {
    super(Activity.this, R.layout.row, R.id.label, items);
  }
  
  public View getView(final int position, View convertView,
                      ViewGroup parent) {
    View row=super.getView(position, convertView, parent);
    deleteImg=(ImageView)row.findViewById(R.id.icon);
  
    deleteImg.setImageResource(R.drawable.delete);      
    deleteImg.setOnClickListener(new OnClickListener() {
        String s = items[position];
      @Override
      public void onClick(View v) {
          Toast.makeText(context, s, Toast.LENGTH_SHORT).show();          
      }
  });
  
    return(row);
  }
  </code></pre>
  
  <p>}</p>
  
  <p>I know the coding is a bit crappy so bear with me, I just want to show the actual process that's it.</p>
  
  <p>It's working for me :)</p>