package com.wangs.activity; import java.util.Stack; import com.wangs.bean.User; import com.wangs.dao.DBHelper; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Gallery; import android.widget.ImageButton; import android.widget.ImageSwitcher; import android.widget.ImageView; import android.widget.Toast; import android.widget.ViewSwitcher.ViewFactory; public class ContactActivity extends Activity { private ImageButton btn_img; private AlertDialog imageChooseDialog; private int[] images = { R.drawable.icon, R.drawable.i1,R.drawable.i2, R.drawable.i3,R.drawable.i4, R.drawable.i5,R.drawable.i6, R.drawable.i7,R.drawable.i2, R.drawable.i9,R.drawable.i10, R.drawable.i11,R.drawable.i12, }; ImageSwitcher is; Gallery gallery; private int imagePosition ;//定义来接收imageid //初始化联系人内容 EditText et_name; EditText et_mobilePhone; EditText et_familyPhone; EditText et_officePhone; EditText et_position; EditText et_company; EditText et_address; EditText et_zipcode; EditText et_email; EditText et_otherContact; EditText et_remark; Button btn_save; Button btv_return; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addnew); initWidget(); //初始化信息 btn_img = (ImageButton) this.findViewById(R.id.btn_img); btn_img.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { initImageChooseDialog(); //初始化alertDialog imageChooseDialog.show(); } }); btn_save.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { String name = et_name.getText().toString(); if(name.equals("")){ Toast.makeText(ContactActivity.this, "姓名不能为空", 2).show(); return; } String mobilePhone= et_mobilePhone.getText().toString(); String familyPhone = et_familyPhone.getText().toString(); String officePhone = et_officePhone.getText().toString(); String position = et_position.getText().toString(); String company = et_company.getText().toString(); String address = et_address.getText().toString(); String zipcode = et_zipcode.getText().toString(); String email = et_email.getText().toString(); String otherContact = et_otherContact.getText().toString(); String remark = et_remark.getText().toString(); int imageId = images[imagePosition]; User user = new User(); user.address = address; user.company = company; user.email = email; user.familyphone = familyPhone; user.imageId = imageId; user.mobilePhone = mobilePhone; user.name = name; user.officePhone = officePhone; user.otherContact = otherContact; user.position = position; user.remark = remark; user.zipCode = zipcode; //save user to database; long success = DBHelper.getInstance(ContactActivity.this).saveUser(user); if(success != -1){ Toast.makeText(ContactActivity.this, "保存成功!!", 2).show(); //setReCode为1的时候代表增加成功,返回主界面 setResult(1); finish() ;// 才跳转 }else{ Toast.makeText(ContactActivity.this, "保存有误,请重新操作!!!", 2).show(); setResult(2); finish(); } } }); btv_return.setOnClickListener(new OnClickListener(){ @Override public void onClick(View arg0) { setResult(2); finish(); } }); } public void initWidget(){ et_name = (EditText) this.findViewById(R.id.et_name); et_officePhone = (EditText) this.findViewById(R.id.et_officePhone); et_mobilePhone = (EditText) this.findViewById(R.id.et_mobilePhone); et_familyPhone = (EditText) this.findViewById(R.id.et_familyPhone); et_company = (EditText) this.findViewById(R.id.et_company); et_address = (EditText) this.findViewById(R.id.et_address); et_email = (EditText) this.findViewById(R.id.et_email); et_position = (EditText) this.findViewById(R.id.et_position); et_zipcode = (EditText) this.findViewById(R.id.et_zipcode); et_remark = (EditText) this.findViewById(R.id.et_remark); et_otherContact = (EditText) this.findViewById(R.id.et_other); btn_save = (Button)this.findViewById(R.id.btn_save); btv_return = (Button)this.findViewById(R.id.btn_return); } private void initImageChooseDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("请选择头像"); //创建2个按钮 builder.setPositiveButton("确定", new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog, int which) { btn_img.setImageResource(images[imagePosition%images.length]); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); //把imageswitch这个文件渲染成view LayoutInflater inflater = LayoutInflater.from(this); View view = inflater.inflate(R.layout.imageswitch, null); //取出view里的gallery和imageswitch gallery = (Gallery) view.findViewById(R.id.img_gallery); is = (ImageSwitcher) view.findViewById(R.id.image_switcher); gallery.setAdapter(new ImageAdapter(this)); gallery.setSelection(images.length/2); //定位到中间 gallery.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { imagePosition = position; is.setImageResource(images[position%images.length]); } @Override public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); is.setFactory(new MyViwFactory(this)); //生成一个iv is.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { btn_img.setImageResource(images[imagePosition%images.length]); //ImageSwitcher选中设置ImageButton的图像 } }); builder.setView(view); imageChooseDialog = builder.create(); //创建它 } class ImageAdapter extends BaseAdapter{ private Context context; private ImageAdapter(Context context){ this.context = context; } @Override public int getCount() { //images.length return Integer.MAX_VALUE; //产生无限循环 } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ImageView iv = new ImageView(context); iv.setImageResource(images[position%images.length]); iv.setAdjustViewBounds(true); //适应视图 iv.setLayoutParams(new Gallery.LayoutParams(80,80)); //设置imageview大小 iv.setPadding(15,10,15,10); //设置图片左上右下间隔 return iv; } } class MyViwFactory implements ViewFactory{ private Context context; private MyViwFactory(Context context){ this.context = context; } @Override public View makeView() { ImageView iv = new ImageView(context); iv.setLayoutParams(new ImageSwitcher.LayoutParams(90,90)); return iv; } } }
----绑定菜单
package com.wangs.activity; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import com.wangs.bean.User; import com.wangs.dao.DBHelper; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.GridView; import android.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { private GridView gv_buttom_menu; private ListView lv_userList; private SimpleAdapter useradapter ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.setContentView(R.layout.main); loadUserList(); } public void loadUserList(){ lv_userList = (ListView) this.findViewById(R.id.lv_userlist); ArrayList data = DBHelper.getInstance(this).getUserList(); useradapter = new SimpleAdapter(this, data, R.layout.list_item, new String[]{"imageid","name","mobilephone"}, new int[]{R.id.user_image,R.id.tv_showname,R.id.tv_showmobile}); lv_userList.setAdapter(useradapter); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if(keyCode == KeyEvent.KEYCODE_MENU){ if(gv_buttom_menu == null){ loadButtomMenu(); } if(gv_buttom_menu.getVisibility() == View.GONE){ gv_buttom_menu.setVisibility(View.VISIBLE); }else{ gv_buttom_menu.setVisibility(View.GONE); } } return super.onKeyDown(keyCode, event); } private void loadButtomMenu(){ gv_buttom_menu = (GridView) this.findViewById(R.id.gv_buttom_menu); gv_buttom_menu.setBackgroundResource(R.drawable.backgound); gv_buttom_menu.setNumColumns(5); gv_buttom_menu.setGravity(Gravity.CENTER); gv_buttom_menu.setVerticalSpacing(4); //水平间隔 ArrayList data = new ArrayList(); Map map = new HashMap(); map.put("itemImage", R.drawable.save); map.put("itemText", "增加"); data.add(map); map = new HashMap(); map.put("itemImage", R.drawable.motify); map.put("itemText", "查找"); data.add(map); map = new HashMap(); map.put("itemImage", R.drawable.laji); map.put("itemText", "删除"); data.add(map); map = new HashMap(); map.put("itemImage", R.drawable.me); map.put("itemText", "菜单"); data.add(map); map = new HashMap(); map.put("itemImage", R.drawable.motify2); map.put("itemText", "退出"); data.add(map); SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.item_menu, new String[]{"itemImage","itemText"}, new int[]{R.id.item_image,R.id.item_text}); gv_buttom_menu.setAdapter(adapter); gv_buttom_menu.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { //HashMap map = (HashMap)parent.getItemAtPosition(1); switch (position) { case 0: //增加 Intent intent = new Intent(MainActivity.this,ContactActivity.class); startActivityForResult(intent, 0); //设置0代表增加操作 break; case 1: break; case 2: break; case 3: break; case 4: break; default: break; } } }); } //判断回调的recode代码 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode ==0){ if(resultCode ==1){ //代表保存成功,刷新主界面 ArrayList data2 = DBHelper.getInstance(this).getUserList(); useradapter = new SimpleAdapter(this, data2, R.layout.list_item, new String[]{"imageid","name","mobilephone"}, new int[]{R.id.user_image,R.id.tv_showname,R.id.tv_showmobile}); lv_userList.setAdapter(useradapter); }else{ //resultCode = 2 } } super.onActivityResult(requestCode, resultCode, data); } }
---- 数据库
package com.wangs.dao; import java.util.ArrayList; import java.util.HashMap; import com.wangs.bean.User; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { public static final String DB_NAME= "contact"; public static final int VERSION = 3; //单例 private static DBHelper instance = null; private SQLiteDatabase db; //主要是让它初始化 public static DBHelper getInstance(Context context){ if(instance == null){ instance = new DBHelper(context); } return instance; } private DBHelper(Context context){ super(context, DB_NAME, null, VERSION); } @Override public void onCreate(SQLiteDatabase db) { StringBuffer tableCreate = new StringBuffer(); tableCreate.append("create table user(_id integer primary key autoincrement,") .append("name text,") .append("mobilephone text,") .append("familyphone text,") .append("officephone text,") .append("address text,") .append("position text,") .append("company text,") .append("email text,") .append("zipcode text,") .append("remark text,") .append("othercontact text,") .append("imageid int)"); db.execSQL(tableCreate.toString()); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { String sql = "drop table if exists user"; db.execSQL(sql); onCreate(db); } //db private void openDatabase(){ if(db ==null){ //查系统,没有就调用onCreate方法,第二次就不会调用了 db = this.getWritableDatabase(); } } public long saveUser(User user){ openDatabase(); //打开.SQLiteDatabase ContentValues cv = new ContentValues(); //相当与hashmap cv.put("name", user.name); cv.put("address", user.address); cv.put("company", user.company); cv.put("email", user.email); cv.put("familyphone", user.familyphone); cv.put("mobilephone", user.mobilePhone); cv.put("officephone", user.officePhone); cv.put("othercontact", user.otherContact); cv.put("position", user.position); cv.put("remark", user.remark); cv.put("zipcode", user.zipCode); cv.put("imageid", user.imageId); return db.insert("user", null, cv); //保存到数据库 } public ArrayList getUserList() { openDatabase(); //打开.SQLiteDatabase Cursor cursor = db.query("user", null, null, null, null, null, null); ArrayList list = new ArrayList(); while(cursor.moveToNext()){ HashMap map = new HashMap(); map.put("imageid",cursor.getInt(cursor.getColumnIndex("imageid"))); map.put("name",cursor.getString(cursor.getColumnIndex("name"))); map.put("mobilephone",cursor.getString(cursor.getColumnIndex("mobilephone"))); list.add(map); } return list; } }