title: Android action listener to call a method in another class
body: <p>I have a listview with a button in each row. I have made a custom Adapter class and a ItemModel class to hold the data for each row. Inside the ItemModel class I have defined an ActionListener for the button. How can I call a method in another class from inside my button's action listener?</p>
  
  <p>Right now if i say Classname clsName = new Classname();  and inside the actionlistener do clsName.methodName(variableToPass); &lt;--- this all compiles but crashes when I click the button..Anyone know how to get this to work?</p>
  
  <p>MyListModel Class</p>
  
  <pre><code>public class MyListItemModel{ //that's our book
  private String title; // the book's title
  private String description; //the book's description
  int id; //book owner id
  String key; //book key
  private Context context;
  Shelf shelf = new Shelf();  //shelf class
  
  
  public MyListItemModel(Context c){
      this.context=c;
  
   }
  
  
  public String getTitle() {
      return title;
  }
  public void setTitle(String title) {
      this.title = title;
  }
  public String getDescription() {
      return description;
  }
  public void setDescription(String description) {
      this.description = description;
  }
  
  public String getKey() {
      return key;
  }
  
  public void setKey(String key){
      this.key = key;
  }
  
  
  OnClickListener listener = new OnClickListener(){ // the book's action
      @Override
      public void onClick(View v) {
          //code for the button action
          //THIS DOESN'T WORK PROPERLY AND CRASHES ON CLICK. However if i use a Toast to print the key on each click - it will print the right key to screen.
  
          shelf.downloadBook(new String(key));
  
      }
  };
  int getBookId(){
  
      return title.hashCode();
  }
  }
  </code></pre>
  
  <p>MyListAdapter class - method for getView</p>
  
  <pre><code>public class MyListAdapter extends BaseAdapter {
  
  View renderer;
  List&lt;MyListItemModel&gt; items;
  ArrayList&lt;HashMap&lt;String, String&gt;&gt; mylist;
  private LayoutInflater mInflater;
  private Context context;
  
  
  public MyListAdapter(Context c){
      this.context=c;
      mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  }
  
  ....
  
   @Override
  public View getView(int position, View convertView, ViewGroup parent) {
  
      if(convertView==null){
          //convertView = renderer;
          convertView = mInflater.inflate(R.layout.shelfrow, null);
  
      }
      MyListItemModel item = items.get(position);
      TextView label = (TextView)convertView.findViewById(R.id.item_title);
      label.setText(item.getTitle());
      TextView label2 = (TextView)convertView.findViewById(R.id.item_subtitle);
      label2.setText(item.getDescription());
      Button button = (Button)convertView.findViewById(R.id.btn_download);
      button.setOnClickListener(item.listener);
  
  
      return convertView;
  }
  </code></pre>
  
  <p>My Shelf class has a method called downloadBook(String bookKey) &lt;-- this is what I want to call with each button click and pass this method the appropriate book key. I also have 2 xml files (shelfrow.xml and shelflist.xml). One contains the textfields and button and the other contains the listview.</p>
  
  <p>Some of the code from Shelf.java class</p>
  
  <pre><code>List&lt;MyListItemModel&gt; myListModel = new ArrayList&lt;MyListItemModel&gt;();
  
              try{
  
                  JSONArray entries = json.getJSONArray("entries");
  
                  for(int i=0;i&lt;entries.length();i++){                        
  
                      MyListItemModel item = new MyListItemModel(this);
                      JSONObject e = entries.getJSONObject(i);
                      item.id = i;        //user ID
                      bookKey = (e.getString("key"));
                      item.setTitle(e.getString("title"));
                      item.setDescription(e.getString("description"));
  
                                      myListModel.add(item);  
                          }
  
                      }catch(JSONException e) {
                          Log.e("log_tag", "Error parsing data "+e.toString());
                      }
  
  
                      MyListAdapter adapter = new MyListAdapter(this);
                      adapter.setModel(myListModel);
                      setListAdapter(adapter);
                      lv = getListView();
                      lv.setTextFilterEnabled(true); 
  
  ….
  
   public void downloadBook(String theKey) {
        //take theKey and append it to a url address to d/l
    }
  </code></pre>
  
  <p>Stacktrace from logcat</p>
  
  <pre><code>05-23 02:34:59.439: INFO/wpa_supplicant(14819): Reset vh_switch_counter due to receive LINKSPEED cmd 05-23 02:34:59.439: DEBUG/ConnectivityService(1346): getMobileDataEnabled returning true 05-23 02:36:39.269: DEBUG/StatusBarPolicy(6068): onSignalStrengthsChange
  </code></pre>
  
  <p>also this came up zygoteinit methodandargscaller.run</p>