可展开的ListView子项未显示

时间:2012-09-06 00:41:05

标签: android android-layout

就像他们根本不存在一样。它编译,但每组中没有任何可见或可选择。这是一个截图:

enter image description here

这是我的Activity类。孩子们充气的xml几乎没有多少。

package com.anthonyce.mcathomie;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PlayoptionsActivity extends Activity {
ExpandableListView Mtopics;
ExpandableAdapter MtopicsAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_playoptions);
    //set up adapter
    MtopicsAdapter = new ExpandableAdapter();
    Mtopics = (ExpandableListView) findViewById(R.id.mtopicsListView);
    Mtopics.setAdapter(MtopicsAdapter);
    //Mtopics.setGroupIndicator(null);
}

public class ExpandableAdapter extends BaseExpandableListAdapter {
        private String[] groups = { "People Names", "Dog Names", "Cat Names",
        "Fish Names" };
private String[][] children = { { "Arnold" }, { "Ace" }, { "Fluffy" },
        { "Goldy" } };

    public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
    }

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

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

        final class ExpandChildRow extends LinearLayout {
            public ExpandChildRow(Context context) {
                super(context);
                LayoutInflater childInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LinearLayout childView = (LinearLayout) childInflate.inflate(R.layout.mtopics_childrow, this, true);

                TextView childtxt = (TextView)childView.findViewById(R.id.mtopicchildtext);
                childtxt.setText(getChild(groupPosition, childPosition).toString());
            }
        }

        ExpandChildRow ChildRow = new ExpandChildRow(getBaseContext());
        return ChildRow;
    }

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

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

        final class ExpandGroupRow extends LinearLayout {
            public ExpandGroupRow(Context context) {
                super(context);
                LayoutInflater groupInflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                LinearLayout groupView = (LinearLayout) groupInflate.inflate(R.layout.mtopics_grouprow, this, true);

                TextView grouptxt = (TextView)groupView.findViewById(R.id.mtopicgrouptext);
                grouptxt.setText(getGroup(groupPosition).toString());
            }
        }

        ExpandGroupRow GroupRow = new ExpandGroupRow(getBaseContext());
        return GroupRow;

    }

    public boolean hasStableIds() {
        return true;
    }

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

}
}

2 个答案:

答案 0 :(得分:1)

我认为问题在于:class ExpandGroupRow extends LinearLayout 您正在尝试将此类用作UI元素,但您没有在构造函数中为this设置布局参数。 使用由inflater而不是此类返回的LinearLayout可能更容易。类似的东西:

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

            LayoutInflater groupInflate = (LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout groupView = (LinearLayout) groupInflate.inflate(R.layout.mtopics_grouprow, parent, false);

            TextView grouptxt = (TextView)groupView.findViewById(R.id.mtopicgrouptext);
            grouptxt.setText(getGroup(groupPosition).toString());
            return groupView;

}

答案 1 :(得分:0)

哦,我真是个老头。它起作用的原因是因为复选框在组行的任何位置与组分隔符或类似的东西冲突。也许组分隔符实际上是一个复选框本身。我只是删除了复选框,一切都恢复了。我想我的布局xml文件需要修改。