要刷新ListView,只需要调用其适配器的notifyDataSetChanged()方法即可。
下面的例子是在“ListView的例子”的基础上演化而言,本文仅给出变化的部分。
布局文件:增加了一个按钮,动态增加一个学生记录。
<?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="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/label_for_listview"/>
<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/add_student"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/add_student"
/>
</LinearLayout>
Java代码:对之前的代码进行了重构,增加了Button的消息处理,即增加一个学生记录,然后刷新ListView:
package com.example.hellolistview;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class MainActivity extends Activity {
private int student_item_id = R.layout.student_item;
private String[] columnNames = new String[] { "name", "score" };
private int[] ids = new int[] { R.id.student_name, R.id.student_score };
private ArrayList<HashMap<String, Object>> students = new ArrayList<HashMap<String, Object>>();
private int student_id = 0;
private Context context = null;
BaseAdapter adapter = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_listview);
context = this;
student_id = 0;
ListView listView = (ListView) this.findViewById(R.id.list_view);
createStudents();
//adapter = new SimpleAdapter(this, students, student_item_id, columnNames, ids);
adapter = new AnotherAdapter();
listView.setAdapter(adapter);
Button addStudent = (Button) this.findViewById(R.id.add_student);
addStudent.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
addStudent();
adapter.notifyDataSetChanged();
}
});
}
private void createStudents() {
for (int i = 1; i <= 10; i++) {
addStudent();
}
}
private void addStudent() {
HashMap<String, Object> map = null;
map = new HashMap<String, Object>();
map.put(columnNames[0], "student-" + student_id);
map.put(columnNames[1], String.valueOf(student_id * 10));
student_id++;
students.add(map);
}
public class AnotherAdapter extends BaseAdapter {
@Override
public int getCount() {
return students.size();
}
@Override
public Object getItem(int position) {
return students.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(R.layout.student_item, null);
}
TextView studentName = (TextView) convertView.findViewById(ids[0]);
TextView studentScore = (TextView) convertView.findViewById(ids[1]);
studentName.setText((CharSequence) students.get(position).get(columnNames[0]));
studentScore.setText((CharSequence) students.get(position).get(columnNames[1]));
return convertView;
}
}
}
运行效果: