动态刷新ListView
来源:互联网 发布:办公室网络安装 编辑:程序博客网 时间:2024/06/01 09:36
要刷新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() {@Overridepublic 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 {@Overridepublic int getCount() {return students.size();}@Overridepublic Object getItem(int position) {return students.get(position);}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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;}}}
运行效果:
0 0
- ListView内容动态刷新
- ListView动态刷新
- notifyDataSetChanged 动态刷新listview
- ListView数据动态刷新
- ListView动态刷新
- JQueryMobile listview动态刷新
- 动态刷新Listview
- ListView数据动态刷新
- 动态刷新ListView
- ListView内容动态刷新
- ListView数据动态刷新
- 动态刷新listview数据
- 动态刷新listview中的数据
- Android透明界面与ListView动态刷新
- Android透明界面与ListView动态刷新
- Android动态刷新listview中的数据
- ListView内容变化后的动态刷新
- 自定义listView及其adapter动态刷新
- Android搜索关键字飞入飞出效果
- CC_11_排序与查找
- POJ Intervals (最小费用最大流)
- 创建一个带有CheckBox的TreeView
- 面向对象
- 动态刷新ListView
- 一种VS2010配置OPENCV的方法
- 图的邻接表的建立 c++
- 找数组中第二大的数字
- [LeetCode] Median of Two Sorted Arrays
- 获取(多)屏幕尺寸--C++
- smail语法简析(1)
- POJ1426_Find The Multiple_测试出真知
- Dword、LPSTR、LPWSTR、LPCSTR、LPCWSTR、LPTSTR、LPCTSTR