title: Listview custom adapter notify parent activity
body: <p>I have created a listview that sits inside a larger activity (the list view fills half the screen, with inputs/buttons above it). This listview uses a custom adapter to draw the row views.</p>
  
  <p>Inside the row view is a button, which when clicked/tapped i want the activity to handle, and not the adapter. However i don't know how inside the adapter class, i can tell it to use the activity. Since it's OOP, im assuming i have to pass some sort of reference to the activity when setting up the adapter (rather than hardcoding the parent activity into the adapter).</p>
  
  <p>I had a simlier issue with sharing a dataset between the activity and adapter (i wanted the adapter to use an arraylist from the activity), however i couldn't solve that one either so i ended up passing the arraylist through as a duplicate into the adapter. So i would hope working out the click listener, will also mean i can get rid of that duplicate data having to be created?</p>
  
  <p>All the code is pretty simple, but here's a rough outline:</p>
  
  <p><strong>Setting the list adapter &amp; declaring dataset (this is the activity dataset, not the adapter)</strong></p>
  
  <pre><code>numbersListAdapter = new NumberListAdapter(this);
  numbersList.setAdapter(numbersListAdapter);
  this.selectedContacts = new HashMap&lt;Long, HashMap&lt;String, String&gt;&gt;();
  </code></pre>
  
  <p><strong>Adding entry into activity data set and adding to the adapter dataset</strong></p>
  
  <pre><code>HashMap&lt;String, String&gt; tempa = new HashMap&lt;String,String&gt;();
  tempa.put("name", name);
  tempa.put("number", number);
  this.selectedContacts.put(contactID, tempa);
  
  this.numbersListAdapter.addEntry(contactID, tempa);
  </code></pre>
  
  <p><strong>The adapters add entry</strong></p>
  
  <pre><code>public void addEntry(Long id, HashMap&lt;String, String&gt; entry) {  
  
          entry.put("contactID", id.toString());      
  
          this.selectedNumbers.add(entry);
  
          this.notifyDataSetChanged();
  
      }
  </code></pre>
  
  <p><strong>The adapters constructor</strong></p>
  
  <pre><code>public NumberListAdapter(Context context) {
  
          selectedNumbers = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
          mInflater = LayoutInflater.from(context);
  
      }
  </code></pre>
  
  <p>Please note: this is my first attempt at this stuff. I've never done android, java and very, very little OO programming. So i already know the code is most likely inefficient and quite terrible. But i've got to learn somehow :)</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Ok so i realised ive been a little silly and i just needed to use the context passed into the adapter to reference the parent activity. However i still ain't getting it. Heres the code:</p>
  
  <p><strong>The constructor for the adapter</strong></p>
  
  <pre><code>numbersListAdapter = new NumberListAdapter(this);
  </code></pre>
  
  <p><strong>The variable declaration and constructor method</strong></p>
  
  <pre><code>public class NumberListAdapter extends BaseAdapter  {
  
      private LayoutInflater mInflater;
  
      private ArrayList&lt;HashMap&lt;String, String&gt;&gt; selectedNumbers;
  
      private Context parentActivity;
  
  
  
      public NumberListAdapter(Context context) {
  
          parentActivity = (Context) context; 
  
          selectedNumbers = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
  
          mInflater = LayoutInflater.from(context);
  
      }
  </code></pre>
  
  <p><strong>The listener</strong></p>
  
  <pre><code>Button theBtn = (Button)rowView.findViewById(R.id.actionRowBtn);
              theBtn.setOnClickListener(this.parentActivity);
  </code></pre>
  
  <p>I get two messages from eclipse, the first occurs when i comment out the listener and i get
  <code>The value of the field NumberListAdapter.parentActivity is not used</code></p>
  
  <p>Once i add the listener in i get</p>
  
  <p><code>The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (Context)</code></p>
  
  <p>Obviously im doing something wrong.  Probably quite a silly mistake again too</p>