public class MainActivity extends Activity {
private ListView listview;
private SimpleAdapter adapter;
private Sqlite_operate_utils sql;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.listview = (ListView) this.findViewById(R.id.listview);
sql = new Sqlite_operate_utils();
// 注册菜单
registerForContextMenu(listview);
adapter();
}
// 创建一个选择菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.add_menu, menu);
return super.onCreateOptionsMenu(menu);
}
// 创建 一个上下文菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
// 设置选中的 名字 为 菜单标题
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
Map<String, String> info_map = (Map<String, String>) adapter
.getItem(info.position);
menu.setHeaderTitle(info_map.get("s_sex"));
getMenuInflater().inflate(R.menu.list_menu, menu);
}
// 山下文 点击事件
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
Map<String, String> item_map = (Map<String, String>) adapter
.getItem(info.position);
// 拿到 选中的id值
final int id = Integer.parseInt(item_map.get("s_id"));
// 选择监听
switch (item.getItemId()) {
case R.id.list_delete:// 删除信息 点击事件
// 弹出提示对话框
AlertDialog.Builder builder = dialog(R.drawable.ic_launcher, "提示:");
builder.setMessage("确定要删除");
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String execute_content = "delete from studentinfo where s_id = ?";
Object[] bindArgs = { id };
boolean result = sql.executeData(execute_content, bindArgs);
if (result) {
Toast.makeText(MainActivity.this, "删除信息成功",
Toast.LENGTH_SHORT).show();
adapter();// 更新listview
} else {
Toast.makeText(MainActivity.this, "删除失败",
Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
break;
case R.id.list_change:
// 按下修改信息 菜单 时 弹出 修改 编辑框
AlertDialog.Builder builder2 = dialog(R.drawable.ic_launcher, "提示:");
View view = getLayoutInflater().inflate(R.layout.editdata_activity,
null);
final EditText edit_name = (EditText) view
.findViewById(R.id.edit_name);
final EditText edit_sex = (EditText) view
.findViewById(R.id.edit_sex);
final EditText edit_age = (EditText) view
.findViewById(R.id.edit_age);
builder2.setView(view);
//把选择的列表信息 填充到 要修改的 编辑框里
String content = "select * from studentinfo where s_id = ?";
String[] condition = {id + ""};
Cursor cursor_student = sql.sqlite_select(content, condition);//查询出选中的 信息
cursor_student.moveToFirst();//把游标 移到第一个位置
edit_name.setText(cursor_student.getString(cursor_student.getColumnIndex("s_sex")));
edit_sex.setText(cursor_student.getString(cursor_student.getColumnIndex("s_age")));
edit_age.setText(cursor_student.getString(cursor_student.getColumnIndex("s_score")));
//关闭 游标
cursor_student.close();
//确定 修改 信息 按钮 事件 监听
builder2.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = edit_name.getText().toString();
String sex = edit_sex.getText().toString();
String age = edit_age.getText().toString();
String execute_content = "update studentinfo set s_sex =?,s_age =?,s_score =? where s_id =?";
Object[] bindArgs = {name,sex,age,id};
// 把修改的数据 重新写入数据库
boolean result = sql.executeData(execute_content, bindArgs);
if (result) {
Toast.makeText(MainActivity.this, "修改信息成功",
Toast.LENGTH_SHORT).show();
adapter();// 更新listview
} else {
Toast.makeText(MainActivity.this, "修改失败",
Toast.LENGTH_SHORT).show();
}
}
});
builder2.show();
break;
}
return super.onContextItemSelected(item);
}
// 添加学生 数据
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.adddata_menu:
// 按下添加信息 菜单 时 弹出 添加 编辑框
AlertDialog.Builder builder = dialog(R.drawable.ic_launcher, "提示:");
View view = getLayoutInflater().inflate(R.layout.editdata_activity,
null);
final EditText edit_name = (EditText) view
.findViewById(R.id.edit_name);
final EditText edit_sex = (EditText) view
.findViewById(R.id.edit_sex);
final EditText edit_age = (EditText) view
.findViewById(R.id.edit_age);
builder.setView(view);
builder.setPositiveButton("确定", new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String name = edit_name.getText().toString();
String sex = edit_sex.getText().toString();
String age = edit_age.getText().toString();
// SQL语句 添加 数据
String execute_content = "insert into studentinfo(s_sex,s_age,s_score) values(?,?,?)";
Object[] bindArgs = { name, sex, age };
// 把添加 的数据 写入 数据库
boolean result = sql.executeData(execute_content, bindArgs);
if (result) {
Toast.makeText(MainActivity.this, "添加信息成功",
Toast.LENGTH_SHORT).show();
adapter();// 更新listview
} else {
Toast.makeText(MainActivity.this, "添加失败",
Toast.LENGTH_SHORT).show();
}
}
});
builder.show();
break;
}
return super.onOptionsItemSelected(item);
}
// Dialog 框
public AlertDialog.Builder dialog(int icon, String title) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(title);
builder.setIcon(icon);
builder.setNeutralButton("取消", null);
return builder;
}
// 设置 适配器 的方法
public void adapter() {
String content = "select * from studentinfo";
List<Map<String, String>> data = sql.sqlite_selectlist(content, null);
adapter = new SimpleAdapter(this, data, R.layout.item_activity,
new String[] { "s_sex", "s_age", "s_score" }, new int[] {
R.id.item_name, R.id.item_sex, R.id.item_age });
listview.setAdapter(adapter);
}
}