0

我有例如。两个父项,其中第一个有一个孩子,第二个有 3 个孩子。如何显示它。如果我有第二个父视图,那么我没有找到这个视图点击日志。我想知道这个视图是点击日志。

我有多次相同的名字,所以显示一次相同的名字,孩子使用相同的名字。

像这样

Asia -> Japan

Europe-> Spain
Europe->England

现在我想显示

Asia->Japan
Europe-> spain
       ->England

我对android中的可扩展视图没有更多的想法。

这是 oncreate 的代码:

if (EduInterface.categoryIDArrayList.size() > 0) {

                    categoryName = new String[EduInterface.categoryNameArrayList
                            .size()][];
                    award = new String[EduInterface.pointIDArrayList.size()][];
                    pointName = new String[EduInterface.pointNameArrayList
                            .size()][];
                    pointValue = new String[EduInterface.pointValueArrayList
                            .size()][];

                    System.out.println("size of category name is ::"
                            + categoryName.length);

                    int catList = EduInterface.categoryIDArrayList.size();
                    Log.d("TAG", "Size of Cat===" + catList);
                    for (int j = 0; j < catList; j++)
                    {
                        for (int i = j; i < EduInterface.pointCategoryArrayList
                                .size(); i++) {
                            categoryName[j] = new String[1];
                            categoryName[j][i] = EduInterface.categoryNameArrayList
                                    .get(j);
                            pointName[j] = new String[1];
                            pointName[j][i] = EduInterface.pointNameArrayList
                                    .get(j);
                            pointValue[j] = new String[1];
                            pointValue[j][i] = EduInterface.pointValueArrayList
                                    .get(j);
                            System.out.println(j + ")categoryName is ::"
                                    + categoryName[j][i]
                                    + "  point value is ::" + pointName[j][i]
                                    + "---" + pointValue[j][i]);
                        }
                    }

                    pointcategoryExpandableList
                            .setAdapter(new MyExpandableAdapter());
                }

在 ExapanableAdater 中:

public class MyExpandableAdapter extends BaseExpandableListAdapter {

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return categoryName[groupPosition][childPosition];
        }

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

        @Override
        @SuppressLint("ResourceAsColor")
        public View getChildView(final int groupPosition, int childPosition,
                boolean isLastChild, View convertView, ViewGroup parent) {
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) PointCategory_index.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(
                        R.layout.point_category_childview, null);
            }

            // View descriptionviewView=(RelativeLayout)
            // convertView.findViewById(R.id.awardButton);
            if (isLastChild) {
                Log.d("TAG", "Last child");
            }
            final TextView awardTextView = (TextView) convertView
                    .findViewById(R.id.award);
            awardTextView.setTextColor(getResources().getColor(
                    R.color.award_color));
            awardTextView.setTypeface(font_regular);

            // for(int i=0;i<)
            /*
             * TextView point = (TextView) convertView
             * .findViewById(R.id.pointTextV); point.setText("Point");
             */
            TextView pointValuetxt = (TextView) convertView
                    .findViewById(R.id.points);
            pointValuetxt.setText(pointValue[groupPosition][childPosition]);
            pointValuetxt.setTypeface(font_regular);

            TextView pointInNumber = (TextView) convertView
                    .findViewById(R.id.pointTextV);
            pointInNumber.setText(pointName[groupPosition][childPosition]);
            pointInNumber.setTypeface(font_regular);

            if (userTypeId.trim().equalsIgnoreCase("4")
                    || userTypeId.trim().equalsIgnoreCase("1")) {
                awardTextView.setVisibility(View.VISIBLE);

                // check usertypeid
                // set point category id
            }

            awardTextView.setOnClickListener(new OnClickListener() {
                // int x=groupPosition;
                @Override
                public void onClick(View v) {
                    awardTextView.setTextColor(getResources().getColor(
                            R.color.green));

                    String id = EduInterface.pointIDArrayList
                            .get(groupPosition); // /Here change by dharma
                                                    // (categoryID->pointID)
                    Log.d("TAG", "Point Id test---->>>>>>" + id);
                    editor.putString("pointID", id);
                    /*
                     * Log.d("Click Point Id is", id);
                     * Log.d("Click Point Value is",
                     * EduInterface.pointValueArrayList.get(groupPosition));
                     */
                    editor.commit();

                    if (userTypeId.trim().equalsIgnoreCase("1")) {
                        startActivity(new Intent(PointCategory_index.this,
                                ChildActivity.class));// Go to
                                                        // professor
                                                        // List
                    } else if (userTypeId.trim().equalsIgnoreCase("4")) {
                        Log.d("TAG", "Award Button was Clicked !!1");
                        startActivity(new Intent(PointCategory_index.this,
                                PointCategory_courseList.class));
                    }

                }
            });
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return categoryName[groupPosition].length;
        }

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

        @Override
        public int getGroupCount() {
            // return EduInterface.categoryIDArrayList.size();
            return RemoveDuplicateCat.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) PointCategory_index.this
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = inflater.inflate(
                        R.layout.point_category_groupview, null);
            }
            TextView name = (TextView) convertView
                    .findViewById(R.id.categoryname);
            // name.setText(EduInterface.categoryNameArrayList.get(groupPosition));
            name.setText(RemoveDuplicateCat.get(groupPosition));
            name.setTypeface(font_regular);

            /*
             * convertView.setOnClickListener(new OnClickListener() {
             * 
             * @Override public void onClick(View v) { Log.d("TAG",
             * "TEST->>"+RemoveDuplicateCat.get(groupPosition)); TextView name =
             * (TextView) findViewById(R.id.categoryname);
             * name.setText(RemoveDuplicateCat.get(groupPosition));
             * name.setTypeface(font_regular); } });
             */
            /*
             * name.setOnClickListener(new View.OnClickListener() {
             * 
             * @Override public void onClick(View v) { Log.d("TAG",
             * "Clicking cATEEEE==="+RemoveDuplicateCat.get(groupPosition));
             * 
             * } });
             */
            // RemoveDuplicateCat

            return convertView;
        }

        /*
         * public boolean onChildClick(ExpandableListView parent, View v, int
         * groupPosition, int childPosition, long id) { String test =
         * (String)MyExpandableAdapter.getItem(groupPosition);
         * Log.d("TAG","CLICK->"+test); // update the text view with the country
         * return true; }
         */
        @Override
        public boolean hasStableIds() {
            return true;
        }

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

1 回答 1

1

我认为问题在于你如何填充你的数组。你正在做:

                        categoryName[j] = new String[1];
                        pointName[j] = new String[1];
                        pointValue[j] = new String[1];

ExpandableList 使用以下函数来计算它必须创建的子节点数量(使用 getView):

    public int getChildrenCount(int groupPosition) {
        return categoryName[groupPosition].length;
    }

因为你总是在做 new String[1],categoryName[groupPosition].length 总是返回 1,所以它总是只显示 1 个孩子。

如果你想让父母有 3 个孩子,你应该做 new String[3] (当然,用你想要显示的数据填充这个位置)。

我不能给你确切的代码,因为我不知道你是如何存储数据的,但我认为你应该这样做:

           for (int j = 0; j < catList; j++)
                {
                    categoryName[j] = new String[NUMBER_OF_CHILDS_THIS_CAT_HAS];
                    pointName[j] = new String[NUMBER_OF_CHILDS_THIS_CAT_HAS];
                    pointValue[j] = new String[NUMBER_OF_CHILDS_THIS_CAT_HAS];

                    for (int i = j; i < EduInterface.pointCategoryArrayList
                            .size(); i++) {
                        categoryName[j][i] = EduInterface.categoryNameArrayList
                                .get(j);
                        pointName[j][i] = EduInterface.pointNameArrayList
                                .get(j);
                        pointValue[j][i] = EduInterface.pointValueArrayList
                                .get(j);
                        System.out.println(j + ")categoryName is ::"
                                + categoryName[j][i]
                                + "  point value is ::" + pointName[j][i]
                                + "---" + pointValue[j][i]);
                    }
                }

[编辑]

哦,我忘了回答你的第二个问题,家长点击。要知道是否单击了父级,您需要在 getGroupView 中使用 onClickListener,就像您评论的代码一样,但是您有一些错误。如果你想点击组的位置,你需要将它存储在某个地方,例如,在视图的标签中:

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) PointCategory_index.this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(
                    R.layout.point_category_groupview, null);
        }
        TextView name = (TextView) convertView
                .findViewById(R.id.categoryname);
        // name.setText(EduInterface.categoryNameArrayList.get(groupPosition));
        name.setText(RemoveDuplicateCat.get(groupPosition));
        name.setTypeface(font_regular);

        convertView.setTag(groupPosition);
        convertView.setOnClickListener(new OnClickListener() {

            @Override public void onClick(View v) { 
                Log.d("TAG", "POSITION CLICKED: "+((Integer) v.getTag()).intValue()); 

                ...
            }
        });

        return convertView;
    }

[编辑2]

我一直在搜索,发现 ExpandableList 有 onGroupClickListener。在您的班级(不在适配器中)尝试这样做:

    pointcategoryExpandableList.setOnGroupClickListener(new OnGroupClickListener(){
            @Override

            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                               Log.d("TAG", "POSITION: "+groupPosition);
                if( parent.isGroupExpanded( groupPosition ) ){
                    parent.collapseGroup( groupPosition );
                }else{
                    parent.expandGroup( groupPosition );
                }
            }

        });

在 pointcategoryExpandableList.setAdapter(new MyExpandableAdapter()); 之后使用它。并从 getGroupView 中删除 setOnClickListener。

于 2013-06-13T11:25:50.943 回答