1 package com.hanqi.contacts; 2 3 import android.app.AlertDialog; 4 import android.content.DialogInterface; 5 import android.os.Bundle; 6 import android.support.v7.app.AppCompatActivity; 7 import android.view.ContextMenu; 8 import android.view.MenuItem; 9 import android.view.View; 10 import android.view.ViewGroup; 11 import android.widget.AdapterView; 12 import android.widget.BaseAdapter; 13 import android.widget.EditText; 14 import android.widget.ListView; 15 import android.widget.TextView; 16 import android.widget.Toast; 17 18 import com.hanqi.contacts.com.hanqi.contacts.orm.Contacts; 19 import com.hanqi.contacts.com.hanqi.contacts.orm.ContactsDAO; 20 21 import java.util.ArrayList; 22 23 public class MainActivity extends AppCompatActivity { 24 ListView lv_1; 25 ContactsDAO ctd = new ContactsDAO(this); 26 ArrayList act; 27 CTAdapter cta; 28 //长按数据的索引 29 int index; 30 31 @Override 32 protected void onCreate(Bundle savedInstanceState) { 33 super.onCreate(savedInstanceState); 34 setContentView(R.layout.activity_main); 35 36 lv_1 = (ListView)findViewById(R.id.lv_1); 37 38 lv_1.setOnCreateContextMenuListener(this); 39 //获取数据集合 40 act = ctd.getAll(); 41 //显示数据 42 cta = new CTAdapter(); 43 lv_1.setAdapter(cta); 44 45 } 46 //重写创建上下文菜单的方法 47 @Override 48 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { 49 super.onCreateContextMenu(menu, v, menuInfo); 50 menu.add(0, 1, 1, "修改"); 51 menu.add(0, 2, 2, "删除"); 52 //获取长按的数据信息 53 //1.得到菜单信息 54 AdapterView.AdapterContextMenuInfo acmi = 55 (AdapterView.AdapterContextMenuInfo)menuInfo; 56 //2.得到数据在集合中的索引 57 index = acmi.position; 58 } 59 //响应菜单点击的回调方法 60 @Override 61 public boolean onContextItemSelected(MenuItem item) { 62 switch (item.getItemId()) 63 { 64 case 1: 65 final View view = View.inflate(this,R.layout.contact_layout,null); 66 new AlertDialog.Builder(this) 67 .setTitle("添加通讯录") 68 .setView(view) 69 .setCancelable(false) 70 .setNegativeButton("取消", null) 71 .setPositiveButton("确定", new DialogInterface.OnClickListener() { 72 @Override 73 public void onClick(DialogInterface dialog, int which) { 74 EditText et_1 = (EditText)view.findViewById(R.id.et_1); 75 EditText et_2 = (EditText)view.findViewById(R.id.et_2); 76 Contacts contacts = act.get(index); 77 contacts.setName(et_1.getText().toString()); 78 contacts.setPhoneNumber(et_2.getText().toString()); 79 if(ctd.update(contacts)>0) 80 { 81 Toast.makeText(MainActivity.this, "修改成功", Toast.LENGTH_SHORT).show(); 82 cta.notifyDataSetChanged(); 83 } 84 else 85 { 86 Toast.makeText(MainActivity.this, "修改失败", Toast.LENGTH_SHORT).show(); 87 } 88 } 89 }) 90 .show(); 91 break; 92 case 2: 93 new AlertDialog.Builder(this) 94 .setTitle("确认对话框") 95 .setCancelable(false) 96 .setNegativeButton("取消",null) 97 .setPositiveButton("确认", new DialogInterface.OnClickListener() { 98 @Override 99 public void onClick(DialogInterface dialog, int which) {100 ctd.delete(act.get(index).getId());101 Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();102 act = ctd.getAll();103 cta.notifyDataSetChanged();104 }105 })106 .show();107 break;108 }109 return super.onContextItemSelected(item);110 }111 112 //BaseAdapter的实现类113 class CTAdapter extends BaseAdapter114 {115 @Override116 public int getCount() {117 return act.size();118 }119 120 @Override121 public Object getItem(int position) {122 return act.get(position);123 }124 125 @Override126 public long getItemId(int position) {127 return act.get(position).getId();128 }129 130 @Override131 public View getView(int position, View convertView, ViewGroup parent) {132 //得到数据133 Contacts contacts = act.get(position);134 //得到视图135 if (convertView == null)136 {137 convertView = View.inflate(MainActivity.this,R.layout.contact1_layout,null);138 }139 //视图和数据做显式匹配140 TextView textView = (TextView)convertView.findViewById(R.id.tv_1);141 textView.setText(contacts.getName());142 TextView textView1 = (TextView)convertView.findViewById(R.id.tv_2);143 textView1.setText(contacts.getPhoneNumber());144 return convertView;145 }146 }147 public void add_onClick(View v)148 {149 final View view = View.inflate(this,R.layout.contact_layout,null);150 new AlertDialog.Builder(this)151 .setTitle("添加通讯录")152 .setView(view)153 .setCancelable(false)154 .setNegativeButton("取消", null)155 .setPositiveButton("保存", new DialogInterface.OnClickListener() {156 @Override157 public void onClick(DialogInterface dialog, int which) {158 EditText et_1 = (EditText)view.findViewById(R.id.et_1);159 EditText et_2 = (EditText)view.findViewById(R.id.et_2);160 Contacts contacts = new Contacts(et_1.getText().toString(), et_2.getText().toString());161 long l = ctd.insert(contacts);162 if (l > 0) {163 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();164 act = ctd.getAll();165 cta.notifyDataSetChanged();166 } else {167 Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();168 }169 }170 })171 .show();172 }173 }