簡體   English   中英

在模擬器中運行時,Android ExpandableLists不顯示。

[英]Android ExpandableLists not showing when run in emulator.

當我在模擬器中運行以下命令時,它崩潰了。 所以我輸入了try和catch語句,現在我得到的只是main.xml上的空白屏幕

我正在嘗試獲取一個簡單的expandablelist,以與數組中的項目一起顯示以填充它。

我相信問題在於explistsActivity.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>

任何幫助將不勝感激!

謝謝!

嘗試這個,

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

感謝您的回答,我想避免使用Hashmaps和僅使用數組,但是我通過刪除該行解決了它。

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

在ExpListActivity類的頂部。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM