Search code examples
androidlistviewadapteronitemclicklisteneronitemclick

OnItemClickListener not triggering on custom ListView


I'm having a problem with OnItemClickListener on a ListView, the OnItemClick overridden method is not triggered... Here's my code:

public void popupCatCategories(){
    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);  
    View popupCatCategoriesView = layoutInflater.inflate(R.layout.popup_cats_categories, null);  
    final PopupWindow popupWindow = new PopupWindow(popupCatCategoriesView, LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    Button btnDismiss = (Button) popupCatCategoriesView.findViewById(R.id.dismiss);

    btnDismiss.setOnClickListener(new Button.OnClickListener(){
        public void onClick(View v) {
            // TODO Auto-generated method stub
            popupWindow.dismiss();
        }
    });

    // listview to display image and text
    ListView listCatCategories = (ListView) popupCatCategoriesView.findViewById(R.id.listCatCategories);

    List<HashMap<String, String>> listCategories = new ArrayList<HashMap<String, String>>();
    db.open();
    Cursor catCategories = db.getAllCatCategoryList();
    if (catCategories.moveToFirst())
    {
        do {                
            HashMap<String, String> hm = new HashMap<String, String>();
            hm.put("catCategoryThumbnail", Integer.toString(catCategories.getInt(1)));
            hm.put("catName", catCategories.getString(0));
            listCategories.add(hm);
        } while (catCategories.moveToNext());
    }
    db.close();

    listCatCategories.setAdapter(new ItemListBaseAdapter(getApplicationContext(), listCategories));

    Log.i("got it?", "wait for it...");
    listCatCategories.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View view, int position, long id) {
            Log.i("got it?", "yea, got it!");
            Toast.makeText(getBaseContext(), "got it", Toast.LENGTH_SHORT).show();
        }
    });

    popupWindow.showAsDropDown(btnCats, 50, -330);
}

I'm simply adding some values in an ArrayList of HashMap, each HashMap contains 2 String variables, which are displayed in each row of the ListView. There is only an ImageView and a TextView inside a RelativeLayout for the custom xml layout of the ListView. It displayed fine, except that then I click on a row, nothing happens, it does not trigger the Log or anything. The Overridden method onItemClick is not triggered at all.

I've already tried to setClickable(true) or setItemsCanFocus(false) on the ListView, or setFocusable(false) and setFocusableInTouchMode(false) on the ImageView and TextView (even though its applicable for Checkboxes and Buttons right?).

Here's the custom xml for the ListView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >


    <ImageView
        android:id="@+id/catCategoryThumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingBottom="10dp"
        android:paddingRight="10dp"
        android:paddingTop="10dp" />

    <TextView
        android:id="@+id/txtCatCategoryName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="15dp"
        android:focusable="false"
        android:focusableInTouchMode="false" />

</RelativeLayout>

Anyway, I don't know what to do anymore, can anyone please help me?

If you need anymore information, please let me know. Thanks


Solution

  • Found an alternate solution: https://stackoverflow.com/a/6579192/1759005. It's working just as fine.