2013-04-17 62 views
2

對於Expandable listview。我寫了下面的代碼:如何在Expandable listview中獲取child和parent的id?

public class MainActivity extends Activity { 
    ExpandableListView explvlist; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    explvlist = (ExpandableListView) findViewById(R.id.expandableListView1); 
    explvlist.setAdapter(new ParentLevel()); 
    } 

    public class ParentLevel extends BaseExpandableListAdapter { 
    @Override 
    public Object getChild(int arg0, int arg1) { 
     return arg1; 
    } 
    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 
    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     CustExpListview SecondLevelexplv = new CustExpListview(MainActivity.this); 
     SecondLevelexplv.setAdapter(new SecondLevelAdapter()); 
     SecondLevelexplv.setGroupIndicator(null); 
     return SecondLevelexplv; 
    } 
    @Override 
    public int getChildrenCount(int groupPosition) { 
     return 3; 
    } 
    @Override 
    public Object getGroup(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public int getGroupCount() { 
     return 8; 
    } 
    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     TextView tv = new TextView(MainActivity.this); 
     tv.setText("->FirstLevel"); 
     tv.setTextColor(Color.BLACK); 
     tv.setTextSize(20); 
     tv.setBackgroundColor(Color.BLUE); 
     tv.setPadding(10, 7, 7, 7); 
     return tv; 
    } 
    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
    } 

    public class CustExpListview extends ExpandableListView { 
    int intGroupPosition, intChildPosition, intGroupid; 
    public CustExpListview(Context context) { 
     super(context); 
    } 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(960, MeasureSpec.AT_MOST); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(600, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
    } 
    public class CustExpListview2 extends ExpandableListView { 
    int intGroupPosition2, intChildPosition2, intGroupid2; 
    public CustExpListview2(Context context) { 
     super(context); 
    } 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
    } 
    public class CustExpListview3 extends ExpandableListView { 
    int intGroupPosition2, intChildPosition2, intGroupid2; 
    public CustExpListview3(Context context) { 
     super(context); 
    } 
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 
     widthMeasureSpec = MeasureSpec.makeMeasureSpec(2000, MeasureSpec.AT_MOST); 
     heightMeasureSpec = MeasureSpec.makeMeasureSpec(1000, MeasureSpec.AT_MOST); 
     super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
    } 
    } 

    public class SecondLevelAdapter extends BaseExpandableListAdapter { 
    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return childPosition; 
    } 
    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 
    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     CustExpListview2 SecondLevelexplv2 = new CustExpListview2(MainActivity.this); 
     SecondLevelexplv2.setAdapter(new ThreeLevelAdapter()); 
     SecondLevelexplv2.setGroupIndicator(null); 
     return SecondLevelexplv2; 
    } 
    @Override 
    public int getChildrenCount(int groupPosition) { 
     return 5; 
    } 
    @Override 
    public Object getGroup(int groupPosition) { 
     return groupPosition; 
    } 
    Override 
    public int getGroupCount() { 
     return 1; 
    } 
    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     TextView tv = new TextView(MainActivity.this); 
     tv.setText("-->Second Level"); 
     tv.setTextColor(Color.BLACK); 
     tv.setTextSize(20); 
     tv.setPadding(12, 7, 7, 7); 
     tv.setBackgroundColor(Color.RED); 
     return tv; 
    } 
    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
    } 

    public class ThreeLevelAdapter extends BaseExpandableListAdapter { 
    @Override 
    public Object getChild(int arg0, int arg1) { 
     return arg1; 
    } 
    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 
    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     CustExpListview3 SecondLevelexplv3 = new CustExpListview3(MainActivity.this); 
     SecondLevelexplv3.setAdapter(new FourLevelAdapter()); 
     SecondLevelexplv3.setGroupIndicator(null); 
     return SecondLevelexplv3; 
    } 
    @Override 
    public int getChildrenCount(int groupPosition) { 
     return 3; 
    } 
    @Override 
    public Object getGroup(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public int getGroupCount() { 
     return 1; 
    } 
    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     TextView tv = new TextView(MainActivity.this); 
     tv.setText("Level --Three"); 
     tv.setTextColor(Color.BLACK); 
     tv.setTextSize(20); 
     tv.setPadding(15, 5, 5, 5); 
     tv.setBackgroundColor(Color.YELLOW); 
     return tv; 
    } 
    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
    } 

    public class FourLevelAdapter extends BaseExpandableListAdapter { 
    @Override 
    public Object getChild(int arg0, int arg1) { 
     return arg1; 
    } 
    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return childPosition; 
    } 
    @Override 
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
     TextView tv = new TextView(MainActivity.this); 
     tv.setText("->five level"); 
     tv.setTextColor(Color.BLACK); 
     tv.setTextSize(20); 
     tv.setBackgroundColor(Color.MAGENTA); 
     tv.setPadding(24, 5, 5, 5); 
     return tv; 
    } 
    @Override 
    public int getChildrenCount(int groupPosition) { 
     return 3; 
    } 
    @Override 
    public Object getGroup(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public int getGroupCount() { 
     return 1; 
    } 
    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 
    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 
     TextView tv = new TextView(MainActivity.this); 
     tv.setText("->four level"); 
     tv.setTextColor(Color.BLACK); 
     tv.setTextSize(20); 
     tv.setBackgroundColor(Color.CYAN); 
     tv.setPadding(21, 5, 5, 5); 
     return tv; 
    } 
    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 
    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) { 
     return true; 
    } 
    } 
} 

爲了獲得onCreateI的ID用下面的代碼:

explvlist.setOnItemClickListener(new OnItemClickListener() { 
    @Override 
    public void onItemClick(AdapterView<?> parent, View v, int groupPosition, long id) { 
    // TODO Auto-generated method stub 
    int itemType = ExpandableListView.getPackedPositionType(groupPosition); 
    Log.i("item type", "" + itemType); 
    if (itemType == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 
     childPosition = ExpandableListView.getPackedPositionChild(id); 
     groupPosition = ExpandableListView.getPackedPositionGroup(id); 
     Log.i("child", "" + childPosition); 
     Log.i("child Group", "" + groupPosition); 
    } else if (itemType == ExpandableListView.PACKED_POSITION_TYPE_GROUP) { 
     groupPosition = ExpandableListView.getPackedPositionGroup(id); 
     Log.i("Group", "" + groupPosition); 
    } 
    } 
}); 

以下代碼:

explvlist.setOnChildClickListener(new OnChildClickListener() { 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
    Toast.makeText(getBaseContext(), "Child clicked", Toast.LENGTH_SHORT).show(); 
    System.out.println(getBaseContext() + "Child clicked"); 
    return true; 
    } 
}); 

但它並沒有幫助任何 輸出不顯示任何東西! 請幫助

回答

2

試試這個

爲獲得組點擊

expandableListView.setOnGroupClickListener(new OnGroupClickListener() { 
     public boolean onGroupClick(ExpandableListView arg0,View arg1, int groupPosition, long arg3) { 

     return false; 
     } 
    ); 

爲了得到孩子單擊

expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
    public boolean onChildClick(ExpandableListView parent,View v, int groupPosition, int childPosition, 
           long id) { 
     return false; 
     } 
    }); 
+0

感謝答案。 – user2261110

+0

Awsem ...爲我工作...謝謝 –

0
expandableList.setOnGroupClickListener(new OnGroupClickListener() 
     { 
       public boolean onGroupClick(ExpandableListView arg0, View arg1, int groupPosition, long arg3) 
       { 
        Toast.makeText(getBaseContext(), "" + groupPosition, 1000).show(); 
        return false; 
       } 
     });