片段中的Expandablelistview - 可扩展列表不显示

时间:2014-02-27 11:42:41

标签: android expandablelistview

我试图在Fragements.i中实现可扩展列表视图已经测试了设置为toast的所有值并且它工作正常。但是我的可扩展列表视图不是Dispaly.I没有在我使用的代码下面找到任何Error.please。< / p>

   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%");


    }


}

4 个答案:

答案 0 :(得分:1)

注意: 在我的情况下,我无法在片段内看到可扩展列表视图的文本 ... ...所有功能都可以正常工作,如点击和展开。

如果情况相同,则需要在xml文件中提供 textview 文字颜色。请立即尝试。因为。我使用了可扩展的listview与片段的工作。

我刚刚更新了每个textview的文字颜色。

这是代码。

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

请忽略额外代码......

答案 1 :(得分:1)

因为您没有返回实际的子项或组计数,所以您不应该在getGroup或getChild方法上返回null。尝试遵循此example

答案 2 :(得分:0)

要使ExpandableListView可见,它必须设置适配器。适配器必须返回getGroupCount()和getChildCount()中的视图计数。如果列表没有元素,则不会显示。它还必须在getGroupView()和getChildView()中返回正确的视图。

适配器的示例可以是:

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

}

答案 3 :(得分:0)

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

}