2014-10-10 65 views
1

我有一個expandableListView其中有三個組項目,每個組包含五個子項目。我想給所有組的每個子項上的onClick事件,並在每個不同的頁面上使用Intent顯示。Android onChildClickListener可展開ListView

我已經完成的代碼結果如下。 onChildClickListener適用於第一組子項目。但是當我點擊其他兩個組的子項時,它又顯示了我爲第一組子項目給出的頁面。我在下面發佈我的代碼。請有人建議我一些解決方案。

MoreActivity.java

package info.demo.goa; 

public class MoreActivity extends Activity { 

private ExpandListAdapter ExpAdapter; 
private ArrayList<Group> ExpListItems; 
private ExpandableListView ExpandList; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.more_tab); 

    ExpandList = (ExpandableListView) findViewById(R.id.exp_list); 
    ExpListItems = SetStandardGroups(); 
    ExpAdapter = new ExpandListAdapter(MoreActivity.this, ExpListItems); 
    ExpandList.setAdapter(ExpAdapter); 

    ExpandList.setOnChildClickListener(new OnChildClickListener() { 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) 
     { 
      if(childPosition==0) 
      { 
       Intent i=new Intent(getApplicationContext(), Basilica_ch.class); 
       startActivity(i); 
      } 
      if(childPosition==1) 
      { 
       Intent i=new Intent(getApplicationContext(), StFrancis_ch.class); 
       startActivity(i); 
      } 
      if(childPosition==2) 
      { 
       Intent i=new Intent(getApplicationContext(), Lady_ch.class); 
       startActivity(i); 
      } 
      if(childPosition==3) 
      { 
       Intent i=new Intent(getApplicationContext(), StThomas_ch.class); 
       startActivity(i); 
      } 
      if(childPosition==4) 
      { 
       Intent i=new Intent(getApplicationContext(), SeCathedral_ch.class); 
       startActivity(i); 
      } 
      if(childPosition==5) 
      { 
       Intent i=new Intent(getApplicationContext(), Aguada_f.class); 
       startActivity(i); 
      } 
      if(childPosition==6) 
      { 
       Intent i=new Intent(getApplicationContext(), Chapora_f.class); 
       startActivity(i); 
      } 
      if(childPosition==7) 
      { 
       Intent i=new Intent(getApplicationContext(), CaboDerama_f.class); 
       startActivity(i); 
      } 
      if(childPosition==8) 
      { 
       Intent i=new Intent(getApplicationContext(), Rachol_f.class); 
       startActivity(i); 
      } 
      if(childPosition==9) 
      { 
       Intent i=new Intent(getApplicationContext(), Terekhol_f.class); 
       startActivity(i); 
      } 
      if(childPosition==10) 
      { 
       Intent i=new Intent(getApplicationContext(), Archa_m.class); 
       startActivity(i); 
      } 
      if(childPosition==11) 
      { 
       Intent i=new Intent(getApplicationContext(), Calizz_m.class); 
       startActivity(i); 
      } 
      if(childPosition==12) 
      { 
       Intent i=new Intent(getApplicationContext(), GoaState_m.class); 
       startActivity(i); 
      } 
      if(childPosition==13) 
      { 
       Intent i=new Intent(getApplicationContext(), Naval_m.class); 
       startActivity(i); 
      } 
      if(childPosition==14) 
      { 
       Intent i=new Intent(getApplicationContext(), Palacio_m.class); 
       startActivity(i); 
      } 
      return false; 
     } 
    }); 

} 

public ArrayList<Group> SetStandardGroups() { 

    String group_names[] = { "Churches", "Forts", "Museums" }; 


    String country_names[] = { "Basilica of Bom Jesus", "Church and Convent of St Francis of Assisi", 
      "Church of Our Lady of Immaculate Conception", "Church of St. Thomas", "Se Cathedral", 
      "The Aguada Fort", "The Chapora Fort", "The Cabo de Rama Fort", "Rachol Fort", "Terekhol Fort", 
      "Archaeological Museum", "Calizz Museum", "Goa State Museum", "Naval Aviation Museum ", "Palacio Do deao" 
      }; 

    int Images[] = { 
      R.drawable.basilicaofbomjesus, R.drawable.stfrancisofassisi,  R.drawable.churchofourladyofimmaculateconception, 
      R.drawable.stthomas, R.drawable.secathedral, R.drawable.fortaguda, 
      R.drawable.chaporafort, R.drawable.cabodarama, R.drawable.racholfort, 
      R.drawable.terekholfort, R.drawable.archaeologicalmuseum, R.drawable.calizzmuseum, 
      R.drawable.goastatemuseum, R.drawable.navalaviationmuseum, R.drawable.palaciododeao, 
      }; 

    ArrayList<Group> list = new ArrayList<Group>(); 

    ArrayList<Child> ch_list; 

    int size = 5; 
    int j = 0; 

    for (String group_name : group_names) { 
     Group gru = new Group(); 
     gru.setName(group_name); 

     ch_list = new ArrayList<Child>(); 
     for (; j < size; j++) { 
      Child ch = new Child(); 
      ch.setName(country_names[j]); 
      ch.setImage(Images[j]); 
      ch_list.add(ch); 
     } 
     gru.setItems(ch_list); 
     list.add(gru); 

     size = size + 5; 
    } 

    return list; 
} 

class ExpandListAdapter extends BaseExpandableListAdapter { 

    private Context context; 
    private ArrayList<Group> groups; 

    public ExpandListAdapter(Context context, ArrayList<Group> groups) { 
     this.context = context; 
     this.groups = groups; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     ArrayList<Child> chList = groups.get(groupPosition).getItems(); 
     return chList.get(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) { 

     Child child = (Child) getChild(groupPosition, childPosition); 
     if (convertView == null) { 
      LayoutInflater infalInflater = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = infalInflater.inflate(R.layout.child_item, null); 
     } 
     ImageView iv = (ImageView) convertView.findViewById(R.id.imgItemImage); 
     TextView tv = (TextView) convertView.findViewById(R.id.txtItemName); 



     tv.setText(child.getName().toString()); 
     iv.setImageResource(child.getImage()); 

     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     ArrayList<Child> chList = groups.get(groupPosition).getItems(); 
     return chList.size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return groups.get(groupPosition); 
    } 

    @Override 
    public int getGroupCount() { 
     return groups.size(); 
    } 

    @Override 
    public long getGroupId(int groupPosition) { 
     return groupPosition; 
    } 

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     Group group = (Group) getGroup(groupPosition); 
     if (convertView == null) { 
      LayoutInflater inf = (LayoutInflater) context 
        .getSystemService(context.LAYOUT_INFLATER_SERVICE); 
      convertView = inf.inflate(R.layout.group_item, null); 
     } 
     TextView tv = (TextView) convertView.findViewById(R.id.group_name); 
     tv.setText(group.getName()); 
     return convertView; 
    } 

    @Override 
    public boolean hasStableIds() { 
     return true; 
    } 

    @Override 
    public boolean isChildSelectable(int groupPosition, int childPosition) 
    { 
     return true; 
    } 

} 

} 


Child.java 

package info.demo.goa; 

public class Child { 

private String Name; 
private int Image; 

public String getName() { 
    return Name; 
} 

public void setName(String Name) { 
    this.Name = Name; 
} 

public int getImage() { 
    return Image; 
} 

public void setImage(int Image) { 
    this.Image = Image; 
} 
} 

Group.java 

package info.demo.goa; 

import java.util.ArrayList; 

public class Group { 

private String Name; 
private ArrayList<Child> Items; 

public String getName() { 
    return Name; 
} 

public void setName(String name) { 
    this.Name = name; 
} 

public ArrayList<Child> getItems() { 
    return Items; 
} 

public void setItems(ArrayList<Child> Items) { 
    this.Items = Items; 
} 

} 

XML代碼

more_tab.xml 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 

<ExpandableListView 
    android:id="@+id/exp_list" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent"> 
</ExpandableListView> 

</RelativeLayout> 


child_item.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:gravity="center"> 

<ImageView 
    android:id="@+id/imgItemImage" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:adjustViewBounds="true" 
    android:src="@drawable/ic_launcher" /> 

<LinearLayout 
    android:layout_width="0dp" 
    android:layout_height="40dp" 
    android:layout_marginLeft="5dp" 
    android:layout_weight="0.92" > 

    <TextView 
     android:id="@+id/txtItemName" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:textColor="@android:color/black"/> 
</LinearLayout> 

<ImageView 
    android:id="@+id/imgItemArrow" 
    android:layout_width="15dp" 
    android:layout_height="15dp" 
    android:src="@drawable/arrow_icon" 
    android:layout_marginRight="5dp"/> 

    </LinearLayout> 


    group_item.xml 


<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" 
android:padding="10dp" > 

<TextView 
    android:id="@+id/group_name" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="25dp" 
    android:textStyle="bold"/> 
</LinearLayout> 

回答

0
If Group Position is 1 then 
1 - 0 , 1, 2, 3, 4 
2 - 0 , 1, 2, 3, 4 but not 5, 6, 7, 8, 9 
3 - 0 , 1, 2, 3, 4 but not 10, 11, 12, 13, 14 
@Override 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
     if (groupPosition == 0) { 
      if (childPosition == 0) { 
       Intent i = new Intent(getApplicationContext(), Basilica_ch.class); 
       startActivity(i); 
      } 
      if (childPosition == 1) { 
       Intent i = new Intent(getApplicationContext(), StFrancis_ch.class); 
       startActivity(i); 
      } 
      if (childPosition == 2) { 
       Intent i = new Intent(getApplicationContext(), Lady_ch.class); 
       startActivity(i); 
      } 
      if (childPosition == 3) { 
       Intent i = new Intent(getApplicationContext(), StThomas_ch.class); 
       startActivity(i); 
      } 
      if (childPosition == 4) { 
       Intent i = new Intent(getApplicationContext(), SeCathedral_ch.class); 
       startActivity(i); 
      } 
     } 
     if (groupPosition == 1) { 
      if (childPosition == 0) { 
       Intent i = new Intent(getApplicationContext(), Aguada_f.class); 
       startActivity(i); 
      } 
      if (childPosition == 1) { 
       Intent i = new Intent(getApplicationContext(), Chapora_f.class); 
       startActivity(i); 
      } 
      if (childPosition == 2) { 
       Intent i = new Intent(getApplicationContext(), CaboDerama_f.class); 
       startActivity(i); 
      } 
      if (childPosition == 3) { 
       Intent i = new Intent(getApplicationContext(), Rachol_f.class); 
       startActivity(i); 
      } 
      if (childPosition == 4) { 
       Intent i = new Intent(getApplicationContext(), Terekhol_f.class); 
       startActivity(i); 
      } 
     } 
     if (groupPosition == 2) { 
      if (childPosition == 0) { 
       Intent i = new Intent(getApplicationContext(), Archa_m.class); 
       startActivity(i); 
      } 
      if (childPosition == 1) { 
       Intent i = new Intent(getApplicationContext(), Calizz_m.class); 
       startActivity(i); 
      } 
      if (childPosition == 2) { 
       Intent i = new Intent(getApplicationContext(), GoaState_m.class); 
       startActivity(i); 
      } 
      if (childPosition == 3) { 
       Intent i = new Intent(getApplicationContext(), Naval_m.class); 
       startActivity(i); 
      } 
      if (childPosition == 4) { 
       Intent i = new Intent(getApplicationContext(), Palacio_m.class); 
       startActivity(i); 
      } 
     } 
     return false; 
    } 
+0

非常感謝這個解決方案。這是完美的。 – user3651722 2014-10-10 11:28:17

+0

@ user3651722,歡迎您... – 2014-10-10 12:03:06