user1391869
user1391869

Reputation:

How to find clicked view in expandableview and display more then 1 child in android

I have for example. two parent items, where first have one child and second have 3 child. How to display it. And if I have have second parent view then i did not find this view click on log. I want to know this view was click on log.

I have same name multiple times, so display same name once and child make that same name.

like this

Asia -> Japan

Europe-> Spain
Europe->England

Now i want to display

Asia->Japan
Europe-> spain
       ->England

I have not much more idea behind expandable view in android.

Here is the Code on 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());
                }

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

Upvotes: 0

Views: 742

Answers (1)

PX Developer
PX Developer

Reputation: 8145

I think the problem is how you fill your arrays. You are doing:

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

ExpandableList uses the following function to calculate how many childs it has to create (using getView):

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

Since you are always doing new String[1], categoryName[groupPosition].length always returns 1 and so it always shows just 1 child.

If you want a parent to have 3 childs you should do new String[3] (and, of course, fill this positions with the data you want to be displayed).

I can't give you the exact code since I don't know exactly how you store the data, but I think you should do something like this:

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

[EDIT]

Oh, I forgot to answer your second question, the parent clicks. To know if a parent has been clicked you need to use onClickListener in getGroupView, as the code you have commented, but you have some errors. If you want the position of the group clicked you need to store it somewhere, for example, in the View's tag:

    @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]

I've been searching and I found that ExpandableList has onGroupClickListener. In your class (NOT in the adapter) try to do this:

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

        });

Use it after pointcategoryExpandableList.setAdapter(new MyExpandableAdapter());. And remove the setOnClickListener from getGroupView.

Upvotes: 1

Related Questions