Why are clicks in my ExpandableListView ignored?
Asked Answered
I

3

11

I have an AlertDialog populated by an ExpandableListView. The list itself works perfectly, but for some reason clicks are ignored. Apparently my click handler is never called.

This is the code:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select something");
ExpandableListView myList = new ExpandableListView(this);
MyExpandableListAdapter myAdapter = new MyExpandableListAdapter();
myList.setAdapter(myAdapter);
myList.setOnItemClickListener(new ExpandableListView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> a, View v, int i, long l) {
        try {
            Toast.makeText(ExpandableList1.this, "You clicked me", Toast.LENGTH_LONG).show();
        }
        catch(Exception e) {
            System.out.println("something wrong here    ");
        }
    }
});
builder.setView(myList);
dialog = builder.create();
dialog.show();

If I instead try to populate the AlertDialog with a plain listview, click events are generated without problems:

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Select Color Mode");

ListView modeList = new ListView(this);
String[] stringArray = new String[] { "Bright Mode", "Normal Mode" };
ArrayAdapter<String> modeAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, android.R.id.text1, stringArray);
modeList.setAdapter(modeAdapter);
modeList.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
          // When clicked, show a toast with the TextView text
          Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
              Toast.LENGTH_SHORT).show();
        }
      });


builder.setView(modeList);
AlertDialog dialog1 = builder.create();
dialog1.show();

What could be the reason why click event fails in my ExpandableListView but not in a normal ListView? I am probably missing something, but I have no idea what that could be.

Inoperative answered 14/7, 2011 at 13:14 Comment(0)
I
20

Ok, the solution was quite simple. Since it is an expandable list, item clicks are captured by the list itself to open the child elements. Thus, the event handler is never called.

Instead you have to implement OnChildClickListener() that - as the name suggests - listens to child clicks!

Inoperative answered 15/7, 2011 at 22:46 Comment(0)
C
2

Thx, I was also wondering about it. But still interesting is info in documentation:

public void setOnItemClickListener (AdapterView.OnItemClickListener l) "Register a callback to be invoked when an item has been clicked and the caller prefers to receive a ListView-style position instead of a group and/or child position.".

In docs there is clearly mentioned that you should be able to set and handle this listener if you want...

Cleave answered 27/3, 2013 at 10:25 Comment(0)
H
1

You can actually use

  1. expandableListView.setOnGroupExpandListener to catch the event when expanding
  2. expandableListView.setOnGroupCollapseListener to catch the event when collapsing.
  3. expandableListView.setOnGroupClickListener to catch the event both when expanding or collapsing

link to developer site:

https://developer.android.com/reference/android/widget/ExpandableListView.html

Hammett answered 18/6, 2016 at 22:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.