how to get view of group in expandablelistview in android?
Asked Answered
W

4

5

im blazilian so, my english is not good.

So.. i need get view of group in expandablelistview for get your object tag throught view.getTag() method.

follow me in this example:

 ExpandableListView

    --> group (i need this view)
    ----> child
    ----> child
    ----> child

    --> group (i need this view)
    ----> child
    ----> child

My code:

    @Override
public boolean onChildClick(final ExpandableListView parent, final View v,
        final int groupPosition, final int childPosition, final long id) {

        /* I NEED GET VIEW OF GROUP FOR GET YOUR TAG*/

        View vParent = parent.getChildAt(groupPosition); // dont work after first group 
        Programa v2 = (Programa) parent.getTag(); // return null

        // v parameter is a child of group

    return true;
}

in my adapter:

    @Override
public View getGroupView(int groupPosition, boolean isExpanded,
        View convertView, ViewGroup parent) {

    TwoLineListItem view = (TwoLineListItem) LayoutInflater.from(contexto)
            .inflate(android.R.layout.simple_expandable_list_item_2,
                    parent, false);

    String programa = map.keySet().toArray(new String[map.keySet().size()])[groupPosition];

    view.getText1().setText(programa);
    view.getText2().setText("PROGRAMA LOCAL");

    view.setTag(programas.get(groupPosition)); // i need get this in child click listener

    return convertView = view;
}

any idea? thanks

Washerwoman answered 1/10, 2013 at 19:21 Comment(3)
Wellcome to SO. Please try to make your question clear so people can understand what are you asking for.Fatling
ok... modification: DONE! :DWasherwoman
+1 for the update. Thanks!Fatling
S
1

If your code is correct, you want to get the group view for the child so that you can call getTag() on it, correct?

If so, why not just skip that step and access the value of the tag, set by programas.get(groupPosition) manually?

You can do this by calling:

programas.get(groupPosition) right on the top of your onChildClick method since you get the group position value there too.

Edit in response to your comment:

The issue here is that you're not going to be able to get the view of the group through the adapter since it might involve recreating the view due to recycling of views in lists. If this method doesn't work, I strongly suggest modifying your code to make it work.

If programas is one of the inputs to your adapter, then call getGroup(groupPosition) on your adapter to access it. Else, make a public method in your adapter class to allow retrieval of that value.

Sanjay answered 1/10, 2013 at 19:45 Comment(2)
two differents classes.. activity (that implements OnChildClickListener) and adapter (contains 'programas' List) =/Washerwoman
Edited my answer with a response to your comment.Sanjay
B
14

To get the group view from an ExpandableListView, you do something as follows:

public View getGroupView(ExpandableListView listView, int groupPosition) {
  long packedPosition = ExpandableListView.getPackedPositionForGroup(groupPosition);
  int flatPosition = listView.getFlatListPosition(packedPosition);
  int first = listView.getFirstVisiblePosition();
  return listView.getChildAt(flatPosition - first);
}
Bouffe answered 6/5, 2015 at 21:28 Comment(3)
Where are you using packedPosition ? It seems to me that it is unused!Variola
@Variola The packedPosition is supposed to be used in getFlatListPosition, instead of the groupPositionPeskoff
1 more clarification, getFirstVisbileView() should be: getFirstVisiblePosition()Daisie
S
1

If your code is correct, you want to get the group view for the child so that you can call getTag() on it, correct?

If so, why not just skip that step and access the value of the tag, set by programas.get(groupPosition) manually?

You can do this by calling:

programas.get(groupPosition) right on the top of your onChildClick method since you get the group position value there too.

Edit in response to your comment:

The issue here is that you're not going to be able to get the view of the group through the adapter since it might involve recreating the view due to recycling of views in lists. If this method doesn't work, I strongly suggest modifying your code to make it work.

If programas is one of the inputs to your adapter, then call getGroup(groupPosition) on your adapter to access it. Else, make a public method in your adapter class to allow retrieval of that value.

Sanjay answered 1/10, 2013 at 19:45 Comment(2)
two differents classes.. activity (that implements OnChildClickListener) and adapter (contains 'programas' List) =/Washerwoman
Edited my answer with a response to your comment.Sanjay
V
1

I tried AedonEtLIRA answer but it only obtained the first item in the list.

My solution was to set a tag in my adapter's getGroupView method.

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = layoutInflater.inflate(R.layout.listitem_group, null);
    }

    //This is your data object that you might be using for storage that can be returned by your getGroup(groupPosition) function
    GroupDataDto groupDataDto = (GroupDataDto) getGroup(groupPosition);
    String name = groupDataDto.getName();
    convertView.setTag(name);
...

Now in your Fragment / Activity, in your onGroupExpand and onGroupCollapse:

    GroupDataDto groupDataDto = (GroupDataDto) listAdapter.getGroup(groupPosition);
    ImageView arrow = (ImageView) getView().findViewWithTag(groupDataDto.getName()).findViewById(R.id.expandable_arrow_down);

Then I can do my animation on the arrow, which is why I wanted to get that group view.

Viper answered 19/1, 2017 at 13:46 Comment(0)
C
-2

Well I used the following code to access a TextView in a group:

@Override
public boolean onChildClick(final ExpandableListView parent, final View v,
    final int groupPosition, final int childPosition, final long id) {

    View vParent = mAdapter.getGroupView(groupPosition, true, null, null); 
    Programa v2 = (Programa) vParent.getTag(); 

return true;

}

I hope it helps

Chowchow answered 31/12, 2014 at 17:14 Comment(7)
This code shouldn't work... First you would be recreating a view EVERY time you click a view. Further, the view would not be attached to the list view in any way, as you just created it. It wouldn't have been added to the list view. Finally, unless you passed a context to the adapter upon creation, you couldn't even create the view; you don't have a source for a context with the parent AND convert view being null.Bouffe
@ AedonEtLIRA In fact it works. I used the same code to access my group's TextView in an expandable ListView. Here's what I mean: TextView dateTV = (TextView) (mCursorAdapter.getGroupView(groupPosition, true, null, null)).findViewById(R.id.date_text_group);. You were in a rush to mark me down. You shouldn't.Chowchow
While this code may compile, and run, I'm certain that the call mAdapter.getGroupView(...) does not return a view that is present in the ExpandableListView unless the adapter is being used in a completely erroneous fashion. Adapters are used to create or update views that are provided as content. That's it. They in themselves do not add a view to a ListView. This code is most certainly not updating a view as one is not passed to the adapter. This leaves the adapter as creating a view. This may work, sure, but its a new view. It's not part of the ListView in any fashion; it was just created.Bouffe
So that said, the result (getting the tag) is should not be working as expected. I'm not trying to be an ass, just simply trying to understand (or make clear) what is going on here. I'm more than willing to remove the vote once the issue is cleared up.Bouffe
@ AedonEtLIRA According to the docs the getGroupView method returns "the View corresponding to the group at the specified position". It's not mandatory to be used to create a group view. Anyway, I am not an Android expert, so there's no point in insisting on it. Do you have any plausible solution to the problem?Chowchow
That is true, but the adapter is meant to provide views to an AdapterListView exclusively. Think of it as a view factory that is designed to only work with a list view. If you are calling adapter.getView(..) manually, you have sidestepped the bounds of the list view/adapter contract and undesired or unexpected results will occur. As for the solution to the original problem, I have posted the proper way of retrieving a group from from an expandable list view.Bouffe
You are ignoring recycling of views by creating views every time as null is passed as convert view parameterDixil

© 2022 - 2024 — McMap. All rights reserved.