android中的多级可扩展数据集中的问题

时间:2016-06-27 04:48:58

标签: android expandablelistview

我在下面有多级可扩展列表视图是我的代码

public class MainActivity extends AppCompatActivity {

public static final int FIRST_LEVEL_COUNT = 6;
public static final int SECOND_LEVEL_COUNT = 4;
public static final int THIRD_LEVEL_COUNT = 20;
private ExpandableListView expandableListView;

private ArrayList<Product> pProductArrayList;
private ArrayList<Product.SubCategory> pSubItemArrayList;
private ArrayList<Product.SubCategory> pSubItemArrayList2;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    expandableListView = (ExpandableListView) findViewById(R.id.mainList);

    ArrayList<Product.SubCategory.ItemList> mItemListArray = new ArrayList<Product.SubCategory.ItemList>();
    mItemListArray.add(new Product.SubCategory.ItemList("Red", "20"));
    mItemListArray.add(new Product.SubCategory.ItemList("Blue", "50"));
    mItemListArray.add(new Product.SubCategory.ItemList("Red", "20"));
    mItemListArray.add(new Product.SubCategory.ItemList("Blue", "50"));

    ArrayList<Product.SubCategory.ItemList> mItemListArray2 = new ArrayList<Product.SubCategory.ItemList>();
    mItemListArray2.add(new Product.SubCategory.ItemList("Pant", "2000"));
    mItemListArray2.add(new Product.SubCategory.ItemList("Shirt", "1000"));
    mItemListArray2.add(new Product.SubCategory.ItemList("Pant", "2000"));
    mItemListArray2.add(new Product.SubCategory.ItemList("Shirt", "1000"));
    mItemListArray2.add(new Product.SubCategory.ItemList("Pant", "2000"));
    mItemListArray2.add(new Product.SubCategory.ItemList("Shirt", "1000"));


    /**
     *
     */
    pSubItemArrayList = new ArrayList<Product.SubCategory>();
    pSubItemArrayList2 = new ArrayList<Product.SubCategory>();
    pSubItemArrayList.add(new Product.SubCategory("Color", mItemListArray));
    pSubItemArrayList2.add(new Product.SubCategory("Cloths", mItemListArray2));
    pSubItemArrayList.add(new Product.SubCategory("Color", mItemListArray));
    pSubItemArrayList2.add(new Product.SubCategory("Cloths", mItemListArray2));
    /**
     *
     */

    pProductArrayList = new ArrayList<Product>();
    pProductArrayList.add(new Product("Emotions", pSubItemArrayList));
    pProductArrayList.add(new Product("Garments", pSubItemArrayList2));
    Log.d("pSubItemArrayList2", String.valueOf(pSubItemArrayList2.size()));
    expandableListView.setAdapter(new ParentLevel(this, pProductArrayList, pSubItemArrayList, pSubItemArrayList2));

    // First level items in the ExpandableListView
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                                    long id) {
            // TODO: whatever you need
            return false /* or true depending on what you need */;
        }
    });

    // Second level items in the ExpandableListView
    ExpandableListView.OnGroupClickListener grpLst = new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                                    long id) {
            // TODO: whatever you need
            return false /* or true depending on what you need */;
        }
    };

    // Third (and last) level items in the ExpandableListView
    ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
                                    int childPosition, long id) {
            // TODO: whatever you need
            return false /* or true depending on what you need */;
        }
    };

    ExpandableListView.OnGroupExpandListener grpExpLst = new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
            /* this one is not required of course, you can delete it from the RootAdapter Constructor
             * it is just an example as to how to implement Listeners on the second level items */
        }
    };
}

public class ParentLevel extends BaseExpandableListAdapter {

    private Context context;

    private ArrayList<Product> pProductArrayList;
    private ArrayList<Product.SubCategory> pSubItemArrayList;
    private ArrayList<Product.SubCategory> pSubItemArrayList2;

    public ParentLevel(Context context) {
        this.context = context;
    }

    public ParentLevel(MainActivity mainActivity, ArrayList<Product> pProductArrayList, ArrayList<Product.SubCategory> pSubItemArrayList, ArrayList<Product.SubCategory> pSubItemArrayList2) {
        this.context = mainActivity;
        this.pProductArrayList = pProductArrayList;
        this.pSubItemArrayList = pSubItemArrayList;
        this.pSubItemArrayList2 = pSubItemArrayList2;

    }

    @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) {
        SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(MainActivity.this);
        secondLevelELV.setAdapter(new SecondLevelAdapter(context, groupPosition, childPosition));
        secondLevelELV.setGroupIndicator(null);
        return secondLevelELV;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return pSubItemArrayList.size();
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_first, null);
            TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
            text.setText(pProductArrayList.get(groupPosition).getpName());
        }
        return convertView;
    }

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

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

public class SecondLevelExpandableListView extends ExpandableListView {

    public SecondLevelExpandableListView(Context context) {
        super(context);
    }

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //999999 is a size in pixels. ExpandableListView requires a maximum height in order to do measurement calculations.
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(999999, MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public class SecondLevelAdapter extends BaseExpandableListAdapter {

    private Context context;
    int group;
    int child;

    public SecondLevelAdapter(Context context) {
        this.context = context;
    }

    public SecondLevelAdapter(Context context, int groupPosition, int childPosition) {
        this.context = context;
        this.group = groupPosition;
        this.child = childPosition;
    }

    @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) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_third, null);
            TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
            final String catName = pProductArrayList.get(group).getmSubCategoryList().get(child).getpSubCatName();

            text.setText(catName);
        }
        return convertView;
    }

    @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) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.row_second, null);
            TextView text = (TextView) convertView.findViewById(R.id.eventsListEventRowText);
            text.setText(pProductArrayList.get(group).getmSubCategoryList().get(child).getmItemListArray().get(childPosition).getItemName() + " " + pProductArrayList.get(group).getmSubCategoryList().get(child).getmItemListArray().get(childPosition).getItemPrice());

        }
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return pSubItemArrayList2.size();
    }

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

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

当我在Cloth中的Garments选项中运行上面的代码时,只显示2个项目,它应该在最后一级显示6个项目,所以我知道如何解决这个问题?你的所有建议都很明显。

修改

public class Product {

private String pName;

private ArrayList<SubCategory> mSubCategoryList;

public Product(String pName, ArrayList<SubCategory> mSubCategoryList) {
    super();
    this.pName = pName;
    this.mSubCategoryList = mSubCategoryList;
}

public String getpName() {
    return pName;
}

public void setpName(String pName) {
    this.pName = pName;
}

public ArrayList<SubCategory> getmSubCategoryList() {
    return mSubCategoryList;
}

public void setmSubCategoryList(ArrayList<SubCategory> mSubCategoryList) {
    this.mSubCategoryList = mSubCategoryList;
}

/**
 * 
 * second level item
 * 
 */

public static class SubCategory {

    private String pSubCatName;
    private ArrayList<ItemList> mItemListArray;

    public SubCategory(String pSubCatName,
            ArrayList<ItemList> mItemListArray) {
        super();
        this.pSubCatName = pSubCatName;
        this.mItemListArray = mItemListArray;
    }

    public String getpSubCatName() {
        return pSubCatName;
    }

    public void setpSubCatName(String pSubCatName) {
        this.pSubCatName = pSubCatName;
    }

    public ArrayList<ItemList> getmItemListArray() {
        return mItemListArray;
    }

    public void setmItemListArray(ArrayList<ItemList> mItemListArray) {
        this.mItemListArray = mItemListArray;
    }

    /**
     * 
     * third level item
     * 
     */
    public static class ItemList {

        private String itemName;
        private String itemPrice;

        public ItemList(String itemName, String itemPrice) {
            super();
            this.itemName = itemName;
            this.itemPrice = itemPrice;
        }

        public String getItemName() {
            return itemName;
        }

        public void setItemName(String itemName) {
            this.itemName = itemName;
        }

        public String getItemPrice() {
            return itemPrice;
        }

        public void setItemPrice(String itemPrice) {
            this.itemPrice = itemPrice;
           }

         }

      }

   }

项目点击不工作

// First level items in the ExpandableListView
    expandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                                    long id) {
            // TODO: whatever you need
            Log.d("Grop ",pProductArrayList.get(groupPosition).getpName());
            return false /* or true depending on what you need */;
        }
    });

    // Second level items in the ExpandableListView
    ExpandableListView.OnGroupClickListener grpLst = new ExpandableListView.OnGroupClickListener() {
        @Override
        public boolean onGroupClick(ExpandableListView eListView, View view, int groupPosition,
                                    long id) {
            // TODO: whatever you need
            Log.d("Sub Grop ",pProductArrayList.get(groupPosition).getmSubCategoryList().get(groupPosition).getpSubCatName());


            return false /* or true depending on what you need */;
        }
    };

    // Third (and last) level items in the ExpandableListView
    ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
        @Override
        public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
                                    int childPosition, long id) {
            Log.d("Child Grop ",pProductArrayList.get(groupPosition).getmSubCategoryList().get(groupPosition).getmItemListArray().get(childPosition).getItemName()+" "+pProductArrayList.get(groupPosition).getmSubCategoryList().get(groupPosition).getmItemListArray().get(childPosition).getItemPrice());

            // TODO: whatever you need
            return false /* or true depending on what you need */;
        }
    };

    ExpandableListView.OnGroupExpandListener grpExpLst = new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {
        /* this one is not required of course, you can delete it from the RootAdapter Constructor
         * it is just an example as to how to implement Listeners on the second level items */
        }
    };

1 个答案:

答案 0 :(得分:1)

在SecondLevelAdapter中,getChildrenCount方法返回pSubItemArrayList2大小,该大小始终为2。更改如下:

@Override
        public int getChildrenCount(int groupPosition) {
            return pProductArrayList.get(group).getmSubCategoryList().get(child).getmItemListArray().size();
        }

对于项目单击:在ParentLevel适配器中更改此:

@Override
        public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            SecondLevelExpandableListView secondLevelELV = new SecondLevelExpandableListView(ExpandableActivity1.this);
            secondLevelELV.setAdapter(new SecondLevelAdapter(context, groupPosition, childPosition));
            secondLevelELV.setGroupIndicator(null);


            // Third (and last) level items in the ExpandableListView
            ExpandableListView.OnChildClickListener childLst = new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView eListView, View view, int groupPosition,
                                            int childPosition, long id) {
                    // TODO: whatever you need
                    Toast.makeText(getApplicationContext(),"test",Toast.LENGTH_SHORT).show();
                    return false /* or true depending on what you need */;
                }
            };

            secondLevelELV.setOnChildClickListener(childLst);

            return secondLevelELV;
        }