erdomester
erdomester

Reputation: 11829

Android ExpandableListView group position always 0

I have an ExpandableListView and I want to log the groupposition when clicking on a group. Unfortunately the code below returns always 0, as if I were clicking on the 0th group.

  exList.setOnGroupClickListener(new OnGroupClickListener() {

    @Override
    public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
          groupPosition = ExpandableListView.getPackedPositionGroup(id);

          Log.i("group position", groupPosition + "");
          return false;
    }

  });

I also have a longclicklistener on the groups and childs which works right:

exList.setOnItemLongClickListener(new OnItemLongClickListener() {
      @Override
      public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
          if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
              groupPosition = ExpandableListView.getPackedPositionGroup(id);
...
}

Any ideas?

Upvotes: 6

Views: 5687

Answers (4)

Sigvent
Sigvent

Reputation: 308

Make sure you have this for the layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">


    <ExpandableListView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

You can't have a ExpandableListView into a ScrollView.

Upvotes: 3

navalkishoreb
navalkishoreb

Reputation: 543

your expandable list view might be inside a scroll view. You can't have nested scroll.

Upvotes: 1

AnujAroshA
AnujAroshA

Reputation: 4811

@Override
public Object getGroup(int groupPosition) {
    Log.i(TAG, "* getGroup : groupPosition =" + groupPosition);

    return categories[groupPosition];
}

When you extends BaseExpandableListAdapter , you will get the above override method. The above Log out put clearly displays the clicked group number.

In my code, categories means the data set I am passing to Expandable list as the groups.

You will have another override method call;

@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
...
}

With in that method you can call as follow;

if(getGroup(groupPosition).toString().equals("GroupName")){
    //Do what even you like in for the clicked group
}

This is a working code and hope you can understand it.

Upvotes: 0

Derzu
Derzu

Reputation: 7146

Use the listener OnGroupExpandListener, the param of the method onGroupExpand is the position of the group at the ExpandableListView.

Like that:

listView = (ExpandableListView) findViewById(R.id.expandableListView);
listView.setOnGroupExpandListener(new OnGroupExpandListener() {
    @Override
    public void onGroupExpand(int groupPosition) {
        Log.d(TAG, "pos " + groupPosition);
    }
});

Upvotes: 1

Related Questions