VenushkaT
VenushkaT

Reputation: 1150

Expandablelistview in fragment- Expandable List Not display

im trying to implement Expandable list View in Fragements.i have tested all the values set to toast and it work fine.But My expandable list view not Dispaly.I didnt get any Error.please find below the code i used.

   package com.test.expandablelistView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tesfragement.R;
import com.example.tesfragement.R.layout;
import android.os.Bundle;
import android.app.Fragment;
import android.database.DataSetObserver;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.Toast;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */

public class ExpandableListFragment extends Fragment {

    View v;
     ExpandableListAdapter mAdapter;
    List<String> _listDataHeader;
    HashMap<String, List<String>> _listDataChild;
    private Parent parent;
    private Child child;
    ExpandableListView lv;




    public ExpandableListFragment() {
        // Required empty public constructor

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v= inflater.inflate(R.layout.expandable_fragements,
                container, false);


        return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onActivityCreated(savedInstanceState);
        parent=new Parent();
        child=new Child();
         ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.expandableListView1);

         //here setting all the values to Parent and child classes
         setDataValues();
         prepareListData();//here get the values and set this values to adoptor and set it visible


         mAdapter=new ExpandableListAdapter() {

            @Override
            public void unregisterDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello", Toast.LENGTH_LONG).show();

            }

            @Override
            public void registerDataSetObserver(DataSetObserver observer) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello1", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onGroupExpanded(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello2", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onGroupCollapsed(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello3", Toast.LENGTH_LONG).show();
            }

            @Override
            public boolean isEmpty() {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello4", Toast.LENGTH_LONG).show();
                return false;
            }

            @Override
            public boolean isChildSelectable(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello5", Toast.LENGTH_LONG).show();
                return false;
            }

            @Override
            public boolean hasStableIds() {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello6", Toast.LENGTH_LONG).show();
                return false;
            }

            @Override
            public View getGroupView(int groupPosition, boolean isExpanded,
                    View convertView, ViewGroup parent) {
                Toast.makeText(getActivity(),"hello7", Toast.LENGTH_LONG).show();
                // TODO Auto-generated method stub
                return v;
            }

            @Override
            public long getGroupId(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello8", Toast.LENGTH_LONG).show();
                return 0;
            }

            @Override
            public int getGroupCount() {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello9", Toast.LENGTH_LONG).show();
                return 0;
            }

            @Override
            public Object getGroup(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello10", Toast.LENGTH_LONG).show();
                return null;
            }

            @Override
            public long getCombinedGroupId(long groupId) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello11", Toast.LENGTH_LONG).show();
                return 0;
            }

            @Override
            public long getCombinedChildId(long groupId, long childId) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello12", Toast.LENGTH_LONG).show();
                return 0;
            }

            @Override
            public int getChildrenCount(int groupPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello13", Toast.LENGTH_LONG).show();
                return 0;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition,
                    boolean isLastChild, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello14", Toast.LENGTH_LONG).show();
                return v;
            }

            @Override
            public long getChildId(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello15", Toast.LENGTH_LONG).show();
                return 0;
            }

            @Override
            public Object getChild(int groupPosition, int childPosition) {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello16", Toast.LENGTH_LONG).show();
                return null;
            }

            @Override
            public boolean areAllItemsEnabled() {
                // TODO Auto-generated method stub
                Toast.makeText(getActivity(),"hello17", Toast.LENGTH_LONG).show();
                return false;
            }
        };

           // mAdapter = new ExpandableListAdapter(this, _listDataHeader, _listDataChild);

            // setting list adapter
            lv.setAdapter(mAdapter);



    }


    public void prepareListData()
    {
        // testing purpose
        _listDataHeader = new ArrayList<String>();
        _listDataChild = new HashMap<String, List<String>>();


        // declare the references
        //add the parent values to List
        _listDataHeader.add(parent.getCardName());
        _listDataHeader.add(String.valueOf(parent.getMinimum_salary()));
        _listDataHeader.add(String.valueOf(parent.getInterest_rate()));


        //set Child views to parent
        List<String> cardDetails=new ArrayList<String>();
        cardDetails.add("");

        List<String> mininum_sal_details=new ArrayList<String>();
        mininum_sal_details.add(child.GetMinimumSalDetails());

        List<String> interest_details=new ArrayList<String>();
        interest_details.add(child.get_interest_rate_details());

        //set to adoptor

        _listDataChild.put(_listDataHeader.get(0),  cardDetails);
        _listDataChild.put(_listDataHeader.get(1),mininum_sal_details);

        //

         for(int i = 0; i < _listDataHeader.size(); i++) //cars name of arraylist
            {
               String value=_listDataHeader.get(i);  
               Toast toast = Toast.makeText(getActivity(),value, Toast.LENGTH_LONG);
               toast.setGravity(Gravity.CENTER, 0, 0);
               toast.show();

            }



    }

    public void setDataValues()
    {
        //set Parent values
        parent.setCardName("Platinum credit Card");
        parent.setMinimum_salary(15000.00);
        parent.setInterest_Rate(1.2);

        //set Child values
        child.set_card_details("You require minimum salary of 1500 per month");
        child.set_interest_rate_details("interest rate is 2.0%");


    }


}

Upvotes: 1

Views: 11758

Answers (4)

Bhavesh Jethani
Bhavesh Jethani

Reputation: 3875

Note: in my case, I could not see the text of expandable listview inside fragment ... otherthan all functionality worked fine like clicking and expand.

if you have same situation then you need give text color of each textview in your xml file. please try at once. because . i have used expandable listview with fragment its work.

I just updated the text color of each textview.

And here is code.

class ExpandableAdapters extends BaseExpandableListAdapter
    {

        LayoutInflater mInflater;
        DisplayImageOptions options;
        ArrayList<Taxons> localArr;
        AnimateFirstDisplayListener animateFirstListener;

        public ExpandableAdapters(ArrayList<Taxons> arrTaxon)
        {

            localArr = arrTaxon;
            mInflater = LayoutInflater.from(view.getContext());

            options = new DisplayImageOptions.Builder().bitmapConfig(Bitmap.Config.RGB_565).imageScaleType(ImageScaleType.EXACTLY).cacheInMemory(true)
                    .cacheOnDisc(true).resetViewBeforeLoading(true).build();

        }

        @Override
        public Object getChild(int groupPosition, int childPosition)
        {

            if (localArr.get(groupPosition - 1).arr_Taxons != null && groupPosition > 0)
                return localArr.get(groupPosition - 1).arr_Taxons.get(childPosition);
            else
                return null;
        }

        @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 infalInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.lyt_subcategory, null);

            }

            TextView tvSubcatname = (TextView) convertView.findViewById(R.id.subcat_tvName);
            tvSubcatname.setText(localArr.get(groupPosition - 1).arr_Taxons.get(childPosition).name);
            return convertView;
        }

        @Override
        public int getChildrenCount(int groupPosition)
        {

            if (localArr.get(groupPosition - 1).arr_Taxons != null && groupPosition > 0)
            {
                return localArr.get(groupPosition - 1).arr_Taxons.size();
            }
            else
                return 0;

        }

        @Override
        public Object getGroup(int groupPosition)
        {

            return localArr.get(groupPosition - 1);
        }

        @Override
        public int getGroupCount()
        {

            return localArr.size() + 1;
        }

        @Override
        public long getGroupId(int groupPosition)
        {

            return groupPosition;
        }

        @Override
        public int getGroupTypeCount()
        {

            return 2;
        }

        @Override
        public int getGroupType(int groupPosition)
        {

            return (groupPosition == 0) ? 1 : 0;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
        {

            final ViewHolder holder;
            int theType = getGroupType(groupPosition);
            if (convertView == null)
            {
                holder = new ViewHolder();
                if (theType == 0)
                {
                    // inflate the search row
                    convertView = mInflater.inflate(R.layout.lyt_category, null);
                    holder.ivCategory = (ImageView) convertView.findViewById(R.id.home_ivCategory);
                    holder.ivCategory.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, screenHeight / 2));
                    holder.ivCategory.setScaleType(ScaleType.CENTER_CROP);
                    holder.tvCategory = (TextView) convertView.findViewById(R.id.home_tvCategoryName);
                    holder.progress = (ProgressBar) convertView.findViewById(R.id.home_progressbar1);
                    holder.ivCategory.setTag(holder.progress);
                    holder.tvCategory.setPadding((int) (screenWidth * 0.07), 0, 0, 0);

                }
                else if (theType == 1)
                {
                    // except zero index it will execute this code.
                    convertView = mInflater.inflate(R.layout.lyt_search_home, null);
                    holder.rlSearchmain = (RelativeLayout) convertView.findViewById(R.id.home_rlsearch);
                    holder.rlSearchmain.setLayoutParams(new AbsListView.LayoutParams(screenWidth, (int) (screenHeight * 0.07)));
                    holder.ivSearch = (ImageButton) convertView.findViewById(R.id.home_Btn_search);
                    holder.etSearch = (EditText) convertView.findViewById(R.id.home_edt_search);

                }
                convertView.setTag(holder);
            }
            else
                holder = (ViewHolder) convertView.getTag();

            //set value here.

            return convertView;
        }

        @Override
        public boolean hasStableIds()
        {

            return false;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition)
        {

            return true;
        }

        private class ViewHolder
        {

            ImageView ivCategory;
            TextView tvCategory;
            ProgressBar progress;
            EditText etSearch;
            ImageButton ivSearch;
            RelativeLayout rlSearchmain;
        }
}

Please ignore extra code...

Upvotes: 1

Rehan K
Rehan K

Reputation: 193

package com.test.expandablelistView;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.tesfragement.R;
import com.example.tesfragement.R.layout;
import android.os.Bundle;
import android.app.Fragment;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Typeface;
import android.support.v4.app.FragmentActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;
import android.widget.Toast;

/**
 * A simple {@link android.support.v4.app.Fragment} subclass.
 * 
 */

public class ExpandableListFragment extends Fragment {

    View v;
     ExpandableListAdapter mAdapter;
    List<String> _listDataHeader;
    HashMap<String, List<String>> _listDataChild;
    private Parent parent;
    private Child child;
    ExpandableListView lv;
    Context con;




    public ExpandableListFragment() {
        // Required empty public constructor

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        v= inflater.inflate(R.layout.expandable_fragements,
                container, false);


        return v;
    }


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub

        super.onActivityCreated(savedInstanceState);
        parent=new Parent();
        child=new Child();
         ExpandableListView lv = (ExpandableListView) v.findViewById(R.id.expandableListView1);

         //here setting all the values to Parent and child classes
         setDataValues();
         prepareListData();//here get the values and set this values to adoptor and set it visible
         con=getActivity();

         mAdapter=new ExpandabelListAdoptor(con,_listDataHeader, _listDataChild) ; //here i didnt set list values to this adoptor



           // mAdapter = new ExpandableListAdapter(this, _listDataHeader, _listDataChild);

            // setting list adapter
            lv.setAdapter(mAdapter);



    }





    public void prepareListData()
    {
        // testing purpose
        _listDataHeader = new ArrayList<String>();
        _listDataChild = new HashMap<String, List<String>>();


        // declare the references
        //add the parent values to List
        _listDataHeader.add(parent.getCardName());
        _listDataHeader.add(String.valueOf(parent.getMinimum_salary()));
        _listDataHeader.add(String.valueOf(parent.getInterest_rate()));


        //set Child views to parent
        List<String> cardDetails=new ArrayList<String>();
        cardDetails.add("");

        List<String> mininum_sal_details=new ArrayList<String>();
        mininum_sal_details.add(child.GetMinimumSalDetails());

        List<String> interest_details=new ArrayList<String>();
        interest_details.add(child.get_interest_rate_details());

        //set to adoptor

        _listDataChild.put(_listDataHeader.get(0),  cardDetails);
        _listDataChild.put(_listDataHeader.get(1),mininum_sal_details);

        //

         for(int i = 0; i < _listDataHeader.size(); i++) //cars name of arraylist
            {
               String value=_listDataHeader.get(i);  
               Toast toast = Toast.makeText(getActivity(),value, Toast.LENGTH_LONG);
               toast.setGravity(Gravity.CENTER, 0, 0);
               toast.show();

            }



    }

    public void setDataValues()
    {
        //set Parent values
        parent.setCardName("Platinum credit Card");
        parent.setMinimum_salary(15000.00);
        parent.setInterest_Rate(1.2);

        //set Child values
        child.set_card_details("You require minimum salary of 1500 per month");
        child.set_interest_rate_details("interest rate is 2.0%");


    }





}
class ExpandabelListAdoptor extends BaseExpandableListAdapter
{

    private Context _context;
    private List<String> _listDataHeader;
    private HashMap<String, List<String>> _listDataChild;





    ExpandabelListAdoptor(Context con,List<String> listDataHeader ,HashMap<String, List<String>>  listDataChild )
    {
        this._context=con;

        this._listDataChild=listDataChild;
        this._listDataHeader=listDataHeader;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
         return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                    .get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
         return childPosition;
    }

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

       final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this._context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.list_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.lblListItem);

        txtListChild.setText(childText);
        return convertView;

    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return this._listDataChild.get(this._listDataHeader.get(groupPosition))
                .size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
         return this._listDataHeader.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return this._listDataHeader.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
         return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {

            String headerTitle = (String) getGroup(groupPosition);
            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this._context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.list_group, null);
            }

            TextView lblListHeader = (TextView) convertView
                    .findViewById(R.id.lblListHeader);
            lblListHeader.setTypeface(null, Typeface.BOLD);
            lblListHeader.setText(headerTitle);

            return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return true;
    }

}

Upvotes: 0

mostafa_zakaria
mostafa_zakaria

Reputation: 116

Because you didn't return the actual childs or groups count also you shouldn't return null on getGroup or getChild methods. try to follow this example

Upvotes: 1

evaristokbza
evaristokbza

Reputation: 872

For a ExpandableListView to be visible, it has to have set an adapter. The adapter have to return the view count in getGroupCount() and getChildCount(). If a list has no elements, it won't be visible. Also it has to return the correct View in getGroupView() and getChildView().

An example of an adapter could be:

public class ItemAdapter extends BaseExpandableListAdapter {

    private LayoutInflater inf;
    private List<TestParent> groups;
    private List<TestChild> childs;

    public ItemAdapter(Context context, int resource, int textViewResourceId, List<TestParent> objects, List<TestChild>childs) {
        super();
        this.inf = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.groups = objects;
        this.childs = childs;
    }

    @Override
    public TestChild getChild(int groupPosition, int childPosition) {
        return getGroup(groupPosition).getChild(childPosition);
    }

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

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        View v = convertView;
        ChildViewHolder holder;

        if (v == null) {
            holder = new ChildViewHolder();
            v = inf.inflate(R.layout.row_child, null);
            v.setTag(holder);
        } else {
            holder = (ChildViewHolder) v.getTag();
        }

        return v;
    }

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

    @Override
    public TestParent getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

        View v = convertView;
        ParentViewHolder holder;

        if (v == null) {
            holder = new ParentViewHolder();
            v = inf.inflate(R.layout.row_parent, null);
            v.setTag(holder);
        } else {
            holder = (ParentViewHolder) v.getTag();
        }

        return v;
    }

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

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

}

Upvotes: 0

Related Questions