2013-04-14 73 views
0

我有兩個類:一個是適配器,第二個是我的活動。當我找到一些代碼來打開不同的類,如果我點擊不同的項目,我的應用程序只是停止,並且項目甚至不可點擊。當我點擊一個項目時,它並不高調,並且應用程序停止。如何使onChildclick真正可點擊進入可擴展列表視圖

因此,這裏是我的適配器

@SuppressWarnings("unchecked") 
public class NewAdapter extends BaseExpandableListAdapter { 

    public ArrayList<String> groupItem, tempChild; 
    public ArrayList<Object> Childtem = new ArrayList<Object>(); 
    public LayoutInflater minflater; 
    public Activity activity; 

    public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) { 
     groupItem = grList; 
     this.Childtem = childItem; 
    } 

    public void setInflater(LayoutInflater mInflater, Activity act) { 
     this.minflater = mInflater; 
     activity = act; 
    } 

    @Override 
    public Object getChild(int groupPosition, int childPosition) { 
     return null; 
    } 

    @Override 
    public long getChildId(int groupPosition, int childPosition) { 
     return 0; 
    } 

    @Override 
    public View getChildView(int groupPosition, final int childPosition, 
      boolean isLastChild, View convertView, ViewGroup parent) { 
     tempChild = (ArrayList<String>) Childtem.get(groupPosition); 
     TextView text = null; 
     if (convertView == null) { 
      convertView = minflater.inflate(R.layout.childrow, null); 
     } 
     text = (TextView) convertView.findViewById(R.id.textView1); 
     text.setText(tempChild.get(childPosition)); 
     /*convertView.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast.makeText(activity, tempChild.get(childPosition), 
         Toast.LENGTH_SHORT).show(); 
      } 
     });*/ 
     return convertView; 
    } 

    @Override 
    public int getChildrenCount(int groupPosition) { 
     return ((ArrayList<String>) Childtem.get(groupPosition)).size(); 
    } 

    @Override 
    public Object getGroup(int groupPosition) { 
     return null; 
    } 

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

    @Override 
    public void onGroupCollapsed(int groupPosition) { 
     super.onGroupCollapsed(groupPosition); 
    } 

    @Override 
    public void onGroupExpanded(int groupPosition) { 
     super.onGroupExpanded(groupPosition); 
    } 

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

    @Override 
    public View getGroupView(int groupPosition, boolean isExpanded, 
      View convertView, ViewGroup parent) { 
     if (convertView == null) { 
      convertView = minflater.inflate(R.layout.grouprow, null); 
     } 
     ((CheckedTextView) convertView).setText(groupItem.get(groupPosition)); 
     //((CheckedTextView) convertView).setChecked(isExpanded); 
     return convertView; 
    } 

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

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

我的活動 - 我只是想前兩個工作,我有更多的家長和孩子,但如果這些工作中,我會做其餘相同。

public class Mecanica extends ExpandableListActivity implements OnChildClickListener { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     ExpandableListView expandbleLis = getExpandableListView(); 
     expandbleLis.setDividerHeight(2); 
     expandbleLis.setGroupIndicator(null); 
     expandbleLis.setClickable(true); 



     setGroupData(); 
     setChildGroupData(); 

     NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem); 
     mNewAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this); 
     getExpandableListView().setAdapter(mNewAdapter); 
     expandbleLis.setOnChildClickListener(this); 
    } 
    @Override 
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { 
     // Create a switch that switches on the specific child position. 
     switch(childPosition) { 
     case 0: 
      // Go to child #0 specific class. 
      Intent child0Intent = new Intent(this, Mecanica1.class); 
      startActivity(child0Intent); 
      break; 
     case 1: 
      // Go to child #1 specific class. 
      Intent child1Intent = new Intent(this, Mecanica2.class); 
      startActivity(child1Intent); 
      break; 
     case 2: 
      // Go to child #1 specific class. 
      Intent child2Intent = new Intent(this, Mecanica3.class); 
      startActivity(child2Intent); 
      break; 
     case 3: 
      // Go to child #1 specific class. 
      Intent child3Intent = new Intent(this, Mecanica4.class); 
      startActivity(child3Intent); 
      break; 
     case 4: 
      // Go to child #1 specific class. 
      Intent child4Intent = new Intent(this, Mecanica5.class); 
      startActivity(child4Intent); 
      break; 
     case 5: 
      // Go to child #1 specific class. 
      Intent child5Intent = new Intent(this, Mecanica6.class); 
      startActivity(child5Intent); 
      break; 
     case 6: 
      // Go to child #1 specific class. 
      Intent child6Intent = new Intent(this, Mecanica7.class); 
      startActivity(child6Intent); 
      break; 
     case 7: 
      // Go to child #1 specific class. 
      Intent child7Intent = new Intent(this, Mecanica8.class); 
      startActivity(child7Intent); 
      break; 

     } 
     return false; 
} 


    public void setGroupData() { 
     groupItem.add("Directia"); 
     groupItem.add("Franarea"); 
     // groupItem.add("Motorul"); 
     //groupItem.add("Rotile"); 
     //groupItem.add("Siguranta si control"); 
     //groupItem.add("Suspensia"); 
     //groupItem.add("Transmisia"); 
    } 

    ArrayList<String> groupItem = new ArrayList<String>(); 
    ArrayList<Object> childItem = new ArrayList<Object>(); 

    public void setChildGroupData() { 
     /** 
     * Add Data For TecthNology 
     */ 
     ArrayList<String> child = new ArrayList<String>(); 
     child.add("Java"); 
     child.add("Drupal"); 
     child.add(".Net Framework"); 
     child.add("PHP"); 
     childItem.add(child); 

     /** 
     * Add Data For Mobile 
     */ 
    child = new ArrayList<String>(); 
     child.add("Android"); 
     child.add("Window Mobile"); 
     child.add("iPHone"); 
     child.add("Blackberry"); 
     childItem.add(child); 
//  /** 
//   * Add Data For Manufacture 
//   */ 
//  child = new ArrayList<String>(); 
//  child.add("HTC"); 
//  child.add("Apple"); 
//  child.add("Samsung"); 
//  child.add("Nokia"); 
//  childItem.add(child); 
//  /** 
//   * Add Data For Extras 
//   */ 
//  child = new ArrayList<String>(); 
//  child.add("Contact Us"); 
//  child.add("About Us"); 
//  child.add("Location"); 
//  child.add("Root Cause"); 
//  childItem.add(child); 
    }  
} 

和logcat的

04-13 19:44:54.753: E/AndroidRuntime(1724): FATAL EXCEPTION: main 
04-13 19:44:54.753: E/AndroidRuntime(1724): java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at java.util.ArrayList.get(ArrayList.java:304) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at com.example.vreaucarnet.NewAdapter.getChildrenCount(NewAdapter.java:65) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.widget.ExpandableListConnector.refreshExpGroupMetadataList(ExpandableListConnector.java:563) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.widget.ExpandableListConnector.expandGroup(ExpandableListConnector.java:688) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.widget.ExpandableListView.handleItemClick(ExpandableListView.java:562) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.widget.ExpandableListView.performItemClick(ExpandableListView.java:522) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.widget.AbsListView$1.run(AbsListView.java:3423) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.os.Handler.handleCallback(Handler.java:725) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.os.Looper.loop(Looper.java:137) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at java.lang.reflect.Method.invoke(Method.java:511) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
04-13 19:44:54.753: E/AndroidRuntime(1724):  at dalvik.system.NativeStart.main(Native Method) 

回答

0
 Mecanica.java 

    package com.example.expademo; 

    import java.util.ArrayList; 

    import android.os.Bundle; 
    import android.app.Activity; 
    import android.app.ExpandableListActivity; 
    import android.content.Context; 
    import android.content.Intent; 
    import android.view.LayoutInflater; 
    import android.view.Menu; 
    import android.view.View; 
    import android.widget.ExpandableListView; 
    import android.widget.ExpandableListView.OnChildClickListener; 
    import android.widget.ListView; 

    public class Mecanica extends ExpandableListActivity implements 
      OnChildClickListener { 
     ArrayList<String> groupItem = new ArrayList<String>(); 
     ArrayList<Object> childItem = new ArrayList<Object>(); 

     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      setContentView(R.layout.activity_main); 

      ExpandableListView expandbleLis = getExpandableListView(); 
      expandbleLis.setDividerHeight(2); 
      expandbleLis.setGroupIndicator(null); 
      expandbleLis.setClickable(true); 

      setGroupData(); 
      setChildGroupData(); 

      NewAdapter mNewAdapter = new NewAdapter(groupItem, childItem); 
      mNewAdapter 
        .setInflater(
          (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), 
          this); 
      getExpandableListView().setAdapter(mNewAdapter); 
      expandbleLis.setOnChildClickListener(this); 
     } 

     @Override 
     public boolean onChildClick(ExpandableListView parent, View v, 
       int groupPosition, int childPosition, long id) { 
      // Create a switch that switches on the specific child position. 
      switch (childPosition) { 
      case 0: 
       // Go to child #0 specific class. 
       Intent child0Intent = new Intent(this, MainActivity.class); 
       startActivity(child0Intent); 
       break; 
      case 1: 
       // Go to child #1 specific class. 
       Intent child1Intent = new Intent(this, MainActivity.class); 
       startActivity(child1Intent); 
       break; 
      case 2: 
       // Go to child #1 specific class. 
       Intent child2Intent = new Intent(this, MainActivity.class); 
       startActivity(child2Intent); 
       break; 
      case 3: 
       // Go to child #1 specific class. 
       Intent child3Intent = new Intent(this, MainActivity.class); 
       startActivity(child3Intent); 
       break; 
      case 4: 
       // Go to child #1 specific class. 
       Intent child4Intent = new Intent(this, MainActivity.class); 
       startActivity(child4Intent); 
       break; 
      case 5: 
       // Go to child #1 specific class. 
       Intent child5Intent = new Intent(this, MainActivity.class); 
       startActivity(child5Intent); 
       break; 
      case 6: 
       // Go to child #1 specific class. 
       Intent child6Intent = new Intent(this, MainActivity.class); 
       startActivity(child6Intent); 
       break; 
      case 7: 
       // Go to child #1 specific class. 
       Intent child7Intent = new Intent(this, MainActivity.class); 
       startActivity(child7Intent); 
       break; 

      } 
      return false; 
     } 

     public void setGroupData() { 
      groupItem.add("Directia"); 
      groupItem.add("Franarea"); 
      groupItem.add("Motorul"); 
      groupItem.add("Rotile"); 
      groupItem.add("Siguranta si control"); 
      groupItem.add("Suspensia"); 
      groupItem.add("Transmisia"); 
     } 

     public void setChildGroupData() { 
      /** 
      * Add Data For TecthNology 
      */ 
      ArrayList<String> child = new ArrayList<String>(); 
      child.add("Java"); 
      child.add("Drupal"); 
      child.add(".Net Framework"); 
      child.add("PHP"); 
      childItem.add(child); 

      /** 
      * Add Data For Mobile 
      */ 
      child = new ArrayList<String>(); 
      child.add("Android"); 
      child.add("Window Mobile"); 
      child.add("iPHone"); 
      child.add("Blackberry"); 
      childItem.add(child); 
      // /** 
      // * Add Data For Manufacture 
      // */ 
      child = new ArrayList<String>(); 
      child.add("HTC"); 
      child.add("Apple"); 
      child.add("Samsung"); 
      child.add("Nokia"); 
      childItem.add(child); 
      // /** 
      // * Add Data For Extras 
      // */ 
      child = new ArrayList<String>(); 
      child.add("Contact Us"); 
      child.add("About Us"); 
      child.add("Location"); 
      child.add("Root Cause"); 
      childItem.add(child); 

      // /** 
      // * Add Data For Extras 
      // */ 
      child = new ArrayList<String>(); 
      child.add("Contact Us"); 
      child.add("About Us"); 
      child.add("Location"); 
      child.add("Root Cause"); 
      childItem.add(child); 
      // /** 
      // * Add Data For Extras 
      // */ 
      child = new ArrayList<String>(); 
      child.add("Contact Us"); 
      child.add("About Us"); 
      child.add("Location"); 
      child.add("Root Cause"); 
      childItem.add(child); 
      // /** 
      // * Add Data For Extras 
      // */ 
      child = new ArrayList<String>(); 
      child.add("Contact Us"); 
      child.add("About Us"); 
      child.add("Location"); 
      child.add("Root Cause"); 
      childItem.add(child); 
     } 
    } 


    NewAdapter.java 

    package com.example.expademo; 

    import java.util.ArrayList; 

    import android.app.Activity; 
    import android.view.LayoutInflater; 
    import android.view.View; 
    import android.view.ViewGroup; 
    import android.widget.BaseExpandableListAdapter; 
    import android.widget.CheckedTextView; 
    import android.widget.TextView; 

    @SuppressWarnings("unchecked") 
    public class NewAdapter extends BaseExpandableListAdapter { 

     public ArrayList<String> groupItem, tempChild; 
     public ArrayList<Object> Childtem = new ArrayList<Object>(); 
     public LayoutInflater minflater; 
     public Activity activity; 

     public NewAdapter(ArrayList<String> grList, ArrayList<Object> childItem) { 
      groupItem = grList; 
      this.Childtem = childItem; 
     } 

     public void setInflater(LayoutInflater mInflater, Activity act) { 
      this.minflater = mInflater; 
      activity = act; 
     } 

     @Override 
     public Object getChild(int groupPosition, int childPosition) { 
      return null; 
     } 

     @Override 
     public long getChildId(int groupPosition, int childPosition) { 
      return 0; 
     } 

     @Override 
     public View getChildView(int groupPosition, final int childPosition, 
       boolean isLastChild, View convertView, ViewGroup parent) { 
      tempChild = (ArrayList<String>) Childtem.get(groupPosition); 
      TextView text = null; 
      if (convertView == null) { 
       convertView = minflater.inflate(R.layout.childrow, null); 
      } 
      text = (TextView) convertView.findViewById(R.id.textView1); 
      text.setText(tempChild.get(childPosition)); 
      /* 
      * convertView.setOnClickListener(new OnClickListener() { 
      * 
      * @Override public void onClick(View v) { Toast.makeText(activity, 
      * tempChild.get(childPosition), Toast.LENGTH_SHORT).show(); } }); 
      */ 
      return convertView; 
     } 

     @Override 
     public int getChildrenCount(int groupPosition) { 
      return ((ArrayList<String>) Childtem.get(groupPosition)).size(); 
     } 

     @Override 
     public Object getGroup(int groupPosition) { 
      return null; 
     } 

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

     @Override 
     public void onGroupCollapsed(int groupPosition) { 
      super.onGroupCollapsed(groupPosition); 
     } 

     @Override 
     public void onGroupExpanded(int groupPosition) { 
      super.onGroupExpanded(groupPosition); 
     } 

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

     @Override 
     public View getGroupView(int groupPosition, boolean isExpanded, 
       View convertView, ViewGroup parent) { 
      if (convertView == null) { 
       convertView = minflater.inflate(R.layout.grouprow, null); 
      } 
      TextView text = (TextView) convertView.findViewById(R.id.textView); 
      text.setText(groupItem.get(groupPosition)); 
      // ((CheckedTextView) convertView).setChecked(isExpanded); 
      return convertView; 
     } 

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

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

    grouprow.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" > 

     <TextView 
      android:id="@+id/textView" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Medium Text" 
      android:textAppearance="?android:attr/textAppearanceMedium" /> 

    </LinearLayout> 

activity_main.xml 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context=".MainActivity" > 

    <ExpandableListView 
     android:id="@android:id/list" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </ExpandableListView> 

</LinearLayout> 
+0

你有沒有測試代碼,看看它是否工作?你忘了孩子的xml – user2152480

+0

我測試過的代碼。我沒有忘記childrow.xml。你已經有了,所以我沒有回覆。無論我的答案是否適合你? –

+0

是的,它的工作原理非常感謝!我的問題並不意味着,我只是好奇 – user2152480