<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView 
        android:layout_width="fill_parent" 
         android:layout_height="fill_parent" 
          android:id="@+id/group"
           android:paddingLeft="35px"
            android:paddingTop="10px"
            android:paddingRight="5px"
              android:paddingBottom="10px"
               android:text="No Data"
        />
   </LinearLayout>
<?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="horizontal" >
    
    <TextView 
        android:id="@+id/item1"
        android:layout_marginLeft="10dp"
        android:layout_width="50dp"
        android:layout_height="wrap_content"
        />
    
    <TextView 
        android:id="@+id/item2"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        />
    
        <TextView 
        android:id="@+id/item3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
    
    </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <ExpandableListView 
         android:id="@id/android:list" 
          android:layout_width="fill_parent"
           android:layout_height="fill_parent"
             android:drawSelectorOnTop="false"
        />
    <TextView 
         android:layout_width="fill_parent" 
          android:layout_height="fill_parent" 
             android:id="@id/android:empty"
              android:text="没有数据"
        />
   </LinearLayout> 
package com.android.test;import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import android.R.string;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.SimpleExpandableListAdapter;
import android.widget.TextView;public class ExpandableListView2Activity extends ExpandableListActivity {

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

//设置箭头的位置,转为右边
ExpandableListView listView = getExpandableListView();
 int width = getWindowManager().getDefaultDisplay().getWidth();
         listView.setIndicatorBounds(width-50, width-20);

// 创建一级条目
List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
// 将二级条目放在一个集合里,供显示时使用
List<List<Map<String, Object>>> childs = new ArrayList<List<Map<String, Object>>>();

// 创建两个一级条目标题
Map<String, String> group1 = new HashMap<String, String>();
group1.put("group", "各单位发电占比");
Map<String, String> group2 = new HashMap<String, String>();
group2.put("group", "发电进度");
groups.add(group1);
groups.add(group2);

// 创建一级条目下的的二级条目
List<Map<String, Object>> child1 = null;
// 同样是在一级条目目录下创建两个对应的二级条目目录
for (int i = 0; i < 3; i++) {
child1 = new ArrayList<Map<String, Object>>();
Map<String, Object> childdata1 = new HashMap<String, Object>();
childdata1.put("name", "九江电厂");
childdata1.put("this", "200");
childdata1.put("last", "300");
child1.add(childdata1);
childs.add(child1);
}

List<Map<String, Object>> child2 =null;
for(int i = 0; i< 3; i++){
child2 = new ArrayList<Map<String,Object>>();
Map<String, Object> childdata2 = new HashMap<String, Object>();
childdata2.put("name", String.valueOf(i+1));
childdata2.put("this", "100");
childdata2.put("last", "200");
child2.add(childdata2);
childs.add(child2);
} /**
 * 使用SimpleExpandableListAdapter显示ExpandableListView 参数1.上下文对象Context   
 * * 参数2.一级条目目录集合    * 参数3.一级条目对应的布局文件    *
 * 参数4.fromto,就是map中的key,指定要显示的对象    * 参数5.与参数4对应,指定要显示在groups中的id    *
 * 参数6.二级条目目录集合    * 参数7.二级条目对应的布局文件    * 参数8.fromto,就是map中的key,指定要显示的对象
 *    * 参数9.与参数8对应,指定要显示在childs中的id   
 */
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this, groups, R.layout.groups, new String[] { "group" },
new int[] { R.id.group }, childs, R.layout.list_item,
new String[] { "name", "this", "last" }, new int[] {
R.id.item1, R.id.item2, R.id.item3 });
setListAdapter(adapter);
}

}

解决方案 »

  1.   

    上面第一个是groups.xml,第二个是list_item.xml,第三个是main.xml,下面是代码。运行结果显示两个group下面的数据相同,且都只有一行数据,大家帮忙看看是哪里出现问题了?
      

  2.   

    // 创建一级条目下的的二级条目       
     List<Map<String, Object>> child1 =  child1 = new ArrayList<Map<String, Object>>();      
     // 同样是在一级条目目录下创建两个对应的二级条目目录        
     for (int i = 0; i < 3; i++) {                        
     Map<String, Object> childdata1 = new HashMap<String, Object>();           
     childdata1.put("name", "九江电厂");            
     childdata1.put("this", "200");            
     childdata1.put("last", "300");            
     child1.add(childdata1);                     
     }
     childs.add(child1);
     List<Map<String, Object>> child2 =new ArrayList<Map<String,Object>>();      
     for(int i = 0; i< 3; i++){            
     //child2 =            
     Map<String, Object> childdata2 = new HashMap<String, Object>();  
     childdata2.put("name", String.valueOf(i+1));    
     childdata2.put("this", "100");            
     childdata2.put("last", "200");         
     child2.add(childdata2);          
     }
     childs.add(child2);忘记了怎么使用ExpandableListView了。借鉴使用了,感谢