ExpandableListView / ExpandableListActivity 使用及数据更新

1. 定义含有ExpandableListView 的布局:main.xml

Xml代码   收藏代码
  1. ExpandableListView / ExpandableListActivity

     

     二者关系 和 ListActivity / ListView 是一样的

    [代码 步骤]

     

    1. 定义含有ExpandableListView 的布局:main.xml

    Xml代码   收藏代码
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:id="@+id/layout"  
  7.     >  
  8. <ExpandableListView    
  9.     android:id="@+id/expandList"  
  10.     android:layout_width="fill_parent"   
  11.     android:layout_height="wrap_content"   
  12.     />  
  13. </LinearLayout>  

 

 

2.  定义数据结构List<String>, List<List<String>> 分别用于存放 Group / Children 的String

Java代码   收藏代码
  1. List<String> group;  
  2.     List<List<String>> child;  

 

 

3. 初始化 List<String> List<List<String>>  并插入一些数据

Java代码   收藏代码
  1. public void initialData(){  
  2.         group = new ArrayList<String>();  
  3.           
  4.         child = new ArrayList<List<String>>();  
  5.           
  6.         addInfo("griffinshi"new String[]{"13776117119","man","Jiangsu"});  
  7.         addInfo("lancewu",new String[]{"1321134","man","Taiwan"});  
  8.         addInfo("kandyli",new String[]{"12345"});  
  9.     }  
  10.       
  11.     public void addInfo(String p,String[] c){  
  12.         group.add(p);  
  13.           
  14.         List<String> item = new ArrayList<String>();  
  15.           
  16.         for(int i=0;i<c.length;i++){  
  17.             item.add(c[i]);  
  18.         }  
  19.           
  20.         child.add(item);  
  21.     }  

 

 

3. 定义BaseExpandableListAdapter 并与List<String> List<List<String>> 数据相适配

Java代码   收藏代码
  1. public class InfoDetailsAdapter extends BaseExpandableListAdapter {  
  2.         Activity activity;  
  3.           
  4.         public InfoDetailsAdapter(Activity a){  
  5.             activity = a;  
  6.         }  
  7.           
  8.         //child method stub  
  9.           
  10.         @Override  
  11.         public Object getChild(int groupPosition, int childPosition) {  
  12.             // TODO Auto-generated method stub  
  13.             return child.get(groupPosition).get(childPosition);  
  14.         }  
  15.   
  16.         @Override  
  17.         public long getChildId(int groupPosition, int childPosition) {  
  18.             // TODO Auto-generated method stub  
  19.             return childPosition;  
  20.         }  
  21.   
  22.         @Override  
  23.         public int getChildrenCount(int groupPosition) {  
  24.             // TODO Auto-generated method stub  
  25.             return child.get(groupPosition).size();  
  26.         }  
  27.           
  28.         @Override  
  29.         public View getChildView(int groupPosition, int childPosition,  
  30.                 boolean isLastChild, View convertView, ViewGroup parent) {  
  31.             // TODO Auto-generated method stub  
  32.             String string = child.get(groupPosition).get(childPosition);  
  33.             return getGenericView(string);  
  34.         }  
  35.   
  36.   
  37.         //group method stub  
  38.         @Override  
  39.         public Object getGroup(int groupPosition) {  
  40.             // TODO Auto-generated method stub  
  41.             return group.get(groupPosition);  
  42.         }  
  43.   
  44.         @Override  
  45.         public int getGroupCount() {  
  46.             // TODO Auto-generated method stub  
  47.             return group.size();  
  48.         }  
  49.   
  50.         @Override  
  51.         public long getGroupId(int groupPosition) {  
  52.             // TODO Auto-generated method stub  
  53.             return groupPosition;  
  54.         }  
  55.   
  56.         @Override  
  57.         public View getGroupView(int groupPosition, boolean isExpanded,  
  58.                 View convertView, ViewGroup parent) {  
  59.             // TODO Auto-generated method stub  
  60.             String string = group.get(groupPosition);  
  61.             return getGenericView(string);  
  62.         }  
  63.   
  64.         //View stub to create Group/Children 's View  
  65.         public TextView getGenericView(String s) {  
  66.             // Layout parameters for the ExpandableListView  
  67.             AbsListView.LayoutParams lp = new AbsListView.LayoutParams(  
  68.                     ViewGroup.LayoutParams.FILL_PARENT, 64);  
  69.   
  70.             TextView text = new TextView(activity);  
  71.             text.setLayoutParams(lp);  
  72.             // Center the text vertically  
  73.             text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);  
  74.             // Set the text starting position  
  75.             text.setPadding(36000);  
  76.               
  77.             text.setText(s);  
  78.             return text;  
  79.         }  
  80.           
  81.           
  82.           
  83.         @Override  
  84.         public boolean hasStableIds() {  
  85.             // TODO Auto-generated method stub  
  86.             return false;  
  87.         }  
  88.   
  89.         @Override  
  90.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  91.             // TODO Auto-generated method stub  
  92.             return true;  
  93.         }  
  94.           
  95.           
  96.     }  

 

 

4. emulator 运行截图:

 

 

 

5. 下面说一下 数据更新 问题 包括:添加数据 删除数据

 

* 定义添加数据界面:add.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  9.     android:orientation="horizontal"  
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     >  
  13. <TextView    
  14.     android:layout_width="wrap_content"   
  15.     android:layout_height="wrap_content"   
  16.     android:text="姓名:"  
  17.     />  
  18. <EditText    
  19.     android:id="@+id/add_name"  
  20.     android:layout_width="200dip"   
  21.     android:layout_height="wrap_content"   
  22.     />  
  23. </LinearLayout>    
  24.   
  25. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  26.     android:orientation="horizontal"  
  27.     android:layout_width="wrap_content"  
  28.     android:layout_height="wrap_content"  
  29.     >  
  30. <TextView    
  31.     android:layout_width="wrap_content"   
  32.     android:layout_height="wrap_content"   
  33.     android:text="电话:"  
  34.     />  
  35. <EditText    
  36.     android:id="@+id/add_phone"  
  37.     android:layout_width="200dip"   
  38.     android:layout_height="wrap_content"   
  39.     />  
  40. </LinearLayout>    
  41.   
  42. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  43.     android:orientation="horizontal"  
  44.     android:layout_width="wrap_content"  
  45.     android:layout_height="wrap_content"  
  46.     >  
  47. <TextView    
  48.     android:layout_width="wrap_content"   
  49.     android:layout_height="wrap_content"   
  50.     android:text="性别:"  
  51.     />  
  52. <EditText    
  53.     android:id="@+id/add_sex"  
  54.     android:layout_width="200dip"   
  55.     android:layout_height="wrap_content"   
  56.     />  
  57. </LinearLayout>  
  58.   
  59. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  60.     android:orientation="horizontal"  
  61.     android:layout_width="wrap_content"  
  62.     android:layout_height="wrap_content"  
  63.     >  
  64. <TextView    
  65.     android:layout_width="wrap_content"   
  66.     android:layout_height="wrap_content"   
  67.     android:text="住址:"  
  68.     />  
  69. <EditText    
  70.     android:id="@+id/add_home"  
  71.     android:layout_width="200dip"   
  72.     android:layout_height="wrap_content"   
  73.     />  
  74. </LinearLayout>     
  75.   
  76. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  77.     android:orientation="horizontal"  
  78.     android:layout_width="wrap_content"  
  79.     android:layout_height="wrap_content"  
  80.     >  
  81. <Button    
  82.     android:id="@+id/add_ok"  
  83.     android:layout_width="90dip"   
  84.     android:layout_height="wrap_content"   
  85.     android:text="OK"  
  86.     />  
  87. <Button    
  88.     android:id="@+id/add_no"  
  89.     android:layout_width="90dip"   
  90.     android:layout_height="wrap_content"   
  91.     android:text="NO"  
  92.     />  
  93. </LinearLayout>   
  94.       
  95. </LinearLayout>  

 

 

* add.xml 里 View 的定义:

Xml代码   收藏代码
  1. public void createDialogAdd(){  
  2.         viewAdd = this.getLayoutInflater().inflate(R.layout.add, null);  
  3.           
  4.         dialogAdd = new Dialog(this);  
  5.         dialogAdd.setContentView(viewAdd);  
  6.         dialogAdd.setTitle("输入新成员信息");  
  7.           
  8.         add_name = (EditText)viewAdd.findViewById(R.id.add_name);  
  9.         add_phone = (EditText)viewAdd.findViewById(R.id.add_phone);  
  10.         add_sex = (EditText)viewAdd.findViewById(R.id.add_sex);  
  11.         add_home = (EditText)viewAdd.findViewById(R.id.add_home);  
  12.           
  13.         add_ok = (Button)viewAdd.findViewById(R.id.add_ok);  
  14.         add_no = (Button)viewAdd.findViewById(R.id.add_no);  
  15.           
  16.         add_ok.setOnClickListener(new OnClickListener(){  
  17.             public void onClick(View v) {  
  18.                 // TODO Auto-generated method stub  
  19.                 String[] data = {  
  20.                         add_phone.getText().toString(),  
  21.                         add_sex.getText().toString(),  
  22.                         add_home.getText().toString()  
  23.                 };  
  24.                   
  25.                 addInfo(add_name.getText().toString(),data);  
  26.                   
  27.                 dialogAdd.dismiss();  
  28.                   
  29.                 mAdapter.notifyDataSetChanged();  
  30.             }  
  31.         });  
  32.           
  33.         add_no.setOnClickListener(new OnClickListener(){  
  34.             public void onClick(View v) {  
  35.                 // TODO Auto-generated method stub  
  36.                 dialogAdd.dismiss();  
  37.             }  
  38.         });  
  39.     }  

 

 

* 运行截图:

 

 

 

 

 

* 定义删除数据界面:delete.xml

Xml代码   收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.   
  8. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  9.     android:orientation="horizontal"  
  10.     android:layout_width="wrap_content"  
  11.     android:layout_height="wrap_content"  
  12.     >  
  13. <TextView    
  14.     android:layout_width="wrap_content"   
  15.     android:layout_height="wrap_content"   
  16.     android:text="ID:"  
  17.     />  
  18. <EditText    
  19.     android:id="@+id/delete_id"  
  20.     android:layout_width="200dip"   
  21.     android:layout_height="wrap_content"   
  22.     />  
  23. </LinearLayout>    
  24.   
  25. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  26.     android:orientation="horizontal"  
  27.     android:layout_width="wrap_content"  
  28.     android:layout_height="wrap_content"  
  29.     >  
  30. <Button    
  31.     android:id="@+id/delete_ok"  
  32.     android:layout_width="90dip"   
  33.     android:layout_height="wrap_content"   
  34.     android:text="OK"  
  35.     />  
  36. <Button    
  37.     android:id="@+id/delete_no"  
  38.     android:layout_width="90dip"   
  39.     android:layout_height="wrap_content"   
  40.     android:text="NO"  
  41.     />  
  42. </LinearLayout>   
  43.       
  44. </LinearLayout>  

 

 

* delete.xml 里View 定义:

Java代码   收藏代码
  1. public void createDialogDelete(){  
  2.         viewDelete = this.getLayoutInflater().inflate(R.layout.delete, null);  
  3.           
  4.         dialogDelete = new Dialog(this);  
  5.         dialogDelete.setContentView(viewDelete);  
  6.         dialogDelete.setTitle("删除指定成员");  
  7.           
  8.         delete_id = (EditText)viewDelete.findViewById(R.id.delete_id);  
  9.         delete_ok = (Button)viewDelete.findViewById(R.id.delete_ok);  
  10.         delete_no = (Button)viewDelete.findViewById(R.id.delete_no);  
  11.           
  12.         delete_ok.setOnClickListener(new OnClickListener(){  
  13.             public void onClick(View v) {  
  14.                 // TODO Auto-generated method stub  
  15.                   
  16.                 String id = delete_id.getText().toString();  
  17.                   
  18.                 if(! id.equals("")){  
  19.                     int i = Integer.parseInt(id);  
  20.                     group.remove(i);  
  21.                     child.remove(i);  
  22.                       
  23.                     dialogDelete.dismiss();  
  24.                       
  25.                     mAdapter.notifyDataSetChanged();  
  26.                 }  
  27.                       
  28.                       
  29.             }  
  30.         });  
  31.           
  32.         delete_no.setOnClickListener(new OnClickListener(){  
  33.             public void onClick(View v) {  
  34.                 // TODO Auto-generated method stub  
  35.                 dialogDelete.dismiss();  
  36.             }  
  37.         });  
  38.     }  

 

 

* 运行截图:

 

 

 

 

最后 说一下ExpandableListView的回调函数 用于监听那个id 被expand

Java代码   收藏代码
  1. expandList.setOnGroupClickListener(new OnGroupClickListener(){  
  2.   
  3.             @Override  
  4.             public boolean onGroupClick(ExpandableListView arg0, View arg1,  
  5.                     int arg2, long arg3) {  
  6.                 // TODO Auto-generated method stub  
  7.                 Toast.makeText(activity,"[Group Click]:"+arg2,Toast.LENGTH_LONG).show();  
  8.                   
  9.                 return false;  
  10.             }  
  11.               
  12.         });  
  13.           
  14.         expandList.setOnChildClickListener(new OnChildClickListener(){  
  15.   
  16.             @Override  
  17.             public boolean onChildClick(ExpandableListView arg0, View arg1,  
  18.                     int arg2, int arg3, long arg4) {  
  19.                 // TODO Auto-generated method stub  
  20.                 Toast.makeText(activity,"[Child Click]:"+arg2+":"+arg3,Toast.LENGTH_LONG).show();  
  21.                   
  22.                 return false;  
  23.             }  
  24.               
  25.         });  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值