2 xiaoliuzi 0 0 xiaoliuzi_0_0 于 2016.03.29 17:44 提问

新手不会使用ExpandListView。。指点下

实现Tab fragment切换界面时,其中一个fragment内部用ExpandListView实现二级下拉列表,但是一直没有内容显示。。。在网上找资料修改的但有点乱,,已经不知道是哪里的问题了。。。

 package com.example.hospital;

import java.util.List;

import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;

import android.widget.TextView;

public class OrderExpandableListviewAdapter extends BaseExpandableListAdapter {
    private Context context;
    private LayoutInflater mLayoutInflater;
    private List<KeShi_1> groups;

    public OrderExpandableListviewAdapter() {
        super();
        // TODO Auto-generated constructor stub
    }


    public OrderExpandableListviewAdapter(Context context, List<KeShi_1> groups) {
        super();
        this.context = context;
        this.groups = groups;
        mLayoutInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }





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

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return groups.get(groupPosition).getChild().size();
    }

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

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return groups.get(groupPosition).getChild().get(childPosition);
    }

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

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

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        KeShi_1 k1=new KeShi_1();
        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = mLayoutInflater.inflate(R.layout.group_list, null);
            viewHolder = new ViewHolder();
            viewHolder.groupImageView = (ImageView) view
                    .findViewById(R.id.group_list_image);
            viewHolder.groupTextView = (TextView) view
                    .findViewById(R.id.group_list_text);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.groupImageView.setTag(k1.getImage());
        viewHolder.groupTextView.setTag(k1.getName());
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        KeShi_2 k2=new KeShi_2();
        View view;
        ViewHolder viewHolder;

        if (convertView == null) {
            view = mLayoutInflater.inflate(R.layout.child_list, null);
            viewHolder = new ViewHolder();
            viewHolder.childImageView = (ImageView) view
                    .findViewById(R.id.child_list_image);
            viewHolder.childTextView = (TextView) view
                    .findViewById(R.id.child_list_text);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (ViewHolder) view.getTag();
        }
        viewHolder.childImageView.setTag(k2.getImage());
        viewHolder.childTextView.setTag(k2.getName());
        return view;
    }

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

    class ViewHolder {
        ImageView groupImageView;
        ImageView childImageView;
        TextView groupTextView;
        TextView childTextView;
    }
}
 package com.example.hospital;

import java.util.ArrayList;
import java.util.FormattableFlags;
import java.util.HashMap;
import java.util.List;

import com.example.hospital.R.id;

import android.app.Activity;
import android.app.Fragment;
import android.content.Loader.ForceLoadContentObserver;
import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.ExpandableListView;

public class OrderFragment extends Fragment {
    private List<KeShi_1> groupk1;
    ExpandableListView mExpandableListView;
    OrderExpandableListviewAdapter mExpandableListviewAdapter;

    @Override
    @Deprecated
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        init();
        mExpandableListviewAdapter = new OrderExpandableListviewAdapter(
                activity, groupk1);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.tab04, container, false);
        mExpandableListView = (ExpandableListView) view
                .findViewById(R.id.expandable_list);
        init();
        mExpandableListView.setAdapter(mExpandableListviewAdapter);

        return view;
    }

    private void init() {
        groupk1 = new ArrayList<KeShi_1>();
        List<KeShi_2> groupk2 = new ArrayList<KeShi_2>();
        KeShi_2 k2=new KeShi_2("111",0,"很好");
        groupk2.add(k2);
        k2=new KeShi_2("112",0,"优秀");
        groupk2.add(k2);

        KeShi_1 k1=new KeShi_1("科室一",0,groupk2);

    }

}

3个回答

luochoudan
luochoudan   Ds   Rxr 2016.03.29 18:40

感觉数据出问题了,二级下拉的数据源自然也是二级形式。

chen00yin
chen00yin   2016.03.29 20:49

你有两个错误:
1:convertView没有赋值的地方,导致convertView复用未生效;
2:你的k1、k2都是new来的,根本没有赋初值,怎么可能会显示出来呢,可以这样:
k1=groups.get(groupPosition);
k2=groups.get(groupPosition).get(childPosition);

CSDNXIAON
CSDNXIAON   2016.03.30 11:31

ExpandListView的使用
Expandlistview的使用
当我父亲也会使用Android手机时想到的
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!