package cn.yj3g.student.activity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import cn.yj3g.student.dao.StudentDao;
import cn.yj3g.student.db.StudentDBHelper;
import cn.yj3g.student.entity.Student;
import cn.yj3g.student.entity.TableContanst;
public class StudentListActivity extends ListActivity implements OnClickListener,
OnItemClickListener, OnItemLongClickListener {
private static final String TAG = "StudentListActivity";
private static final int DIALOG_DELETE_ALL_COMFIRM = 2;
private static final int DIALOG_DELETE_COMFIRM = 1;
protected static final int DIALOG_DELETE_PROGRESS = 3;
private static final int WHAT_FRESH_PROGRESS = 1;
private static final int WHAT_DISMISS_DIALOG = 2;
private StudentDao dao;
private Button addStudent;
private ImageView multiSelect;
private Button cancelBtn;
private Button deleteBtn;
private CheckBox selectAllBox;
private RelativeLayout headLayout;
private LinearLayout bottomLayout;
private ListView listView;
private boolean isListDelete = false;
private HashMap<Long, Boolean> checkBoxStatus;
private List<Long> selectIds;
private ProgressBar mProgressBar;
private AlertDialog mDialog;
private TextView mPercentSizeView;
private int mDeletedSize;
private static int mTotalSize;
private boolean delete = false;
private View selectedView = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
setContentView(R.layout.main);
dao = new StudentDao(new StudentDBHelper(this)); // this instanceof
// Context true
addStudent = (Button) findViewById(R.id.btn_add_student);
multiSelect = (ImageView) findViewById(R.id.iv_multi_select);
cancelBtn = (Button) findViewById(R.id.btn_cancel_delete);
deleteBtn = (Button) findViewById(R.id.btn_comfirm_delete);
selectAllBox = (CheckBox) findViewById(R.id.cb_select_all);
headLayout = (RelativeLayout) findViewById(R.id.rl_header_layout);
bottomLayout = (LinearLayout) findViewById(R.id.ll_bottom);
listView = getListView();
addStudent.setOnClickListener(this);
multiSelect.setOnClickListener(this);
cancelBtn.setOnClickListener(this);
deleteBtn.setOnClickListener(this);
selectAllBox.setOnClickListener(this);
// 点击列表项进入学员信息详情界面
listView.setOnItemClickListener(this);
// 长按列表项弹出上下文菜单
listView.setOnCreateContextMenuListener(this);
listView.setOnItemLongClickListener(this);
showListView(); // 显示listView
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
menu.add(1, 1, 1, "详细信息");
menu.add(1, 2, 1, "删除信息");
menu.add(1, 3, 1, "修改信息");
}
/**
* 点击上下文菜单项时被回调
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
int itemId = item.getItemId();
Student s = (Student) listView.getTag();
Intent intent = null;
switch (itemId) { // int 所有可以转换成int类型 shot byte char
case 1:
intent = new Intent(this, ShowStudentActivity.class);
intent.putExtra(TableContanst.STUDENT_TABLE, s);
startActivity(intent);
break;
case 2:
Bundle b = new Bundle();
b.putLong("id", s.getId());
showDialog(1,b);
break;
case 3:
intent = new Intent(this, AddStudentActivity.class);
intent.putExtra(TableContanst.STUDENT_TABLE, s);
//startActivity(intent);
startActivityForResult(intent, 1);
break;
default:
break;
}
return super.onContextItemSelected(item);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==1 && resultCode==2) {
Log.v(TAG, "onActivityResult");
Student student = (Student) data.getSerializableExtra(TableContanst.STUDENT_TABLE);
TextView nameView = (TextView) selectedView.findViewById(R.id.tv_item_name);
nameView.setText(student.getName());
//showListView(); // 显示listView
}
}
@Override
protected Dialog onCreateDialog(int code, Bundle args) {
Dialog dialog;
if(code==DIALOG_DELETE_COMFIRM) {
final long id = args.getLong("id");
dialog = new AlertDialog.Builder(this)
.setTitle("学员信息批量删除")
.setMessage("确认删除这些学员信息吗?")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//去数据库删除数据
int raws = dao.deleteStudentById(id);
String message = null;
if(raws>0) {
message = "id为"+id+"的记录被删除成功!";
showListView();
} else {
message = "删除失败,请重试!";
}
Toast.makeText(StudentListActivity.this, message, 0).show();
}
})
.setNegativeButton("取消", null).show();
} else if(code==DIALOG_DELETE_ALL_COMFIRM) {
//
}
return super.onCreateDialog(code, args);
}
private void showListView() {
List<Map<String, Object>> data = dao.getAllStudents();
String[] from = { TableContanst.StudentColumns.ID, TableContanst.StudentColumns.NAME,
TableContanst.StudentColumns.SEX, TableContanst.StudentColumns.LIKES,
TableContanst.StudentColumns.PHONE_NUMBER, TableContanst.StudentColumns.TRAIN_DATE,
TableContanst.StudentColumns.AGE, TableContanst.StudentColumns.MODIFY_TIME };
int[] to = { R.id.tv_item_id, R.id.tv_item_name, R.id.tv_item_sex, R.id.tv_item_likes,
R.id.tv_item_phone, R.id.tv_item_train_date, R.id.tv_item_age,
R.id.tv_item_modify_date };
SimpleAdapter adapter = new StudentAdpter(this, data, R.layout.student_list_item2, from, to);
listView.setAdapter(adapter);
}
public void onClick(View v) {
// 跳转到添加信息的界面
if(v==addStudent) {
startActivity(new Intent(this, AddStudentActivity.class));
} else if(v==multiSelect) {
//1 隐藏headView 2 显示bottomView 3 显示listView所有item的checkbox
showOrHiddenMultSelectUI(true);
} else if(v==cancelBtn) {
showOrHiddenMultSelectUI(false);
} else if(v==deleteBtn) {
deleteSeleteData();
//showDialog(DIALOG_DELETE_ALL_COMFIRM);
} else if (v==selectAllBox) {
checkOrcancelAllbox(selectAllBox.isChecked());
Log.v(TAG, "select box count="+selectIds.size());
Log.v(TAG, "select box status count="+checkBoxStatus.size());
}
}
/**
* 删除选中的记录
*/
private void deleteSeleteData() {
if(selectIds.size()>0) {
for(int i=0;i<selectIds.size();i++) {
long id = selectIds.get(i);
Log.e(TAG, "------delete id="+id);
dao.deleteStudentById(id);
}
dao.closeDB();
showOrHiddenMultSelectUI(false);
selectIds.clear();
checkBoxStatus.clear();
showListView();
}
}
private void clearAllboxs() {
for(int i=0;i<listView.getChildCount();i++) {
View view = listView.getChildAt(i);
if(view!=null) {
CheckBox cb = (CheckBox) view.findViewById(R.id.cb_item_delete);
cb.setChecked(false);
Log.v(TAG, "clearAllboxs i="+cb.isChecked
- 1
- 2
- 3
- 4
- 5
前往页