一、第一步:创建Activity
package com.xw.firstapp.shake;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import com.xw.firstapp.R;
import com.xw.firstapp.shake.db.StudentTableOperator;
public class AllRecordActivity extends Activity {
private StudentListViewAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_all_record);
initListView();
}
@Override
protected void onResume() {
super.onResume();
updateList();
}
private void initListView() {
adapter = new StudentListViewAdapter(this, null);
ListView l = (ListView) findViewById(R.id.aty_student_list);
l.setAdapter(adapter); //绑定adapter
}
private void updateList() {
//从数据库中查询出所有数据设置给adapter的list
adapter.setData(StudentTableOperator.getInstance(this).getAllStudents());
}
}
布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.xw.firstapp.shake.AllRecordActivity" >
<ListView
android:id="@+id/aty_student_list"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
二、第二步:创建数据模型item
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<LinearLayout
android:id="@+id/item_list_view_student_l"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/item_list_student_numbers"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学号:" />
<TextView
android:id="@+id/item_list_student_remarks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:maxLines="1"
android:text="备注:" />
</LinearLayout>
<TextView
android:layout_marginTop="10dp"
android:id="@+id/item_list_student_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="时间:" />
</LinearLayout>
三、第三步:创建一个实体类
package com.xw.firstapp.shake;
public class Student {
private int _id;
private String numbers;
private String remarks;
private String time;
public int get_Id() {
return _id;
}
public void set_Id(int _id) {
this._id = _id;
}
public String getNumbers() {
return numbers;
}
public void setNumbers(String numbers) {
this.numbers = numbers;
}
public String getRemarks() {
return remarks;
}
public void setRemarks(String remarks) {
this.remarks = remarks;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
四、第四步:创建adapter适配器
package com.xw.firstapp.shake;
import java.util.List;
import com.xw.firstapp.R;
import com.xw.firstapp.shake.db.StudentTableOperator;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
public class StudentListViewAdapter extends BaseAdapter implements
View.OnClickListener, View.OnLongClickListener {//实现单击和长按事件接口
private List<Student> students;
private Context context;
public StudentListViewAdapter(Context context, List<Student> students) {
this.context = context;
this.students = students;
}
public void setData(List<Student> students) {
this.students = students;
notifyDataSetChanged();
}
public List<Student> getStudents() {
return this.students;
}
public int getCount() {
return students == null ? 0 : students.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.item_list_view_student, null, false);
holder = new ViewHolder(convertView);
// 设置长按事件和点击事件监听器
holder.l.setOnClickListener((OnClickListener) this);
holder.l.setOnLongClickListener((OnLongClickListener) this);
convertView.setTag(holder);
}
holder = (ViewHolder) convertView.getTag();
Student student = students.get(position);
holder.tv_numbers.setText("学号:31671102" + student.getNumbers());
holder.tv_remarks.setText(student.getRemarks());
holder.tv_time.setText(student.getTime());
holder.l.setTag(position);
return convertView;
}
public void onClick(View v) {
int p = (Integer) v.getTag();
Intent intent = new Intent(context, ParticularsActivity.class);
intent.putExtra("id", students.get(p).get_Id());
context.startActivity(intent);
}
public boolean onLongClick(final View v) {
final int p = (Integer) v.getTag();
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("操作").setItems(new String[] { "删除记录" },
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:// 点击删除记录
int id = getStudents().get(p).get_Id();
long rows = StudentTableOperator.getInstance(
context).deleteById(id);
if (rows > 0) {
getStudents().remove(p);
notifyDataSetChanged();
}
break;
}
}
});
builder.create().show();
return true;
}
private class ViewHolder {
private TextView tv_numbers, tv_remarks, tv_time;
private LinearLayout l;
public ViewHolder(View v) {
tv_numbers = (TextView) v
.findViewById(R.id.item_list_student_numbers);
tv_remarks = (TextView) v
.findViewById(R.id.item_list_student_remarks);
tv_time = (TextView) v.findViewById(R.id.item_list_student_time);
l = (LinearLayout) v.findViewById(R.id.item_list_view_student_l);
}
}
}
本文介绍了一个基于Android的应用程序开发实例,该应用用于管理学生记录,包括创建Activity、定义数据模型、实现实体类及适配器等步骤。通过具体代码展示了如何在Android应用中展示并操作学生数据。
1534

被折叠的 条评论
为什么被折叠?



