我正在尝试在Fragements中实现可扩展列表视图。我已经测试了所有设置为toast的值,并且工作正常。但是我的可扩展列表视图不是Dispaly。我没有得到任何Error。

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


    }


}

最佳答案

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

}

08-17 18:25