如何在expandableview中找到点击的视图,并在android中显示多于1个孩子

时间:2013-06-13 10:28:57

标签: android expandablelistview expandablelistadapter

我有例如。两个父项,其中第一个有一个孩子,第二个有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;
        }
    }

1 个答案:

答案 0 :(得分: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;
    }

由于你总是在做新的String [1],categoryName [groupPosition] .length总是返回1,所以它总是只显示1个孩子。

如果你想让父母有3个孩子,你应该做新的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,作为您已注释的代码,但是您有一些错误。如果您希望单击该组的位置,则需要将其存储在某个位置,例如,在View的标记中:

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

[EDIT2]

我一直在搜索,我发现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。