Android ExpandableLists在模拟器中运行时未显示。

时间:2012-01-31 17:41:14

标签: android expandablelistview

当我在模拟器中运行以下内容时,它会崩溃。所以我输入了try和catch语句,现在我得到的是main.xml上的一个空白页面

我正在尝试使用一个简单的expandablelist来显示数组中的项目以填充它。

我相信问题在于exploistsActivity.java中的“setContentView(listlevel1)”行

这是我的代码:

ExpListsActivity.java

package exp.Lists.Project;

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ExpandableListView;


public class ExpListsActivity extends ExpandableListActivity implements OnClickListener {
    /** Called when the activity is first created. */
    ExpandableListView listlevel1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        //try{
        super.onCreate(savedInstanceState);

        listlevel1 = new ExpandableListView(this);
        listlevel1 = (ExpandableListView) findViewById(R.id.listlevel1);
       //the next line is the listener for the child list- debug. 
        // listlevel1.setOnChildClickListener(onChildClickListener)
    listlevel1.setGroupIndicator(null);
    listlevel1.setChildIndicator(null);

    // NOW WE SET UP DATA IN ARRAYS
    String[] titles = {"Days","Months"};
    String[] Days = {"Mon","Tue","Wed","Thurs","Fri","Sat","Sun"};
    String[] Months = {"Jan","Feb","Mar","Apr","May","Aug","Sep","Oct","Nov","Dec"};

    String[][] contents = {Days, Months};
    ExpandableAdapter adapter = new ExpandableAdapter(this, titles, contents);
    listlevel1.setAdapter(adapter);
    setContentView(listlevel1);
    }//catch(Exception e){
        //System.out.println("Errrr +++ " + e.getMessage());
//  }


    //}
    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

    }
}

还有ExpandableAdapter.java

package exp.Lists.Project;


import android.content.Context;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;


public class ExpandableAdapter extends BaseExpandableListAdapter {
    private Context mContext;
    private String [][] mContents;
    private String [] mTitles;

    public ExpandableAdapter
    (Context context, String[] titles, String[][] contents){
        super();

        if(titles.length != contents.length) {
            throw new IllegalArgumentException
            ("Titles and Contents must be the same size.");
        }

        mContext = context;
        mContents = contents;
        mTitles = titles;
    }


    @Override
    public String getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return mContents[groupPosition][childPosition];
    }
    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView childlevel1 = (TextView)convertView;
        if(childlevel1==null){
            childlevel1 = new TextView(mContext);
        }
        childlevel1.setText(mContents[groupPosition][childPosition]);
        return childlevel1;
    }
    @Override
    public int getChildrenCount(int groupPosition) {
        return mContents[groupPosition].length;

    }
    @Override
    public String[] getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return mContents[groupPosition];
    }
    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return mContents.length;
    }
    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return 0;
    }
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView row_name_level1 = (TextView)convertView;
        if (row_name_level1==null) {
            row_name_level1 = new TextView(mContext);
        }
        row_name_level1.setTypeface(Typeface.DEFAULT_BOLD);
        row_name_level1.setText(mTitles[groupPosition]);
        return row_name_level1;

    }

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

}

main.xml:

<?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"
    android:orientation="vertical" >

    <ExpandableListView
        android:id="@+id/listlevel1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </ExpandableListView>

</LinearLayout>

child_row_level1.xml(子列表)

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

    <TextView android:id="@+id/childlevel1"
         android:paddingLeft="50dp"
         android:focusable="false"
         android:textSize="14dp"
         android:textStyle="normal"
         android:layout_width="150dp"
         android:layout_height="wrap_content"/>

</LinearLayout>

group_row_level1.xml(父列表)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView android:id="@+id/row_name_level1"
         android:paddingLeft="50dp"
         android:textSize="20dp"
         android:textStyle="normal"
         android:layout_width="320dp"
         android:layout_height="wrap_content"/>

</LinearLayout>

非常感谢任何帮助!

谢谢!

2 个答案:

答案 0 :(得分:1)

试试这个,

<强> main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="ExpandableListViewSample" />

    <ExpandableListView
        android:id="@+id/ExpandableListView"
        android:layout_width="fill_parent"
        android:layout_height="0dip"
        android:layout_weight="1" >
    </ExpandableListView>

</LinearLayout>

<强> ExpList.java

package com.example.android.photoalbum;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.SimpleExpandableListAdapter;

public class ExpList extends Activity {
    private static final String KEY1 = "GROUP";
    private static final String KEY2 = "CHILD";

    private String[] GROUPS = { "Group1", "Group2", "Group3" };
    private String[][][] CHILDREN = {
            { { "Child11", "Text11" } },
            { { "Child21", "Text21" }, { "Child22", "Text22" } },
            { { "Child31", "Text31" }, { "Child32", "Text32" },
                    { "Child33", "Text33" } }, };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        List<Map<String, String>> groupData = new ArrayList<Map<String, String>>();
        List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();

        for (int i = 0; i < GROUPS.length; i++) {

            Map<String, String> curGroupMap = new HashMap<String, String>();
            groupData.add(curGroupMap);
            curGroupMap.put(KEY1, GROUPS[i]);
            curGroupMap.put(KEY2, "");

            List<Map<String, String>> children = new ArrayList<Map<String, String>>();
            if (CHILDREN.length > i) {
                for (int j = 0; j < CHILDREN[i].length; j++) {

                    Map<String, String> curChildMap = new HashMap<String, String>();
                    children.add(curChildMap);
                    curChildMap.put(KEY1, CHILDREN[i][j][0]);
                    curChildMap.put(KEY2, CHILDREN[i][j][1]);
                }
            }
            childData.add(children);
        }

        ExpandableListAdapter adapter = new SimpleExpandableListAdapter(this,
                groupData, android.R.layout.simple_expandable_list_item_1,
                new String[] { KEY1, KEY2 }, new int[] { android.R.id.text1,
                        android.R.id.text2 }, childData,
                android.R.layout.simple_expandable_list_item_2, new String[] {
                        KEY1, KEY2 }, new int[] { android.R.id.text1,
                        android.R.id.text2 });

        ExpandableListView listView = (ExpandableListView) findViewById(R.id.ExpandableListView);

        listView.setAdapter(adapter);

        listView.setOnGroupClickListener(new OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v,
                    int groupPosition, long id) {

                return false;
            }
        });

        listView.setOnChildClickListener(new OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {

                return false;
            }
        });
    }
}

答案 1 :(得分:0)

感谢您的回答,我想避免使用Hashmaps而只使用数组,但是我通过删除该行来解决它。

listlevel1 = (ExpandableListView) findViewById(R.id.listlevel1);

位于ExpListActivity类的顶部。