动态刷新listview中的数据
来源:互联网 发布:淘宝店铺装修除了切图 编辑:程序博客网 时间:2024/06/05 16:25
转自:http://www.cnblogs.com/ProgramBull/archive/2012/02/06/2340401.html
之前一直困扰listview刷新后位置的问题,现在才明白,原来不能重新设置listview的adapter,而应该用notifyDataSetChanged()来刷新,这样位置就不会置顶。
下面做了一个测试的例子,点击最后一条记录,会增加一条新的记录。
代码如下:
1.TestAdapter
package com.TestAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.ViewGroup;import android.widget.AdapterView;import android.widget.AbsListView.OnScrollListener;import android.widget.AdapterView.OnItemClickListener;import android.widget.AdapterView.OnItemSelectedListener;import android.widget.AbsListView;import android.widget.ImageView;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.TextView;import android.widget.Toast;public class TestAdapter extends Activity { /** Called when the activity is first created. */ private int[] images ; private ListView listview; private MyAdapter adapter; List<Map<String,Integer>> al; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); images = new int[]{android.R.drawable.ic_btn_speak_now, android.R.drawable.alert_light_frame, android.R.drawable.arrow_down_float, android.R.drawable.arrow_up_float, android.R.drawable.btn_star_big_off, android.R.drawable.btn_star_big_on, android.R.drawable.button_onoff_indicator_off, android.R.drawable.button_onoff_indicator_on, android.R.drawable.checkbox_off_background, android.R.drawable.checkbox_on_background, android.R.drawable.ic_btn_speak_now, android.R.drawable.ic_delete}; listview = (ListView)findViewById(R.id.listview); al = new ArrayList<Map<String,Integer>>(); for(int i=0; i<12; i++){ HashMap<String,Integer > map = new HashMap<String,Integer>(); map.put(""+i, images[i]); al.add(map); } adapter = new MyAdapter(this, al, R.layout.list_item, new String[]{"imageview", "tv"}, new int[]{R.id.imageview, R.id.tv}); listview.setAdapter(adapter); listview.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> arg0, View arg1, int count, long arg3) { if(adapter.getCount()==count+1){ HashMap<String ,Integer> map = new HashMap<String, Integer>(); map.put(""+(adapter.mItemList.size()), android.R.drawable.ic_dialog_email); al.add(map); adapter.mItemList = al; adapter.notifyDataSetChanged(); Toast.makeText(TestAdapter.this, "正在刷新", Toast.LENGTH_SHORT).show(); } } }); } private class MyAdapter extends SimpleAdapter{ int count = 0; private List<Map<String, Integer>> mItemList; public MyAdapter(Context context, List<? extends Map<String, Integer>> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); mItemList = (List<Map<String, Integer>>) data; if(data == null){ count = 0; }else{ count = data.size(); } } public int getCount() { return mItemList.size(); } public Object getItem(int pos) { return pos; } public long getItemId(int pos) { return pos; } @Override public View getView(int position, View convertView, ViewGroup parent) { Map<String ,Integer> map = mItemList.get(position); int image = map.get(""+position); View view = super.getView(position, convertView, parent); ImageView imageview = (ImageView)view.findViewById(R.id.imageview); TextView tv = (TextView)view.findViewById(R.id.tv); imageview.setBackgroundResource(image); tv.setText(""+position); return view; } }}
2.main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" ></ListView> </LinearLayout>
3.list_item.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <ImageView android:id="@+id/imageview" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ImageView> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" ></TextView></LinearLayout>
- 动态刷新listview中的数据
- Android动态刷新listview中的数据
- Android动态刷新listview中的数据
- ListView数据动态刷新
- ListView数据动态刷新
- ListView数据动态刷新
- 动态刷新listview数据
- listView数据实现增删后动态刷新
- 动态刷新ListView中的某个item
- 动态加载listView中的数据
- ListView内容动态刷新
- ListView动态刷新
- notifyDataSetChanged 动态刷新listview
- ListView动态刷新
- JQueryMobile listview动态刷新
- 动态刷新Listview
- 动态刷新ListView
- ListView内容动态刷新
- live555 创建基本创建流程
- JDBC 连接数据库 PreparedStatement的使用
- 从CVPR2013看计算机视觉研究的三个趋势
- Objective-C数据类型和表达式
- 《Head First Python》学习笔记02 模块和函数参数缺省值
- 动态刷新listview中的数据
- 请使用PL/SQL来按父子层次关系查询出表的所有数据
- 笔记本输入字母时变成了字母解决方法
- POJ3635 周游诸城:变形的SPFA/Dijkstra最短路+动规思想+Heap
- QT信号与槽“带参数”
- Android内存使用研究
- 如何搜索出第N~M条记录?
- mysql 表数据的自我复制
- 黑马程序员_静态应用/封装/编写帮助文档