package com.android.contactsmanager;
import java.util.ArrayList;
import java.util.Random;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorTreeAdapter;
import android.widget.EditText;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;
import com.android.contactsmanager.bean.MyConstant;
import com.android.contactsmanager.dao.ContactsManagerDbAdater;
public class ContactsManager extends Activity{
public static final String TAG = "ContactsManager";
private ContactsManagerDbAdater contactsManagerDbAdapter;
int groupNameIndex;
private MyCursrTreeAdapter myCursorTreeAdapter;
private ExpandableListView mExpandableListView;
private AutoCompleteTextView mSearchEditText;
private ImageButton mSearchButton;
private LinearLayout topBar;
View view;
PopupWindow pop;
Button btnSms;
Button btnEmail;
Button btnCall;
// 缓存除了所选联系人所在组的所有组,用在移动联系人上
String groups[];
// 缓存用户所在的组,用在移动联系人上
String mygroupName;
// 长按分组上的 菜单
public static final int MENU_GROUP_ADD = Menu.FIRST;
public static final int MENU_GROUP_DELETE = Menu.FIRST + 1;
public static final int MENU_GROUP_MODIFY = Menu.FIRST + 2;
public static final int MENU_GROUP_ADDCONTACT = Menu.FIRST + 3;
// 长按联系人菜单
public static final int MENU_CONTACTS_DELETE = Menu.FIRST;
public static final int MENU_CONTACTS_MODIFY = Menu.FIRST + 1;
public static final int MENU_CONTACTS_MOVE = Menu.FIRST + 2;
// 联系人各个字段索引
private static final int icon_index = 1;
private static final int name_index = 2;
private static final int description_index = 3;
private static final int telPhone_index = 4;
private static final int email_index = 5;
// 组上groupName字段索引
private static final int groupName_index = 1;
private Cursor groupCursor;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
initLayout();
initMyAdapter();
initPopupWindow();
// 由于程序的背景不是黑色的,若不设置为0,拖动时会出现黑色
mExpandableListView.setCacheColorHint(0);
// 去掉每项下面的黑线(分割线)
mExpandableListView.setDivider(null);
// 自定义组的左边的下拉图标状态变化
mExpandableListView.setGroupIndicator(getResources().getDrawable(
R.drawable.expander_ic_folder));
}
/**
* *******************************各种初始化函数*******************************
*/
private void initLayout() {
/*
* 由于ExpandableListView与其他组件似乎不能直接嵌套在一个 布局文件里面,但是他有个增加头布局和尾布局的方法,我们可以
* 结合LayoutInflater和这个两个方法就可以在含有ExpandableListView 与ListView的布局中添加其他控件了
*/
// 初始化搜索栏布局
topBar = (LinearLayout) getLayoutInflater().inflate(R.layout.top_bar,
null).findViewById(R.id.top_bar);
mSearchEditText = (AutoCompleteTextView) topBar
.findViewById(R.id.search);
mSearchButton = (ImageButton) topBar.findViewById(R.id.add_contact);
// 初始化ExpandableListView布局
mExpandableListView = (ExpandableListView) findViewById(R.id.list);
registerForContextMenu(mExpandableListView);
mExpandableListView.addHeaderView(topBar);
mExpandableListView.setOnTouchListener(mExpandTouchListener);
// 打开数据库
contactsManagerDbAdapter = new ContactsManagerDbAdater(this);
contactsManagerDbAdapter.open();
// 绑定按钮监听
mSearchButton.setOnClickListener(mSearchListener);
mExpandableListView.setOnChildClickListener(mContactClickListener);
}
private void initPopupWindow() {
// 联系人菜单弹出框初始化
view = this.getLayoutInflater().inflate(R.layout.popup_window, null);
pop = new PopupWindow(view, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
pop.setOutsideTouchable(true);
btnSms = (Button) view.findViewById(R.id.btnSms);
btnEmail = (Button) view.findViewById(R.id.btnEmail);
btnCall = (Button) view.findViewById(R.id.btnCall);
}
// 给适配器赋值,刷新界面的时候也会用到
public void initMyAdapter() {
// 初始化ExpandableListView数据
groupCursor = contactsManagerDbAdapter.getAllGroups();
startManagingCursor(groupCursor);
// get the groupName column index
groupNameIndex = groupCursor.getColumnIndexOrThrow("groupName");
// set my adapter
myCursorTreeAdapter = new MyCursrTreeAdapter(groupCursor, this, true);
mExpandableListView.setAdapter(myCursorTreeAdapter);
// 初始化搜索框数据
String sql = "select name from contacts";
Cursor contactCursor = contactsManagerDbAdapter.getCursorBySql(sql,
null);
Log.w(TAG, "" + contactCursor.toString());
contactCursor.moveToFirst();
ArrayList<String> nameList = new ArrayList<String>();
for (int i = 0; i < contactCursor.getCount(); i++) {
nameList.add(contactCursor.getString(contactCursor
.getColumnIndex("name")));
contactCursor.moveToNext();
}
contactCursor.close();
// 绑定自动完成框数据
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
ContactsManager.this,
android.R.layout.simple_dropdown_item_1line, nameList);
mSearchEditText.setAdapter(adapter);
}
/**
* *******************************监听器类定义*******************************
*/
// 联系人搜索监听器
private OnClickListener mSearchListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = mSearchEditText.getText().toString();
// String sql="select * from contacts where name=" + name;
String sql = "select name from contacts";
Cursor contactCursor = contactsManagerDbAdapter.getCursorBySql(sql,
null);
Log.w(TAG, "" + contactCursor.toString());
contactCursor.moveToFirst();
for (int i = 0; i < contactCursor.getCount(); i++) {
if (contactCursor.getString(
contactCursor.getColumnIndex("name")).equals(name)) {
Intent intent = new Intent();
intent.putExtra("name", name);
intent.setAction(Intent.ACTION_EDIT);
intent.setDataAndType(Uri.parse(MyConstant.CONTENT_URI),
MyConstant.CONTENT_TYPE_EDIT);
startActivity(intent);
contactCursor.close();
return;
}
contactCursor.moveToNext();
}
contactCursor.close();
Toast.makeText(ContactsManager.this, "没有找到联系人", Toast.LENGTH_LONG)
.show();
}
};
// 联系人详细信息查看按钮
private OnChildClickListener mContactClickListener = new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
TextView name = (TextView) v.findViewById(R.id.name);
//Uri uri = Uri.parse(MyConstant.CONTENT_URI);
//Intent intent = new Intent(Intent.ACTION_EDIT,uri);
Intent intent = new Intent();
intent.setType(MyConstant.CONTENT_TYPE_EDIT);
- 1
- 2
- 3
前往页