0

我尝试使用自己的 ExpandableListAdapter 构建 ExpandableListView 但 ExpandableListView 在我的活动中不可见:-(

您可以在下面找到我的源代码。

谢谢你的帮助 :-)

问候,康吉

这里是来自活动的代码:

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.group_main);

    onCreateDBAndDBTabled();

    mAddGroupButton = (Button) findViewById(R.id.Button_Add_Group);
    mAddGroupButton.setOnClickListener(this);

    mExpandableList = (ExpandableListView) findViewById(R.id.ExpandableListView01);

    ArrayList<ExpandableListItem> valueTree = new ArrayList<ExpandableListItem>();

    ExpandableListItem gp1 = new ExpandableListItem();
    gp1.putProperty("name", "e");

    ExpandableListItem gp11 = new ExpandableListItem();
    gp11.putProperty("name", "l");

    gp1.addChild(gp11);

    ExpandableListItem gp2 = new ExpandableListItem();
    gp2.putProperty("name", "A");

    ExpandableListItem gp22 = new ExpandableListItem();
    gp22.putProperty("name", "B");  
    ExpandableListItem gp23 = new ExpandableListItem();
    gp23.putProperty("name", "A1");
    ExpandableListItem gp24 = new ExpandableListItem();
    gp24.putProperty("name", "A3");
    ExpandableListItem gp25 = new ExpandableListItem();
    gp25.putProperty("name", "A4");

    gp2.addChild(gp22);
    gp2.addChild(gp23);
    gp2.addChild(gp24);
    gp2.addChild(gp25);

    valueTree.add(gp1);
    valueTree.add(gp2);

    Log.d("onCreate", "hasChild " + gp1.hasChilds());
    Log.d("onCreate", "hasChild " + gp2.hasChilds());


    MyColoredExpandableListAdapter adapter = new MyColoredExpandableListAdapter(this, valueTree);

    mExpandableList.setAdapter(adapter);



}

代码 MyColoredExpandableListAdapter:

private class MyColoredExpandableListAdapter extends MyExpandableListAdapter{



    public MyColoredExpandableListAdapter(Context context,

            ArrayList<ExpandableListItem> valueTree) {
        super(context, valueTree);

        Log.d("MyColoredExpandableListAdapter", "Group Count" + this.getGroupCount());

        for(int i = 0; i < this.getGroupCount(); i++)
            Log.d("MyColoredExpandableListAdapter", "Child Count" + this.getChildrenCount(i));

        // TODO Auto-generated constructor stub
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        Log.d("getChildView", "GO INTO");

        if(convertView == null){
            LayoutInflater inflater = LayoutInflater.from(mContext);
            convertView = inflater.inflate(R.layout.group_exp_childs, null);
        }
            TextView text = (TextView) convertView.findViewById(R.id.TextView_Group_EXP_Childs);

            text.setText(">>" + this.mValueTree.get(groupPosition).getChild(childPosition).getProperty("name"));


        return convertView;
    }

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

            Log.d("getGroupView", "GO INTO");

            if(convertView == null){
                LayoutInflater inflater = LayoutInflater.from(mContext);
                convertView = inflater.inflate(R.layout.group_exp_childs, null);
            }

            TextView text = (TextView) convertView.findViewById(R.id.TextView_Group_EXP_Childs);

            text.setText(">" + this.mValueTree.get(groupPosition).getProperty("name"));


        return convertView;
    }

}

代码 MyExpandableListAdapter:

public abstract class MyExpandableListAdapter extends BaseExpandableListAdapter {

protected Context mContext;
protected ArrayList<ExpandableListItem> mValueTree;

public MyExpandableListAdapter(Context context, 

        ArrayList<ExpandableListItem> valueTree)
{
    mContext = context;
    mValueTree = valueTree;

}

public Object getChild(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub

    if(mValueTree.get(groupPosition).hasChilds())
        return mValueTree.get(groupPosition).getChild(childPosition);

    return null;
}

public long getChildId(int groupPosition, int childPosition) {
    // TODO Auto-generated method stub
    return childPosition; // WE NEED NO SPECIAL ID 
}



public int getChildrenCount(int groupPosition) {
    // TODO Auto-generated method stub

    if(mValueTree.get(groupPosition).hasChilds())
        return mValueTree.get(groupPosition).sizeOfChilds();

    return 0;
}

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

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

public long getGroupId(int groupPosition) {
    // TODO Auto-generated method stub
    return groupPosition; // WE NEED NO SPECIAL ID 
}

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

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

}

代码扩展列表项:

public class ExpandableListItem {

private HashMap<String,String> properties = new HashMap<String,String>();

public void clearProperties() {
    properties.clear();
}
public boolean containsPropertyKey(String key) {
    return properties.containsKey(key);
}
public boolean containsPropertyValue(String value) {
    return properties.containsValue(value);
}
public String getProperty(String key) {
    return properties.get(key);
}
public boolean hasProperties() {
    return !properties.isEmpty();
}
public String putProperty(String key, String value) {
    return properties.put(key, value);
}
public String removeProperty(String key) {
    return properties.remove(key);
}
public int sizeOfProperties() {
    return properties.size();
}

public String[] propertiesKeys()
{
    int count = 0;
    String[] result = new String[sizeOfProperties()];

    for(String key : this.properties.keySet())
        result[count++] = key;

    return result;
}

private ArrayList<ExpandableListItem> childs = new ArrayList<ExpandableListItem>();

public boolean addChild(ExpandableListItem object) {
    return childs.add(object);
}
public void clearChilds() {
    childs.clear();
}
public ExpandableListItem getChild(int index) {
    return childs.get(index);
}
public boolean hasChilds() {
    return !childs.isEmpty();
}
public ExpandableListItem removeChild(int index) {
    return childs.remove(index);
}
public int sizeOfChilds() {
    return childs.size();
}

}

4

1 回答 1

0

布局代码(无标签):

R.id.TextView_Group_EXP_Childs :

 ?xml version="1.0" encoding="utf-8"?
LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"

TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="20dp" android:id="@+id/TextView_Group_EXP_Childs" android:text="&gt;&gt;"/TextView
/LinearLayout
于 2010-09-30T18:43:01.120 回答