Android ExpandableListView group position always 0
Asked Answered
T

4

6

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?

Triclinium answered 9/4, 2012 at 6:47 Comment(6)
Do you use a custom Adapter for the ExpandableListView?Latoyia
yes. My other listeners works rightTriclinium
ok, make sure your getItem and getItemId methods return valid values and not 0 .. (in your custom Adapter) you could also take a look at this adapter developer.android.com/resources/samples/ApiDemos/src/com/…Latoyia
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) did return group position.Powwow
I have no idea, but if I dont define the groupPosition, it is working fine...Triclinium
Check this: #16740190Mondrian
S
3

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.

Susurrous answered 1/4, 2016 at 13:30 Comment(0)
D
1

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);
    }
});
Distant answered 24/5, 2012 at 2:17 Comment(0)
M
1

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

Muffle answered 12/3, 2015 at 5:41 Comment(2)
Thanks! I needed this. I just couldn't understand what I am doing wrong. I was in this case.Heritor
glad to help...!! :)Muffle
A
0
@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.

Alfy answered 24/5, 2012 at 3:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.