2013-03-08 72 views
3

我在android中开发三级可展开的listview。但是我的子节点按重复顺序填充。下面是我的源代码:在Expandable中重复儿童listView android

public class CarPanel extends ExpandableListActivity { 

    static ArrayList<String> groupItem = new ArrayList<String>(); 
    static ArrayList<String> childItem = new ArrayList<String>(); 
    static ArrayList<Object> grandChildItem = new ArrayList<Object>(); 
    static public ArrayList<String> childValue; 
    static public ArrayList<String> grandChildValue; 

    EditText modelType, price, dyp, insurance; 

    static int dummyFlag = 0; 
    public LayoutInflater minflater; 
    public Activity activity; 

    private MyDBManager dbManager; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState);  
     //Remove title bar 
     this.requestWindowFeature(Window.FEATURE_NO_TITLE); 
     //Remove notification bar  
     this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     dbManager = new MyDBManager(this); 
     dbManager.open(); 

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

     // 1st level data  
     setGroupData(); 

     // 2nd level data 
     setChildGroupData(); 

     // 3rd level data 
     setGrandChildGroupData(); 

     ParentLevel parentAdapter = new ParentLevel(groupItem); 
     parentAdapter.setInflater((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE), this); 
     getExpandableListView().setAdapter(parentAdapter); 
     expandbleLis.setOnChildClickListener(this); 

    } 

    public class ParentLevel extends BaseExpandableListAdapter { 


     public ParentLevel(ArrayList<String> groupList) { 
      groupItem = groupList; 
     } 

     public Object getChild(int arg0, int arg1) { 
      return arg1; 
     } 

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


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


     public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
      CustExpListview SecondLevelexplv = new CustExpListview(CarPanel.this); 
      SecondLevelexplv.setAdapter(new SecondLevelAdapter(childItem, grandChildItem)); 
      SecondLevelexplv.setGroupIndicator(null); 

      return SecondLevelexplv; 
     } 


     public int getChildrenCount(int groupPosition) { 
      Log.v("childItem.size()", "childItem.size() " +childItem.size()); 
      return childItem.size(); 
     } 


     public Object getGroup(int groupPosition) { 
      return groupPosition; 
     } 


     public int getGroupCount() { 
      Log.v("groupItem.size()", "groupItem.size() " +groupItem.size()); 
      return groupItem.size(); 
     } 


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

     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; 
     } 


     public boolean hasStableIds() { 
      return true; 
     } 


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

    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); 
     } 
    } 

    class SecondLevelAdapter extends BaseExpandableListAdapter { 

     public LayoutInflater minflater; 
     public Context activity; 

    public SecondLevelAdapter(ArrayList<String> childList, ArrayList<Object> grandChildList) { 
      childItem = childList; 
      grandChildItem = grandChildList; 
     } 

     public void setInflater(LayoutInflater mInflater, Context parentLevel) { 
      minflater = mInflater; 
      activity = parentLevel; 
     } 

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


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


     @SuppressWarnings("unchecked") 
     public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 
      grandChildValue = (ArrayList<String>) grandChildItem.get(groupPosition); 
      EditText modelType, price, dyp, insurance; 
      if (convertView == null) { 
       LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = layoutInflater.inflate(R.layout.grandchildrow, null); 
      } 

      modelType = (EditText) convertView.findViewById(R.id.et_ModelType); 
      price = (EditText) convertView.findViewById(R.id.et_Price); 
      dyp = (EditText) convertView.findViewById(R.id.et_DYP); 
      insurance = (EditText) convertView.findViewById(R.id.et_Insurance); 

      modelType.setText(grandChildValue.get(childPosition)); 

      convertView.setOnClickListener(new OnClickListener() { 
       public void onClick(View v) { 
        Toast.makeText(getBaseContext(), grandChildValue.get(childPosition), Toast.LENGTH_SHORT).show(); 
       } 
      }); 


      return convertView; 
     } 


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


     public Object getGroup(int groupPosition) { 
      return groupPosition; 
     } 

     public int getGroupCount() { 
      return childItem.size(); 
     } 


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


     public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { 

      if (convertView == null) { 
       LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = layoutInflater.inflate(R.layout.childrow, null); 
      } 
      ((CheckedTextView) convertView).setText(childItem.get(groupPosition)); 
      ((CheckedTextView) convertView).setChecked(isExpanded); 
      return convertView; 

     } 


     public boolean hasStableIds() { 
      return true; 
     } 


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

    } 

    public void setGroupData() { 
     groupItem.clear(); 

     Cursor cursor = dbManager.execQuery("SELECT DISTINCT carName FROM model"); 
     final List<ModelBean> modelList = new ArrayList<ModelBean>(); 
     cursor.moveToFirst(); 
     while (!cursor.isAfterLast()) { 
      ModelBean beanObj = cursorToModel(cursor); 
      modelList.add(beanObj); 
      cursor.moveToNext(); 
     } 
     // Make sure to close the cursor 
     cursor.close(); 
     ModelBean modelObj = null; 

     if (modelList.size() > 0) 
     { 
      for (int i = 0; i < modelList.size(); i++) 
      { 
       modelObj = modelList.get(i); 
       String carName = modelObj.getCarName();    
       groupItem.add(carName); 
      } 
     } 
    }  

    public void setChildGroupData() { 

     childItem.clear(); 

     childItem.add("Android"); 

     childItem.add("iOS"); 

     childItem.add("HTC"); 

    } 

    public void setGrandChildGroupData() { 
     grandChildItem.clear(); 

     ArrayList<String> child = new ArrayList<String>(); 
     child.add("PASSAT 1"); 
     child.add("PASSAT 2"); 
     grandChildItem.add(child); 

     child = new ArrayList<String>(); 
     child.add("POLO 1"); 
     child.add("POLO 2"); 
     grandChildItem.add(child); 

     child = new ArrayList<String>(); 
     child.add("VITZ 1"); 
     child.add("VITZ 2"); 
     grandChildItem.add(child); 

    } 

    private ModelBean cursorToModel(Cursor cursor) 
     { 
      ModelBean modelBean = new ModelBean(); 
      modelBean.setCarName(cursor.getString(0)); 

     return modelBean; 
     } 
} 

现在我想块的这段代码为每个父组只重复一次,但是它重复三次。我搜索了更多和类似的SO问题,但没有解决。

的Android

的iOS

HTC

+0

你有没有解决这个问题?如果是,如何? – AbhishekB 2013-03-19 12:43:15

+1

是的,我找到了解决办法。发布供将来参考[这里](http://naeemgik.blogspot.com/2013/08/android-expandablelistview-example.html) – sns 2014-07-01 07:51:09

回答

6

我这个同样的问题挣扎,无法具竞争力的超低/ SO找到解决方案。

做了一些日志记录之后,我得出结论:父级别的BaseExpandbleAdapater会调用/创建一个与您的数据集/组中的子级数对应的childView。不幸的是,这在三级嵌套列表场景中是不希望的。

所以产生了三级列表时,只是让功能getChildrenCount在这样父适配器:

@Override 
    public ing getChildrenCount(int groupPosition){ 
     return 1; 
    } 

现在只能建“1”的孩子列表项的迭代,即使1嵌套列表可以包含任意数量的子元素,从子元素adapater的角度来看,这些元素将成为组。

这很令人困惑,因为您的子/嵌套适配器处理显示实际的“子”,而在单个嵌套列表中,您的子对象在相同的父适配器类中处理。在这种情况下,子/嵌套适配器将处理显示什么是孩子作为其自己的groupView,从而使父适配器中的子项数量不相关。至少这就是我的结果和环境向我展示的......

希望我能帮上忙,这让我难倒了半天。

+1

感谢哥们,我一直在努力解决这个问题,差不多一周。 – Sid 2014-09-15 18:40:37