/>
创建 listView 中显示学员信息的 xml 格式 student_list_item.xml
?
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"/>
创建一个 StudentListActivity 做为主页显示学员信息以及进行一些操作。
package cn.yj3g.student.activity;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
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.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListene
r;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import cn.yj3g.student.dao.StudentDao;
import cn.yj3g.student.db.StudentDBHelper;
import cn.yj3g.student.entry.Student;
import cn.yj3g.student.entry.TableContanst;
public class StudentListActivity extends ListActivity impl
ements
OnClickListener, OnItemClickListener, OnItemLongCli
ckListener {
private static final String TAG = "TestSQLite";
private Button addStudent;
private Cursor cursor;
private SimpleCursorAdapter adapter;
private ListView listView;
private List list;
private RelativeLayout relativeLayout;
private Button searchButton;
private Button selectButton;
private Button deleteButton;
private Button selectAllButton;
private Button canleButton;
private LinearLayout layout;
private StudentDao dao;
private Student student;
private Boolean isDeleteList = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.e(TAG, "onCreate");
list = new ArrayList();
student = new Student();
dao = new StudentDao(new StudentDBHelper(this));
addStudent = (Button) findViewById(R.id.btn_add_stu
searchButton = (Button) findViewById(R.id.bn_search
selectButton = (Button) findViewById(R.id.bn_selec
deleteButton = (Button) findViewById(R.id.bn_delet
selectAllButton = (Button) findViewById(R.id.bn_sel
dent);
_id);
t);
e);
ectall);
r);
canleButton = (Button) findViewById(R.id.bn_canel);
layout = (LinearLayout) findViewById(R.id.showLine
relativeLayout=(RelativeLayout) findViewById(R.id.R
elativeLayout);
listView = getListView();
// 为按键设置监听
addStudent.setOnClickListener(this);
searchButton.setOnClickListener(this);
selectButton.setOnClickListener(this);
deleteButton.setOnClickListener(this);
canleButton.setOnClickListener(this);
selectAllButton.setOnClickListener(this);
listView.setOnItemClickListener(this);
listView.setOnItemLongClickListener(this);
listView.setOnCreateContextMenuListener(this);
}
@Override
protected void onStart() {
// 调用 load()方法将数据库中的所有记录显示在当前页面
super.onStart();
load();
}
public void onClick(View v) {
// 跳转到添加信息的界面
if (v == addStudent) {
startActivity(new Intent(this, AddStudentActivit
y.class));
} else if (v == searchButton) {
// 跳转到查询界面
startActivity(new Intent(this, StudentSearch.cla
} else if (v == selectButton) {
ss));
// 跳转到选择界面
isDeleteList = !isDeleteList;
if (isDeleteList) {
checkOrClearAllCheckboxs(true);
} else {
showOrHiddenCheckBoxs(false);
} else if (v == deleteButton) {
// 删除数据
if (list.size() > 0) {
}
}
for (int i = 0; i < list.size(); i++) {
long id = list.get(i);
Log.e(TAG, "delete id=" + id);
int count = dao.deleteStudentById(id);
}
dao.closeDB();
load();
} else if (v == canleButton) {
// 点击取消,回到初始界面
load();
layout.setVisibility(View.GONE);
isDeleteList = !isDeleteList;
} else if (v == selectAllButton) {
// 全选,如果当前全选按钮显示是全选,则在点击后变为取消全
选,如果当前为取消全选,则在点击后变为全选
selectAllMethods();
}
}
// 创建菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View
ContextMenuInfo menuInfo) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.layout.menu, menu);
}
v,
// 对菜单中的按钮添加响应时间
@Override
public boolean onContextItemSelected(MenuItem item) {
int item_id = item.getItemId();
student = (Student) listView.getTag();
Log.v(TAG, "TestSQLite++++student+" + listView.getT
ag() + "");
final long student_id = student.getId();
Intent intent = new Intent();
// Log.v(TAG, "TestSQLite+++++++id"+student_id);
switch (item_id) {
// 添加
case R.id.add:
startActivity(new Intent(this, AddStudentActivit
y.class));
break;
// 删除
case R.id.delete:
deleteStudentInformation(student_id);
break;
case R.id.look: