android简易的通讯录代码 主要用到了 数据库存储的知识点 还有menu
package com.example.song.lx_day13_contact;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private TextView empty;
private SQLiteDatabase db;
private Cursor cursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//设置空视图
listView.setEmptyView(empty);
registerForContextMenu(listView);
initDataBase();
initData();//listview赋值
}
private void initData() {
cursor = db.rawQuery("select * from contact", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, new String[]{ "name", "sex", "number"}, new int[]{ R.id.tv_name, R.id.tv_sex, R.id.tv_phoneNumber}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
//true 带自己创建菜单
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.mipmap.icon);
builder.setTitle("添加联系人");
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view, null);
builder.setView(view);
final EditText nameEditText = (EditText) view.findViewById(R.id.et_name);
final EditText sexEditText = (EditText) view.findViewById(R.id.et_sex);
final EditText phoneEditText = (EditText) view.findViewById(R.id.et_phone);
builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContentValues values = new ContentValues();
values.put("name", nameEditText.getText() + "");
values.put("sex", sexEditText.getText() + "");
values.put("number", phoneEditText.getText() + "");
long insert = db.insert("contact", null, values);
if (insert != -1) {//判断是否添加上联系人
initData();//跟新一下数据
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
return super.onOptionsItemSelected(item);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
getMenuInflater().inflate(R.menu.contextmenu_listview_main, menu);
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
// menu.setHeaderTitle("id:" + info.id);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
final long id = info.id;//单条对应的数据库
switch (item.getItemId()) {
case R.id.action_delete:
db.execSQL("delete from contact where _id =?", new Object[]{id});
initData();
break;
case R.id.action_update:
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("更新联系人");
builder.setIcon(R.mipmap.icon);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_view, null);
builder.setView(view);
final EditText nameEditText = (EditText) view.findViewById(R.id.et_name);
final EditText sexEditText = (EditText) view.findViewById(R.id.et_sex);
final EditText phoneEditText = (EditText) view.findViewById(R.id.et_phone);
Cursor cursor = db.rawQuery("select * from contact where _id = ?", new String[]{"" + id});
cursor.moveToFirst();
nameEditText.setText(cursor.getString(cursor.getColumnIndex("name")));
sexEditText.setText(cursor.getString(cursor.getColumnIndex("sex")));
phoneEditText.setText(cursor.getString(cursor.getColumnIndex("number")));
builder.setPositiveButton("修改", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
ContentValues contentValues = new ContentValues();
contentValues.put("name", nameEditText.getText()+"");
contentValues.put("sex", sexEditText.getText()+"");
contentValues.put("number", phoneEditText.getText()+"");
db.update("contact", contentValues, "_id=?", new String []{id+""});
initData();
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
break;
}
return super.onContextItemSelected(item);
}
private void initDataBase() {
OpenHelper helper = new OpenHelper(this);
db = helper.getReadableDatabase();
}
private void initView() {
listView = ((ListView) findViewById(R.id.lv));
empty = ((TextView) findViewById(R.id.tv_empty));
}
}
package com.example.song.lx_day13_contact;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by song on 2017/1/18.
*/
public class OpenHelper extends SQLiteOpenHelper {
public static final String NAME = "contacts.db";
public static final int VERSION = 1;
public OpenHelper(Context context) {
super(context, NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table if not exists contact(_id integer primary key autoincrement,name,sex,number)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
if (newVersion > oldVersion) {
db.execSQL("drop table if exists contact");
onCreate(db);
}
}
}
点击打开链接