Akanksha Sawarkar
Akanksha Sawarkar

Reputation: 89

Child view is not showing for Expandable list view

Question: I've got and ExpandableListView which I assign to an extended BaseExpandableListAdapter. I have implemented the getChildView() method to setup and return a View for each child but it's not showing child layout,it's only showing parent layout:

Expandable List Adapter:

    public View getChildView(int groupPosition, final int childPosition,
        boolean isLastChild, View convertView, ViewGroup parent) {

    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this._context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.list_child_item, null);
    }   
    //In child layout  we are getting values from   model class i.e. Category Model and setting it to text view
     TextView txtViewBusinessName = (TextView) convertView.findViewById(R.id.textViewBusinessName);
     txtViewBusinessName.setText(categoryModel.getBusinessName().trim());
     Log.i(TAG,"(categoryModel.getBusinessName().trim()"+(categoryModel.getBusinessName().trim()));

     TextView txtViewPhoneNo = (TextView)
     convertView.findViewById(R.id.textViewMobileNo);
     txtViewPhoneNo.setText(categoryModel.getPhoneNumber().trim());

     TextView txtViewLandmark= (TextView)
     convertView.findViewById(R.id.textViewLandmark);
     txtViewLandmark.setText(categoryModel.getLandMark().trim());

    return convertView;
}

Inside MainActivity class I have written following code:

/**for getting child data*******/
//fetch data from Category table and store it in a  cursor of given `categoryId`

        Cursor cursorChild = databaseHelper.fetchChildren(2);
        //fetchChildren is a method written in database 
        //helper class for getting values for selected category;

        // loop cursor to get data
        if (cursorChild.moveToFirst()) {
            do {
                categoryModel = new CategoryModel();
                //get business name,phone no & landmark from cursor
                Log.i(TAG, "business name from cursor::"+cursorChild.getString(0));
                String businessName = cursorChild.getString(0);
                String phoneNo = cursorChild.getString(1);
                String landMark = cursorChild.getString(2);

                //get business name,phone no & landmark inro arraylist
                childArrayList=new ArrayList<String>();
                childArrayList.add(businessName);
                childArrayList.add(phoneNo);
                childArrayList.add(landMark);

                // set data to model
                categoryModel.setBusinessName(businessName);
                categoryModel.setPhoneNumber(phoneNo);
                categoryModel.setLandMark(landMark);


                //put data of arraylist into hashmap
                listDataChild=new   HashMap<String, ArrayList<String>>();
                listDataChild.put("category_id",childArrayList);


            //  childArraylist.add(businessName);
                //childArraylist.add(phoneNo);
            //  childArraylist.add(landMark);
                Log.i(TAG, "childHashMap"+listDataChild);

                //set data of array to hashmap                  

                //listDataChild=childArraylist;
            //  Log.i(TAG, "listDataChild::"+listDataChild);

            } while (cursorChild.moveToNext());

            cursorChild.close();  


        expandableListAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
        Log.i(TAG, "listDataHeader :" + listDataHeader);

        // set adapter to expandable list view
        expListView.setAdapter(expandableListAdapter);
    }

I have made changes in layouts also. Still it's not showing me child data. Only parent view is showing in layout.Please suggest me any solutions why it is not getting shown & also how to add data in child view at run time.

Upvotes: 0

Views: 2720

Answers (2)

sonaysevik
sonaysevik

Reputation: 31

Focusable attribute of your group layout and all clickable views in group layout must be false. If they are not, try setting them to false. Like:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusable="false">
</RelativeLayout>

Upvotes: 3

Lawrence Gimenez
Lawrence Gimenez

Reputation: 3235

For anyone in the near future that was stuck with this problem. I like you, followed every tutorial/examples from the internet about how to use this ExpandableListView, but encountered a problem with the child items not displaying.

The API I'm building on is API 22, the solution I did was I explicitly called expandGroup() method of the class ExpandableListView.

Something along the lines below

mExpandableTriggers.setAdapter(adapter);
mExpandableTriggers.expandGroup(0);

Upvotes: 0

Related Questions