Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get view of group in expandablelistview in android?

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

like image 901
user155542 Avatar asked Oct 01 '13 19:10

user155542


2 Answers

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);
}
like image 60
ahodder Avatar answered Sep 28 '22 17:09

ahodder


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.

like image 44
Kasra Rahjerdi Avatar answered Sep 28 '22 16:09

Kasra Rahjerdi