title: How to implement onClickListener on a custom adapter?
body: <p>I am learning to make a simple time table managing application.
  I have a list of courses displayed. Each item in a list is a textview + a delete button. The onClick Listener in my list item isn't working as expected. When I click on the delete button, it is working fine. However, I want to open up some other activity when user clicks on the textview of the list item.</p>
  
  <p>Code:</p>
  
  <p>ShowAll.java (the main activity in which I am displaying a list of classes)</p>
  
  <pre><code>package com.example.android.mytimetable;
  
  import android.content.Intent;
  import android.os.Bundle;
  import android.support.v7.app.ActionBarActivity;
  import android.util.Log;
  import android.view.Menu;
  import android.view.MenuItem;
  import android.view.View;
  import android.widget.AdapterView;
  import android.widget.ArrayAdapter;
  import android.widget.ListView;
  import android.widget.TextView;
  
  import java.util.ArrayList;
  
  
  public class ShowAll extends ActionBarActivity {
      private ArrayAdapter&lt;String&gt; adapter ;
      ArrayList &lt;ClassDetail&gt; classesDetail ;
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_show_all);
          this.bindAdapter();
  
          ListView listView = (ListView) this.findViewById(R.id.class_list);
          listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int i, long l) {
                  Log.v("Item", "clicked");
                  Intent intent = new Intent(view.getContext(), ShowAllClicked.class);
                  ClassDetail classDetail = classesDetail.get(i);
                  Bundle bundle = new Bundle();
                  bundle.putString("CLASS_NAME", classDetail.class_name);
                  bundle.putString("BUILDING", classDetail.building);
                  bundle.putString("MONDAY_START", classDetail.monday_start);
                  bundle.putString("MONDAY_END", classDetail.monday_end);
                  bundle.putString("TUESDAY_START", classDetail.tuesday_start);
                  bundle.putString("TUESDAY_END", classDetail.tuesday_end);
                  bundle.putString("WEDNESDAY_START", classDetail.wednesday_start);
                  bundle.putString("WEDNESDAY_END", classDetail.wednesday_end);
                  bundle.putString("THURSDAY_START", classDetail.thursday_start);
                  bundle.putString("THURSDAY_END", classDetail.thursday_end);
                  bundle.putString("FRIDAY_START", classDetail.friday_start);
                  bundle.putString("FRIDAY_END", classDetail.friday_end);
                  intent.putExtras(bundle);
                  startActivity(intent);
              }
          });
      }
  
      void bindAdapter() {
          DBHelper db = new DBHelper(this);
          classesDetail = db.getClassesDetail();
          ArrayList &lt;String&gt; classes = new ArrayList&lt;&gt;();
          for(int i = 0 ; i &lt; classesDetail.size() ; i++) {
              Log.v("Adding ", classesDetail.get(i).class_name);
              classes.add(classesDetail.get(i).class_name);
          }
          if(classes.size() == 0)
              ((TextView) this.findViewById(R.id.holiday)).setText(getString(R.string.noClass));
          CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(classes, this);
          ((ListView) this.findViewById(R.id.class_list)).setAdapter(customArrayAdapter);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.menu_show_all, menu);
          return true;
      }
  
      @Override
      public boolean onOptionsItemSelected(MenuItem item) {
          // Handle action bar item clicks here. The action bar will
          // automatically handle clicks on the Home/Up button, so long
          // as you specify a parent activity in AndroidManifest.xml.
  
          return super.onOptionsItemSelected(item);
      }
  }
  </code></pre>
  
  <p>activity_show_all.xml (the xml layout of ShowAll.java)</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
      android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
      android:paddingRight="@dimen/activity_horizontal_margin"
      android:paddingTop="@dimen/activity_vertical_margin"
      android:paddingBottom="@dimen/activity_vertical_margin"
      tools:context="com.example.android.mytimetable.ShowAll"
      android:orientation="vertical"&gt;
  
      &lt;ListView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/class_list"/&gt;
  
      &lt;TextView
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:id="@+id/holiday"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>CustomArrayAdapter.java (The custom array adapter file)</p>
  
  <pre class="lang-java prettyprint-override"><code>package com.example.android.mytimetable;
  
  import android.content.Context;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.widget.BaseAdapter;
  import android.widget.Button;
  import android.widget.ListAdapter;
  import android.widget.TextView;
  
  import java.util.ArrayList;
  
  /**
   * Created by Aman Goel on 02-08-2015.
   */
  public class CustomArrayAdapter extends BaseAdapter implements ListAdapter {
      private ArrayList &lt;String&gt; list = new ArrayList&lt;String&gt;();
      private Context context;
  
      public CustomArrayAdapter(ArrayList &lt;String&gt; list, Context context) {
          this.list = list;
          this.context = context;
      }
  
      @Override
      public int getCount() {
          return list.size();
      }
  
      @Override
      public Object getItem(int pos) {
          return list.get(pos);
      }
  
      @Override
      public long getItemId(int pos) {
          return 0;
      }
  
      @Override
      public View getView(final int position, View convertView, ViewGroup parent) {
          View view = convertView;
          if(view == null) {
              LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
              view = inflater.inflate(R.layout.list_item, null);
          }
  
          ((TextView) view.findViewById(R.id.list_item)).setText(list.get(position));
  
          Button deleteBtn = (Button) view.findViewById(R.id.delete);
  
          deleteBtn.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  DBHelper db = new DBHelper(context);
                  db.deleteClass(list.get(position));
                  list.remove(position);
                  notifyDataSetChanged();
              }
          });
          return view;
      }
  }
  </code></pre>
  
  <p>list_item.xml (The layout of each list view)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_height="wrap_content"
      android:layout_width="match_parent"
      android:orientation="horizontal"&gt;
  
      &lt;TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:minHeight="?android:attr/listPreferredItemHeight"
          android:gravity="center_vertical"
          android:id="@+id/list_item"
          android:focusableInTouchMode="false"
          android:clickable="false"
          android:focusable="false"/&gt;
  
      &lt;Button
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@string/delete"
          android:id="@+id/delete"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>I tried to take help from here: <a href="https://stackoverflow.com/questions/22591793/set-onclicklistener-into-custom-adapter">Set onClickListener into custom adapter</a> and here: <a href="https://stackoverflow.com/questions/10617380/where-should-i-place-the-onclicklistener-on-a-custom-listview">Where should I place the onClickListener on a Custom ListView?</a></p>
  
  <p>However, I am still not able to make the adapter work. Any help would be appreciated</p>