动态刷新ListView

本文介绍如何通过添加按钮动态更新ListView,并使用notifyDataSetChanged()方法刷新视图。示例代码展示了如何在Android应用中增加学生记录并实时更新列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

要刷新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;
		}
		
	}
}

运行效果:





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值