package com.vanceinfo.stus;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import com.vanceinfo.stus.StudentDbAdapter.DatabaseHelper;
public class ActivityStudents extends ListActivity implements
OnClickListener, OnItemClickListener, OnItemLongClickListener
{
private static final int ACTIVITY_CREATE = 0;
private static final int ACTIVITY_EDIT = 1;
private static final int ACTIVITY_QUERY = 2;
private ListView listView;
private StudentDbAdapter sDbAdapter;
private Cursor sStudentCursor;
private String test; //查询时输入的内容
private static final int INSERT_ID = Menu.FIRST;
private static final int DELETE_ID = Menu.FIRST + 1;
private static final int QUERY_ID = Menu.FIRST + 2;
private static final int INSERT_ID1 = Menu.FIRST + 3;
private static final int QUERY_ID1 = Menu.FIRST + 4;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainpara);
sDbAdapter = new StudentDbAdapter(this);
sDbAdapter.open();
listView = getListView();
getListView().addHeaderView(buildHeader());
getListView().addHeaderView(buildHeaderTitle());
getListView().addFooterView(buildFooter());
getListView().addFooterView(buildFooterRefresh());
renderListView(0);
listView.setOnCreateContextMenuListener(this);
listView.setOnItemLongClickListener(this);
}
//根据查询内容的不同调用不同查询语句
private void renderListView(int x)
{
switch (x)
{
case 0:
sStudentCursor = sDbAdapter.getAllStudents();
break;
case 1:
sStudentCursor = sDbAdapter.getAllStudentsByName();
break;
case 2:
sStudentCursor = sDbAdapter.getAllStudentsByNumber();
break;
case 3:
sStudentCursor = sDbAdapter.getStudentByName(test);
break;
case 4:
sStudentCursor = sDbAdapter.getStudentByNumber(test);
break;
default:
setTitle("出错了");
}
startManagingCursor(sStudentCursor);
String[] from = new String[] { StudentDbAdapter.KEY_NAME,
StudentDbAdapter.KEY_SEX , StudentDbAdapter.KEY_NUMBER };
int[] to = new int[] { R.id.name_text, R.id.sex_text, R.id.number_text};
SimpleCursorAdapter stu = new SimpleCursorAdapter(this,
R.layout.student_row, sStudentCursor, from, to);
setListAdapter(stu);
}
private View buildHeaderTitle() {
TextView tx = new TextView (this);
tx.setText("姓名 性别 学号 icon");
return tx;
}
//姓名排序 按钮 以及其监听器
private View buildHeader() {
Button btn1=new Button(this);
btn1.setText("按姓名排序");
btn1.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
renderListView(1);
}
});
return btn1;
}
private View buildFooter() {
Button btn2=new Button(this);
btn2.setText("按学号排序");
btn2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
renderListView(2);
}
});
return btn2;
}
//刷新按钮
private View buildFooterRefresh() {
Button btn2=new Button(this);
btn2.setText("全部学生信息");
btn2.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View v) {
renderListView(0);
}
});
return btn2;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(1,INSERT_ID, 1, "添加信息");
menu.add(1,DELETE_ID, 1, "删除信息");
menu.add(1, QUERY_ID, 1, "查询信息");
}
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
menu.add(0, INSERT_ID1, 0, "添加");
//menu.add(0, DELETE_ID, 0, "删除");
menu.add(0, QUERY_ID1, 0, "查询");
return true;
}
public boolean onMenuItemSelected(int featureId, MenuItem item){
{
switch (item.getItemId()) {
case INSERT_ID1:
createStudent ();
return true;
/*case DELETE_ID:
sDbAdapter.deleteStudent(getListView().getSelectedItemId());
renderListView(0);
return true;*/
case QUERY_ID1:
queryStudent ();
return true;
}
return super.onMenuItemSelected(featureId, item);}
}
//menu
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
.getMenuInfo();
int index = (int) info.id;
switch (item.getItemId()) {
case INSERT_ID:
createStudent ();
return true;
case DELETE_ID:
sDbAdapter.deleteStudent(index);
renderListView(0);
return true;
case QUERY_ID:
queryStudent ();
return true;
}
return super.onContextItemSelected(item);
}
private void queryStudent()
{
Intent i = new Intent(this, ActivityQuery.class);
startActivityForResult(i, ACTIVITY_QUERY);
//startActivity(i);
}
private void createStudent()
{
Intent i = new Intent(this, ActivityEdit.class);
startActivityForResult(i, ACTIVITY_CREATE);
}
/*// 删除数据
public void deleteData(String id){
DatabaseHelper dbHelper = new DatabaseHelper(ActivityStudents.this,sDbAdapter.DATABASE_NAME);
SQLiteDatabase sdb = dbHelper.getWritableDatabase();
sdb.delete(sDbAdapter.DATABASE_NAME, " _id=?", new String[]{id}); //删除数据
}
*//**
* 删除操作
*//*
public void alertDeleteOpt(final String id){
new AlertDialog.Builder(this)
.setCancelable(false)
.setMessage("您确定要删除这条信息吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteData(id);
}})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}})
.show();
}*/
// 需要对position和id进行一个很好的区分
// position指的是点击的这个ViewItem在当前ListView中的位置
// 每一个和ViewItem绑定的数据,肯定都有一个id,通过这个id可以找到那条数据。
protected void onListItemClick(ListView l, View v, int position, long id)
{
super.onListItemClick(l, v, position, id);
Cursor c = sStudentCursor;
c.moveToPosition(position-2); //将数据库中的id与实际位置相对应,由于添加Headerview导致实际位置+2
Intent i = new Intent(this, ActivityEdit.class);
i.putExtra(StudentDbAdapter.KEY_ROWID, id);
i.putExtra(StudentDbAdapter.KEY_NAME, c.getString(c
.getColumnIndexOrThrow(StudentDbAdapter.KEY_NAME)));
i.putExtra(StudentDbAdapter.KEY_SEX, c.getString(c
.getColumnIndexOrThrow(StudentDbAdapter.KEY_SEX)));
i.putExtra(StudentDbAdapter.KEY_NUMBER, c.getString(c
.getColumnIndexOrThrow(StudentDbAdapter.KEY_NUMBER)));
startActivityForResult(i, ACTIVITY_EDIT);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent)
{
renderListView(0);
if(resultCode ==ACTIVITY_QUERY)
{
test=intent.getStringExtra("num");
String radiobutton = intent.getStringExtra("selected");
if(radiobutton.equals("name"))
{
renderListView(3);
}else if(radiobutton.equals("number"))
{