下边演示一个使用ListView和自定义适配器的案例,点击ListView中的条目会出现一个对话框,进行成绩的修改,修改之后会立即通知适配器进行数据的重新加载,如下:
(1)、用于显示listView的布局文件:
<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:orientation="vertical"
android:padding="20dp" >
<ListView
android:id="@+id/course_student_manage"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
二、用于显示每一天信息的布局文件,就是每一个item选项,需要在适配器中使用到
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:padding="8dp" >
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="40dp" >
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:text="姓名"
android:textSize="20sp" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1" />
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:text="课程"
android:textSize="20sp" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1" />
<TextView
android:layout_width="40dp"
android:layout_height="40dp"
android:text="分数"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dp" >
<TextView
android:id="@+id/tv_name"
android:layout_width="80dp"
android:layout_height="50dp"
android:text=""
android:textSize="20sp" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_course"
android:layout_width="80dp"
android:layout_height="50dp"
android:text=""
android:textSize="20sp" />
<View
android:layout_width="wrap_content"
android:layout_height="1dp"
android:layout_weight="1" />
<TextView
android:id="@+id/tv_score"
android:layout_width="40dp"
android:layout_height="40dp"
android:text=""
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
效果如下:
(3)对listview的控制类:这里边有一些加载数据的方法,根据具体的情况修改之就可以
public class ManageStudentScoreActivity extends Activity implements
OnItemClickListener {
private List<Course> courseList;
private ListView course_student_manage;
private CourseAdapter adapter;
private EditText update_score;
private Button btn_ok;
private String studentName;
private String courseName;
private AlertDialog dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.course_studen_manage);
course_student_manage = (ListView) this
.findViewById(R.id.course_student_manage);
// 加载数据
courseList = loadCourseData();
if (courseList != null) {
adapter = new CourseAdapter(this, courseList);
} else {
ToastUtil.showToast(this, "没有学生选课信息!");
}
course_student_manage.setAdapter(adapter);
course_student_manage.setOnItemClickListener(this);
}
/**
* 查询所有学生信息
*
* @return
*/
protected List<Course> loadCourseData() {
CourseInter courseInter = new CourseImpl();
return courseInter.getCourseDetail();
}
/**
* listview的点击事件
*/
public void onItemClick(AdapterView<?> adapter, View view, int position,
long id) {
//这里就是创建一个弹出的对话框
AlertDialog.Builder builder = new AlertDialog.Builder(this);
// 加载布局文件
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View dialogView = inflater.inflate(R.layout.dialog_update_score, null);
update_score = (EditText) dialogView.findViewById(R.id.update_score);
btn_ok = (Button) dialogView.findViewById(R.id.btn_ok);
builder.setView(dialogView);
builder.create();
dialog = builder.show(); //一定要show
// 获得每一个item的条目学生姓名 课程姓名 和分数
View adapterView = view;
TextView tv_score = (TextView) adapterView.findViewById(R.id.tv_score);
TextView tv_name = (TextView) adapterView.findViewById(R.id.tv_name);
TextView tv_course = (TextView) adapterView.findViewById(R.id.tv_course);
studentName = tv_name.getText().toString();
courseName = tv_course.getText().toString();
btn_ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String scoreString = update_score.getText().toString();
float score = Float.parseFloat(scoreString);
// 更改成绩
updateCourseDetail(studentName, courseName, score);
}
});
// dialog.dismiss();
}
/**
* 更新学生成绩信息的
*
* @param studentName
* @param courseName
* @param score
*/
protected void updateCourseDetail(String studentName, String courseName,
float score) {
CourseInter courseInter = new CourseImpl();
if (courseInter.updateStudentCourse(studentName, courseName, score)) {
ToastUtil.showToast(ManageStudentScoreActivity.this, "修改成功!");
//取消弹出的对话框
dialog.dismiss();
// 通知适配器数据已经变化
adapter.notifyDataSetChanged();
} else {
ToastUtil.showToast(ManageStudentScoreActivity.this, "修改失败!");
}
}
}
(4)自定义适配器如下:为了可以使适配器可以自动刷新,一定要继承的方法
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
具体的适配器代码:
public class CourseAdapter extends BaseAdapter {
private Context context;
private List<Course> coursetList;
public CourseAdapter(Context context, List<Course> coursetList) {
this.context = context;
this.coursetList = coursetList;
}
public int getCount() {
return coursetList.size();
}
public Object getItem(int position) {
return coursetList.get(position);
}
public long getItemId(int position) {
return position;
}
/**
* 通知adapter更新数据
*/
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
}
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
//这里加载的每一个item条目的布局文件
convertView = LayoutInflater.from(context).inflate(
R.layout.student_score_item, null);
}
TextView tv_name = (TextView) convertView.findViewById(R.id.tv_name);
TextView tv_course = (TextView) convertView
.findViewById(R.id.tv_course);
TextView tv_score = (TextView) convertView.findViewById(R.id.tv_score);
// 获得某一个位置的student
Course course = coursetList.get(position);
tv_name.setText(course.getStudentName() + "");
tv_course.setText(course.getCourseName() + "");
tv_score.setText(course.getCourseSocre() + "");
return convertView;
}
}
ok!到此完成。