package org.ngweb.addgroups;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.ContextMenu;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
public class AddGroupsActivity extends ExpandableListActivity implements
OnClickListener {
/** Called when the activity is first created. */
//控制进度条
/*protected static final int stop = 1;
protected static final int start = 2;
int intCounter = 1;
ProgressDialog dialog = null;
*/
DBHelper dbHelper;
EditText editAddGroup;
EditText editDeleteGroup;
EditText editAddChildren;
EditText editChildNewName;
EditText editNetName;
ArrayList<String> groupsList = new ArrayList<String>();
LinearLayout lineUpdate;
EditText editGroupsID;
EditText editGroupsNewName;
int groupsPosition = 0;
String index;
String index2;
SQLiteDatabase db;
Cursor cur;
Cursor curChild;
Cursor backimage;
Button btnAddGroups;// 添加分组
Button btnClose;// 关闭
Button btnFind;// 搜索
//8个imageButton,用来选择图片
ImageButton imagebtn;
ImageButton imagebtn1;
ImageButton imagebtn2;
ImageButton imagebtn3;
ImageButton imagebtn4;
ImageButton imagebtn5;
ImageButton imagebtn6;
ImageButton imagebtn7;
String findName = " ";// 选中的成员姓名
ImageView previewImage;//图片预览
ImageView previewImage2;//图片预览
AutoCompleteTextView autoComplete;//搜索姓名
int imageButtonIndex = 0;// 用来标志添加成员还是重命名成员
boolean isSelectImage;// 判断添加时是否选择了图片
int[] imageSource = { R.drawable.source, R.drawable.source1,
R.drawable.source2, R.drawable.source3, R.drawable.source4,
R.drawable.source5, R.drawable.source6, R.drawable.source7 };//ImageButton显示的图片资源
int imageIndex;// 作为标记,决定imageView的图片资源
String title;// 从界面上得到的姓名
int changImage = 0;
int width;// 屏幕的宽度
int height;// 屏幕的高度
//ArrayAdapter<String> adapter;
public String[][] children;
public String[][] netName;// 用来存储网名
public int[][] imagePosition;// 用来存储图片
//设置控件
public void setupViews() {
btnFind = (Button) this.findViewById(R.id.id_btnFind);
btnFind.setOnClickListener(this);
btnAddGroups = (Button) this.findViewById(R.id.id_btnAddGroups);
btnAddGroups.setOnClickListener(this);
btnClose = (Button) this.findViewById(R.id.id_btnClose);
btnClose.setOnClickListener(this);
autoComplete = (AutoCompleteTextView) this.findViewById(R.id.auto);
}
@Override
public void onCreate(Bundle savedInstanceState) {
dbHelper = new DBHelper(this);
db = dbHelper.getReadableDatabase();
ExpandableListAdapter adapter;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
// 获取屏幕分辨率
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
width = dm.widthPixels;
height = dm.heightPixels;
adapter = new MyExpandableListAdapter();
setListAdapter(adapter);
registerForContextMenu(getExpandableListView());
}
public class MyExpandableListAdapter extends BaseExpandableListAdapter {
ArrayList<String> groups = new ArrayList<String>();
ArrayList<String> arrayChildren = new ArrayList<String>();
ArrayList<String> arrayChildrenList = new ArrayList<String>();
public MyExpandableListAdapter() {
cur = db.query(DBHelper.CONTACTS_TABLE, ContactColumn.StringGroup,
null, null, null, null, null);
curChild = db.query(DBHelper.CONTACTS_TABLE2,
ContactColumn.StringChild, null, null, null, null, null);
children = new String[cur.getCount()][];
netName = new String[cur.getCount()][];
imagePosition = new int[cur.getCount()][];
// 搜索时自动弹出成员姓名代码
Cursor curChildName = db.query(DBHelper.CONTACTS_TABLE2,
ContactColumn.StringChild, null, null, null, null, null);
String[] childrenNames = new String[curChildName.getCount()];
int childPosition = 0;
if (curChildName.getCount() != 0) {
while (curChildName.moveToNext()) {
childrenNames[childPosition] = curChildName.getString(1);
childPosition++;
}
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
AddGroupsActivity.this,
android.R.layout.simple_dropdown_item_1line, childrenNames);
autoComplete.setAdapter(adapter);
int i = 0;
while (cur.moveToNext()) {
groups.add(cur.getString(1));
int len = 0;
int j = 0;
boolean isFirst = curChild.isFirst();
if (isFirst) {
String childFirst = curChild.getString(3);
String groupFirst = cur.getString(2);
if (childFirst.equals(groupFirst)) {
len++;
}
}
while (curChild.moveToNext()) {
String child = curChild.getString(3);
String group = cur.getString(2);
if (child.equals(group)) {
len++;
}
}
curChild.moveToFirst();
if (curChild.getCount() != 0 && cur.getCount() != 0) {
// /////////////////////////////////////////////
children[i] = new String[len];// 必须先这样定义
// ///////////////////////////////////////////////
netName[i] = new String[len];
imagePosition[i] = new int[len];
String child = curChild.getString(3);
String group = cur.getString(2);
if (child.equals(group)) {
children[i][j] = curChild.getString(1);
netName[i][j] = curChild.getString(4);
imagePosition[i][j] = curChild.getInt(5);
j++;
}
}
while (curChild.moveToNext()) {
String child2 = curChild.getString(3);
String group2 = cur.getString(2);
if (child2.equals(group2)) {
children[i][j] = curChild.getString(1);
netName[i][j] = curChild.getString(4);
imagePosition[i][j] = curChild.getInt(5);
j++;
}
}
i++;
curChild.moveToFirst();
}
cur.close();
curChild.close();
}
public Object getNetName(int groupPosition, int childPosition) {
return netName[groupPosition][childPosition];
}
public Object getChild(int groupPosition, int childPosition) {
return children[groupPosition][childPosition];
}
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount(int groupPosition) {
Cursor curChild2 = db.query(DBHelper.CONTACTS_TABLE2,
ContactColumn.StringChild, null, null, null, null, null);
if (curChild2.getCount() > 0 && groupPosition < children.length) {
curChild2.close();
return children[groupPosition].length;
}
curChild2.close();
return 0;
}
// 设置显示父分组的TextView
public TextView getParentView() {
AbsListView.LayoutParams lp;
if (height > 480) {//根据屏幕对控件位置进行控制
lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
} else {
lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 55);
}
TextView textView = new TextView(AddGroupsActivity.this);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
//根据屏幕对控件位置进行控制
if (width > 320) {
textView.setPadding(
- 1
- 2
前往页