package com.zhengping.contact;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import com.zhengping.contact.db.DBHelper;
import com.zhengping.contact.entity.User;
public class Main extends Activity {
//显示所有数据的ListView
ListView lv;
ArrayList list;
//拥有所有数据的Adapter
SimpleAdapter adapter;
//屏幕下方的工具栏
GridView bottomMenuGrid;
//主菜单的布局
GridView mainMenuGrid;
//主菜单的视图
View mainMenuView;
//登录的视图
View loginView;
//装搜索框的linearlayout,默认情况下visibility=gone
LinearLayout searchLinearout;
LinearLayout mainLinearLayout;
//搜索框
EditText et_search;
EditText et_enter_file_name;
//主菜单的对话框
AlertDialog mainMenuDialog;
//确认对话框
AlertDialog confirmDialog;
//进度条对话框
AlertDialog progressDialog;
//输入文件名的对话框
AlertDialog enterFileNameDialog;
//输入用户名密码的对话框
AlertDialog loginDialog;
//表示保密状态
boolean privacy = false;
//存储标记的数目
int markedNum;
//存储标记条目的_id号
ArrayList<Integer> deleteId;
// 菜单文字
String[] main_menu_itemName = { "显示所有", "删除所有", "备份数据", "还原数据", "更新", "后退"};
//主菜单图片
int[] main_menu_itemSource = {
R.drawable.showall,
R.drawable.menu_delete,
R.drawable.menu_backup,
R.drawable.menu_restore,
R.drawable.menu_fresh,
R.drawable.menu_return};
String[] bottom_menu_itemName = { "增加", "查找", "删除", "菜单","退出" };
String fileName;
int[] bottom_menu_itemSource = {
R.drawable.menu_new_user,
R.drawable.menu_search,
R.drawable.menu_delete,
R.drawable.controlbar_showtype_list,
R.drawable.menu_exit };
/**
* onCreate做的工作就是把listView显示出来
* bottomMenuGrid,mainMenuGrid,searchLinearout都是到要用
* 的时候再初始化,并且只初始化一次
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mainLinearLayout = (LinearLayout)findViewById(R.id.list_ll);
DBHelper helper = new DBHelper(this);//获得所有用户的list
helper.openDatabase(); //打开数据库,就打开这一次,因为Helper中的SQLiteDatabase是静态的。
list = helper.getAllUser(privacy);//拿到所有保密状态为privacy的用户的list
lv = (ListView)findViewById(R.id.lv_userlist); //创建ListView对象
if(list.size() == 0) {
Drawable nodata_bg = getResources().getDrawable(R.drawable.nodata_bg);
mainLinearLayout.setBackgroundDrawable(nodata_bg);
setTitle("没有查到任何数据");
}
//将数据与adapter集合起来
adapter = new SimpleAdapter(this,
list,
R.layout.listitem,
new String[]{"imageid","name","mobilephone"},
new int[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone});
lv.setAdapter(adapter);//将整合好的adapter交给listview,显示给用户看
lv.setOnItemClickListener(new OnItemClickListener() {
/**
* 响应单击事件,单点击某一个选项的时候,跳转到用户详细信息页面
*/
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
HashMap item = (HashMap)arg0.getItemAtPosition(arg2);
int _id = Integer.parseInt(String.valueOf(item.get("_id")));
Intent intent = new Intent(Main.this,UserDetail.class);
User user = new User();
user._id = Integer.parseInt(String.valueOf(item.get("_id")));
user.address = String.valueOf(item.get("address"));
user.company = String.valueOf(item.get("company"));
user.email = String.valueOf(item.get("email"));
user.familyPhone = String.valueOf(item.get("familyphone"));
user.mobilePhone = String.valueOf(item.get("mobilephone"));
user.officePhone = String.valueOf(item.get("officephone"));
user.otherContact = String.valueOf(item.get("othercontact"));
user.position = String.valueOf(item.get("position"));
user.remark = String.valueOf(item.get("remark"));
user.username = String.valueOf(item.get("name"));
user.zipCode = String.valueOf(item.get("zipcode"));
user.imageId = Integer.parseInt(String.valueOf(item.get("imageid")));
intent.putExtra("user", user);
if(searchLinearout != null && searchLinearout.getVisibility()==View.VISIBLE) {
searchLinearout.setVisibility(View.GONE);
}
/*将arg2作为请求码传过去 用于标识修改项的位置*/
startActivityForResult(intent, arg2);
}
});
lv.setCacheColorHint(Color.TRANSPARENT); //设置ListView的背景为透明
lv.setOnItemLongClickListener(new OnItemLongClickListener(){
@Override
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if(deleteId == null) {
deleteId = new ArrayList<Integer>();
}
HashMap item = (HashMap)arg0.getItemAtPosition(arg2);
Integer _id = Integer.parseInt(String.valueOf(item.get("_id")));
RelativeLayout r = (RelativeLayout)arg1;
ImageView markedView = (ImageView)r.getChildAt(2);
if(markedView.getVisibility() == View.VISIBLE) {
markedView.setVisibility(View.GONE);
deleteId.remove(_id);
} else {
markedView.setVisibility(View.VISIBLE);
deleteId.add(_id);
}
return true;
}
});
//为list添加item选择器
Drawable bgDrawable = getResources().getDrawable(R.drawable.list_bg);
lv.setSelector(bgDrawable);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
//清除deleteId的内容
if(deleteId != null) {
deleteId.clear();
}
//当resultCode==3时代表添加了一个用户返回,当resultCode==4的时候代表修改了用户,或者删除了用户,其他条件代表数据没有变化
if(resultCode == 3 || resultCode == 4) {
DBHelper helper = new DBHelper(this);
list = helper.getAllUser(privacy);
adapter =
new SimpleAdapter(
this,
list,
R.layout.listitem,
new String[]{"imageid","name","mobilephone"},
new int[]{R.id.user_image,R.id.tv_name,R.id.tv_mobilephone});
if(list.size() > 0){
mainLinearLayout.setBackgroundDrawable(null);
}
}
lv.setAdapter(adapter); //将整合好的adapter交给listview,显示给用户看
/**
* resultCode只有3、4、5
* 当等于4或者5的时候,代表由UserDetail转过来的。在转想UserDetail的时候,requestCode的值设置的是选中项的位置
*/
if(resultCode == 3) {
lv.setSelection(list.size());
} else {
lv.setSelection(requestCode);
}
}
/**
* 响应点击Menu按钮时的事件,用于设置底部菜单是否可见
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_MENU) {
loadBottomMenu();
if(bottomMenuGrid.getVisibility() == View.VISIBLE) {
if(searchLinearout != null && searchLinearout.getVisib