Android 动态添加删除ExpandableListView的item的例子
这个例子可以学习到如下几点:
1.通过自定义Dialog(单独布局的xml文件进行弹出显示)
2.通过menu点击监听添加,删除view中的items
3.点击ExpandableListView中group和child的事件监听
下面是代码:
1.ExpandableListViewStudy.Java
package cn.com.example; import java.util.ArrayList; import java.util.List; import android.app.Activity; import android.app.Dialog; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ExpandableListView; import android.widget.Toast; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.ExpandableListView.OnGroupClickListener; public class ExpandableListViewStudy extends Activity { public final static int MENU_ADD = Menu.FIRST; public final static int MENU_DELETE = Menu.FIRST + 1; ExpandableListView expandList; InfoDetailsAdapter adapter; Activity activity; List<String> group; List<List<String>> child; // 初始化group child内容 public void initialData() { group = new ArrayList<String>(); child = new ArrayList<List<String>>(); addInfo("group 1", new String[] { "one 1", "two 1", "three 1" }); addInfo("group 2", new String[] { "one 2", "two 2", "three 2" }); addInfo("group 3", new String[] { "one 3", "two 3", "three 3" }); } 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); } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); activity = this; expandList = (ExpandableListView) findViewById(R.id.expandList); // 初始化各级元素 initialData(); // 适配器内容 adapter = new InfoDetailsAdapter(this, group, child); expandList.setAdapter(adapter); expandList.setOnGroupClickListener(new OnGroupClickListener() { @Override public boolean onGroupClick(ExpandableListView arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub System.out.println("The row id of the group clicked" + arg3); Toast.makeText(activity, "[Group Click]:" + arg2, Toast.LENGTH_SHORT).show(); return false; } }); expandList.setOnChildClickListener(new OnChildClickListener() { @Override public boolean onChildClick(ExpandableListView arg0, View arg1, int arg2, int arg3, long arg4) { // TODO Auto-generated method stub Toast.makeText(activity, "[Child Click]:" + arg2 + ":" + arg3, Toast.LENGTH_SHORT).show(); return false; } }); } // 下述2个函数处理Menu按钮的事件 public boolean onCreateOptionsMenu(Menu menu) { // TODO Auto-generated method stub menu.add(0, MENU_ADD, 0, " 添加 "); menu.add(0, MENU_DELETE, 0, " 删除 "); return super.onCreateOptionsMenu(menu); } public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case MENU_ADD: Log.i("", "FRIEND_ID"); createDialogAdd(); dialogAdd.show(); break; case MENU_DELETE: Log.i("", "FRIEND_ID"); createDialogDelete(); dialogDelete.show(); break; } return super.onOptionsItemSelected(item); } EditText add_name, add_phone, add_sex, add_home; EditText delete_id; Button add_ok, add_no; Button delete_ok, delete_no; Dialog dialogAdd, dialogDelete; public void createDialogAdd() { Log.i("", "createDialogAdd"); View 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(); adapter.notifyDataSetChanged(); } }); add_no.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub dialogAdd.dismiss(); } }); } public void createDialogDelete() { View 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(); adapter.notifyDataSetChanged(); } } }); delete_no.setOnClickListener(new OnClickListener() { public void onClick(View v) { // TODO Auto-generated method stub dialogDelete.dismiss(); } }); } }
2.InfoDetailsAdapter.java(适配器文件,用于提供给View内容)
package cn.com.example; import java.util.List; import android.app.Activity; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; //为expandable list view 提供内容的基类 public class InfoDetailsAdapter extends BaseExpandableListAdapter { Activity activity; List<String> group; List<List<String>> child; public InfoDetailsAdapter(Activity a, List<String> group, List<List<String>> child) { activity = a; this.group = group; this.child = child; } // child method stub @Override public Object getChild(int groupPosition, int childPosition) { // TODO Auto-generated method stub // System.out.println("*******************"+child.get(groupPosition).get(childPosition)); 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 long getGroupId(int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public int getGroupCount() { // TODO Auto-generated method stub return group.size(); } @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; } }
至于里面的方法各自起什么作用,可以自己进行测试
3.main.xml布局文件
<?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" android:id="@+id/layout"> <ExpandableListView android:id="@+id/expandList" android:layout_width="fill_parent" android:layout_height="wrap_content" /> </LinearLayout>
4.add.xml
<?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 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 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 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 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 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>
这个布局可以优化一下
5.delete.xml
<?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>