title: Android how to create clickable menu from ListView and SimpleMenuAdapter
body: <p>I have problem about SimpleCursorAdapter and ListView. When I want to create dynamic list menu by query from database. The problem is the listview cannot set onClickListener to do something when user click. These are my code.</p>
  
  <p><strong>In file "menu_header.xml"</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:id="@+id/menu_bar"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:paddingLeft="5dip"
          android:paddingRight="5dip"
          android:background="@drawable/menu_background"&gt;
  
          &lt;Button
                  android:id="@+id/bt_back"
                  android:layout_width="70dip"
                  android:layout_height="40dip"
                  android:layout_centerVertical="true"
                  android:text="@string/menu_back" /&gt;
  
          &lt;ImageButton
                  android:id="@+id/bt_search"
                  android:layout_width="50dip"
                  android:layout_height="50dip"
                  android:background="@null"
                  android:src="@android:drawable/ic_menu_search"
                  android:layout_alignParentRight="true"
                  android:paddingLeft="5dip"
                  android:paddingRight="5dip" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>file "menu_wrapper.xml"</strong></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_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"&gt;
  
      &lt;include layout="@layout/menu_header" /&gt;
  
      &lt;RelativeLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"&gt;
  
              &lt;ListView
                      android:id="@android:id/list"
                      android:layout_below="@id/menu_bar"
                      android:layout_width="fill_parent"
                      android:layout_height="wrap_content" /&gt;
  
      &lt;/RelativeLayout&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p><strong>In file "menu_choice.xml"</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
          xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:background="@drawable/menu_selector"
          android:clickable="true"
          android:padding="10dip"&gt;
  
          &lt;LinearLayout
                  android:id="@+id/thumbnail"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:padding="3dip"
                  android:layout_alignParentLeft="true"
                  android:background="@drawable/image_bg"
                  android:layout_marginRight="10dip"&gt;
  
                  &lt;ImageView
                          android:id="@+id/list_image"
                          android:layout_width="60dip"
                          android:layout_height="60dip"
                          android:src="@drawable/building" /&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;TextView
                  android:id="@+id/eng_name"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:layout_alignTop="@+id/thumbnail"
                  android:layout_toRightOf="@+id/thumbnail"
                  android:text="Building"
                  android:textColor="#040404"
                  android:typeface="sans"
                  android:paddingTop="5dip"
                  android:textSize="20dip"
                  android:textStyle="bold" /&gt;
  
          &lt;TextView
                  android:id="@+id/thai_name"
                  android:layout_width="fill_parent"
                  android:layout_height="wrap_content"
                  android:layout_below="@id/eng_name"
                  android:textColor="#343434"
                  android:textSize="15dip"
                  android:layout_marginTop="3dip"
                  android:layout_toRightOf="@+id/thumbnail"
                  android:text="Test" /&gt;
  
          &lt;ImageView android:layout_width="15dip"
                     android:layout_height="15dip"
                     android:src="@drawable/next_arrow"
                     android:layout_alignParentRight="true"
                     android:layout_centerVertical="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>and the last one is the file that I use to create the menu</strong></p>
  
  <pre><code>public class Menu extends ListActivity {
  
      private ListView listView;
      private ImageButton imageButtonSearch;
      private Button buttonBack;
  
      private Constants constants = Constants.getInstance();
      private Database database = new Database(this);
      private Beans beans = Beans.getInstance();
      private Context context = this;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.menu_wrapper);
  
          buttonBack = (Button)findViewById(R.id.bt_back);
          imageButtonSearch = (ImageButton)findViewById(R.id.bt_search);
  
          buttonBack.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  finish();
              }
          });
  
          imageButtonSearch.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
                  Intent i = new Intent();
                  i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                  startActivityForResult(new Intent(context, Search.class), beans.REQUEST_CODE);
              }
          });
  
          listView = this.getListView();
          listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
              @Override
              public void onItemClick(AdapterView&lt;?&gt; adapterView, View view, int position, long id) {
                  Log.d("Item Click","item click");
              }
          });
  
          database.open();
          Cursor cursorPlaceType = database.getPlaceType();
          startManagingCursor(cursorPlaceType);
  
          if(cursorPlaceType != null){
  
              String[] columns = new String[]{ constants.PLACE_TYPE_IMAGE, constants.PLACE_TYPE_ENAME, constants.PLACE_TYPE_TNAME };
              int[] to = new int[]{ R.id.list_image, R.id.eng_name, R.id.thai_name };
  
              SimpleCursorAdapter menuAdapter = new SimpleCursorAdapter(this, R.layout.menu_choice, cursorPlaceType, columns, to);
              menuAdapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
                  @Override
                  public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
                      if(view.getId() == R.id.list_image){
  
                          try {
  
                              String imageFile = constants.IMAGE_ASSETS + cursor.getString(columnIndex) + ".png";
                              Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open(imageFile));
                              ((ImageView)view).setImageBitmap(bitmap);
  
                          } catch (IOException e) { e.printStackTrace(); }
                          return true;
                      }//end if
  
                      return false;
                  }//end setViewValue
              });
              this.setListAdapter(menuAdapter);
  
          }else{
              new AlertDialog.Builder(this)
                      .setMessage("Try again")
                      .setNeutralButton("OK", new DialogInterface.OnClickListener() {
                          @Override
                          public void onClick(DialogInterface dialogInterface, int i) {
                              finish();
                          }
                      }).show();
          }
  
      }//end onCreate()
  </code></pre>
  
  <p><strong>Thank you indeed for your help.</strong></p>