How to get onChildClick value from expandablelistview in android?

后端 未结 3 1442
陌清茗
陌清茗 2021-02-06 15:58

I am working on a project with parsing JSON from url to ExpandableListView. Depending on the \"status\" tag value (active or pending) , will be placing records accordingly to to

相关标签:
3条回答
  • 2021-02-06 16:39

    The below isn't perfect but it works. It might be helpful to those who need to access the view without overriding the onChildClick method such as a custom dialog with an ExpandableListView.

        ExpandableListView ellQuickKeys = (ExpandableListView) layout.findViewById(R.id.ellQuickKeys);
    
        SimpleExpandableListAdapter expListAdapter =
                new SimpleExpandableListAdapter(
                                this,
                                createGroupList(),                              // Creating group List.
                                R.layout.group_row,                             // Group item layout XML.                       
                                new String[] { "Group Item" },  // the key of group item.
                                new int[] { R.id.row_name },    // ID of each group item.-Data under the key goes into this TextView.                                   
                                createChildList(),                              // childData describes second-level entries.
                                R.layout.child_row,                             // Layout for sub-level entries(second level).
                                new String[] {"Sub Item"},              // Keys in childData maps to display.
                                new int[] { R.id.lblChildText}             // Data under the keys above go into these TextViews.
                        );
    
        ellQuickKeys.setOnChildClickListener(new OnChildClickListener() {    
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,int childPosition,long id) {
                Log.d("Selected text: " + ((TextView) v.findViewById(R.id.lblChildText)).getText());
    
                return false;
            }
        });
        ellQuickKeys.setAdapter(expListAdapter);
    

    child_row.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    
    <TextView android:id="@+id/lblChildText"
         android:paddingLeft="50dp"
         android:focusable="false"
         android:textSize="18dp"
         android:textStyle="normal"
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"/>
    
    </LinearLayout>
    
    0 讨论(0)
  • 2021-02-06 16:54

    Try This:

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
    
        String title = ((TextView) info.targetView).getText().toString();
    
        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        System.out.println(" ----- " + type);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
            int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
            Toast.makeText(this,title + ": Child " + childPos + " clicked in group "+ groupPos, Toast.LENGTH_SHORT).show();
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
            Toast.makeText(this, title + ": Group " + groupPos + " clicked",Toast.LENGTH_SHORT).show();
            return true;
        }
    
        return false;
    }
    
    0 讨论(0)
  • 2021-02-06 16:55

    You have to use Adapter's getChild(groupPosition, childPosition) method to retrieve the instance of child and the cast it to your Map and get it working.

    @Override
    public boolean onChildClick(ExpandableListView parent, View v,
            int groupPosition, int childPosition, long id) {
    
        Toast.makeText(this, (String)((Map<String, String>)
                    adapter.getChild(groupPosition, childPosition)).get("lalit"),
                                                          Toast.LENGTH_LONG).show();
        return super.onChildClick(parent, v, groupPosition, childPosition, id);
    }
    
    0 讨论(0)
提交回复
热议问题