ExpandableListView / ExpandableListActivity的使用及数据更新

ExpandableListView / ExpandableListActivity

 

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

 

 

 

[代码 步骤]

 

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

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

 

 

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

Java代码  
  1. List<String> group;   
  2.     List<List<String>> child;  
List<String> group;
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.     }  
public void initialData(){
group = new ArrayList<String>();
child = new ArrayList<List<String>>();
addInfo("griffinshi", new String[]{"13776117119","man","Jiangsu"});
addInfo("lancewu",new String[]{"1321134","man","Taiwan"});
addInfo("kandyli",new String[]{"12345"});
}
public void addInfo(String p,String[] c){
group.add(p);
List<String> item = new ArrayList<String>();
for(int i=0;i<c.length;i++){
item.add(c[i]);
}
child.add(item);
}

 

 

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.     }  
public class InfoDetailsAdapter extends BaseExpandableListAdapter {
Activity activity;
public InfoDetailsAdapter(Activity a){
activity = a;
}
//child method stub
@Override
public Object getChild(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return child.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
// TODO Auto-generated method stub
return childPosition;
}
@Override
public int getChildrenCount(int groupPosition) {
// TODO Auto-generated method stub
return child.get(groupPosition).size();
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String string = child.get(groupPosition).get(childPosition);
return getGenericView(string);
}
//group method stub
@Override
public Object getGroup(int groupPosition) {
// TODO Auto-generated method stub
return group.get(groupPosition);
}
@Override
public int getGroupCount() {
// TODO Auto-generated method stub
return group.size();
}
@Override
public long getGroupId(int groupPosition) {
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
String string = group.get(groupPosition);
return getGenericView(string);
}
//View stub to create Group/Children 's View
public TextView getGenericView(String s) {
// Layout parameters for the ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.FILL_PARENT, 64);
TextView text = new TextView(activity);
text.setLayoutParams(lp);
// Center the text vertically
text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// Set the text starting position
text.setPadding(36, 0, 0, 0);
text.setText(s);
return text;
}
@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;
}
}

 

 

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>  
<?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"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
/>
<EditText
android:id="@+id/add_name"
android:layout_width="200dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话:"
/>
<EditText
android:id="@+id/add_phone"
android:layout_width="200dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
/>
<EditText
android:id="@+id/add_sex"
android:layout_width="200dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="住址:"
/>
<EditText
android:id="@+id/add_home"
android:layout_width="200dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/add_ok"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@+id/add_no"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:text="NO"
/>
</LinearLayout>
</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.     }  
public void createDialogAdd(){
viewAdd = this.getLayoutInflater().inflate(R.layout.add, null);
dialogAdd = new Dialog(this);
dialogAdd.setContentView(viewAdd);
dialogAdd.setTitle("输入新成员信息");
add_name = (EditText)viewAdd.findViewById(R.id.add_name);
add_phone = (EditText)viewAdd.findViewById(R.id.add_phone);
add_sex = (EditText)viewAdd.findViewById(R.id.add_sex);
add_home = (EditText)viewAdd.findViewById(R.id.add_home);
add_ok = (Button)viewAdd.findViewById(R.id.add_ok);
add_no = (Button)viewAdd.findViewById(R.id.add_no);
add_ok.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String[] data = {
add_phone.getText().toString(),
add_sex.getText().toString(),
add_home.getText().toString()
};
addInfo(add_name.getText().toString(),data);
dialogAdd.dismiss();
mAdapter.notifyDataSetChanged();
}
});
add_no.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
dialogAdd.dismiss();
}
});
}

 

 

* 运行截图:

 

 

 

 

 

* 定义删除数据界面: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>  
<?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"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ID:"
/>
<EditText
android:id="@+id/delete_id"
android:layout_width="200dip"
android:layout_height="wrap_content"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/delete_ok"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:text="OK"
/>
<Button
android:id="@+id/delete_no"
android:layout_width="90dip"
android:layout_height="wrap_content"
android:text="NO"
/>
</LinearLayout>
</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.     }  
public void createDialogDelete(){
viewDelete = this.getLayoutInflater().inflate(R.layout.delete, null);
dialogDelete = new Dialog(this);
dialogDelete.setContentView(viewDelete);
dialogDelete.setTitle("删除指定成员");
delete_id = (EditText)viewDelete.findViewById(R.id.delete_id);
delete_ok = (Button)viewDelete.findViewById(R.id.delete_ok);
delete_no = (Button)viewDelete.findViewById(R.id.delete_no);
delete_ok.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
String id = delete_id.getText().toString();
if(! id.equals("")){
int i = Integer.parseInt(id);
group.remove(i);
child.remove(i);
dialogDelete.dismiss();
mAdapter.notifyDataSetChanged();
}
}
});
delete_no.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO Auto-generated method stub
dialogDelete.dismiss();
}
});
}

 

 

* 运行截图:

 

 

 

 

最后 说一下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.         });  

 

 

你可能感兴趣的:(ExpandableListView / ExpandableListActivity的使用及数据更新)