2011-05-07 3 views
0

Je suis très nouveau dans le développement Android, et a obtenu un emploi après mon diplôme BS. Il ya quelques jours je commence à travailler sur Android, comme mon chef d'équipe m'a assigné des tâches liées à GUI.J'entends parler de ce site Web d'un de mes amis, je suis confronté à un problème avec la liste extensible dans android.I prend un exemple de code de google search, que je poste dans ci-dessous. J'ai besoin d'aide pour ajouter un auditeur lorsque je clique sur childs du groupe. Je veux utiliser BaseExpandableListAdapter en utilisant ce que je ne suis pas en mesure d'attacher un écouteur Child Click.Joindre Listener sur ExpanableListView dans Android

Des idées?

import android.app.ExpandableListActivity; 
    import android.os.Bundle; 
    import android.view.Gravity; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.AbsListView; 
    import android.widget.BaseExpandableListAdapter; 
    import android.widget.ExpandableListAdapter; 
    import android.widget.TextView; 
    import android.widget.ExpandableListView.OnChildClickListener; 
    public class ExpandableList1 extends ExpandableListActivity implements 
     OnChildClickListener { 
    ExpandableListAdapter mAdapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     mAdapter = new MyExpandableListAdapter(); 
     setListAdapter(mAdapter); 
     getExpandableListView().setOnChildClickListener(this); 
    } 
    public class MyExpandableListAdapter extends BaseExpandableListAdapter { 
     private String[] groups = { "People Names", "Dog Names", "Cat Names", 
       "Fish Names" }; 
     private String[][] children = { 
       { "Arnold", "Barry", "Chuck", "David" }, 
       { "Ace", "Bandit", "Cha-Cha", "Deuce" }, 
       { "Fluffy", "Snuggles" }, { "Goldy", "Bubbles" } }; 

     public Object getChild(int groupPosition, int childPosition) { 
      return children[groupPosition][childPosition]; 
     } 
     public long getChildId(int groupPosition, int childPosition) { 
      return childPosition; 
     } 
     public int getChildrenCount(int groupPosition) { 
      return children[groupPosition].length; 
     } 
     public TextView getGenericView() { 
      AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT, 64); 
      TextView textView = new TextView(ExpandableList1.this); 
      textView.setLayoutParams(lp); 
      textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); 
      textView.setPadding(36, 0, 0, 0); 
      return textView; 
     } 
     public View getChildView(int groupPosition, int childPosition, 
       boolean isLastChild, View convertView, ViewGroup parent) { 
      TextView textView = getGenericView(); 
      textView.setText(getChild(groupPosition, childPosition).toString()); 
      return textView; 
     } 
     public Object getGroup(int groupPosition) { 
      return groups[groupPosition]; 
     } 
     public int getGroupCount() { 
      return groups.length; 
     } 
     public long getGroupId(int groupPosition) { 
      return groupPosition; 
     } 
     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
      TextView textView = getGenericView(); 
      textView.setText(getGroup(groupPosition).toString()); 
      return textView; 
     } 
     public boolean isChildSelectable(int groupPosition, int childPosition) { 
      return true; 
     } 
     public boolean hasStableIds() { 
      return true; 
     } 
    } 
} 

Répondre

2

Vous pouvez utiliser OnChildClickListener pour ExpandableListView

ExpandListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() { 
    public void onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
     Object e = (Object)adapter.getChild(groupPosition, childPosition); 
     //doing some work for child 
    } 
} 

Et sérieusement, vous feriez mieux de chercher dans les références android premier

http://developer.android.com/reference/android/widget/ExpandableListView.OnChildClickListener.html