之前一直困扰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=; i<; 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+){
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 = ;
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 = ;
}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>

Android 动态刷新listview中的数据的更多相关文章

  1. C#中清空ListView中的数据

    我的显示数据的方式通过button按钮点击事件,当点击之后查询数据库库并将数据显示出来. 代码如下: private void button6_Click(object sender, EventAr ...

  2. c#导出ListView中的数据到Excel表格

    1.添加组件:Microsoft.Office.Interop.Excel 步骤:右键点击“引用”--->添加引用--->COM--->Microsoft.Office.Intero ...

  3. android代码优化----ListView中自定义adapter的封装(ListView的模板写法)

    [声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...

  4. 43.Android之ListView中BaseAdapter学习

    实际开发中个人觉得用的比较多是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView.GridView.Gallery.Spinner ...

  5. Android 关于ListView中按钮监听的优化问题(方法一)

    在Android应用开发过程中经常会用到ListView,并且每次在item中都要对点击事件进行监听.在给按钮添加OnClickListener时,一般会下意识的在getView()中找到每一个But ...

  6. 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)

    引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...

  7. 【Android】ListView中EditText焦点问题

    一.描述: 近期一个项目中需要开发一种类似表格的界面来显示和配置参数,Android并无直接类似表格的控件支持,我采用了ListView中布局EditText和TextView来实现,其中TextVi ...

  8. Android 实现ListView中Item被单击后背景色保持高亮

    今天为了解决一个需求,就是我有一个slidingDrawer,里面是一个ListView.然后,单击其中的Item,默认只是显示一个橙色背景后就恢复了.客户便有着个需求,需要单击这个Item的背景高亮 ...

  9. 42.Android之ListView中ArrayAdapter简单学习

    今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...

随机推荐

  1. .net 中连接mysql

    1. 下载mysql驱动.里面包含需要连接mysql的dll.mysql-connector-net    地址:http://dev.mysql.com/downloads/file/?id=463 ...

  2. java环境变量详解---找不到或无法加载主类

    默认安装在C:\ProgramFiles\Java\jdk1.7.0目录下环境变量配置为PATH=.;%JAVA_HOME%\binCLASSPATH=.;%JAVA_HOME%\lib\dt.jar ...

  3. 安装.NET Framework组件时,电脑意外重启后再次安装失败

    因为软件运行环境需要安装.Net Framework,我安装的是2.0sp版本,可以安装过程中计算机意外关闭,重新打开后再次安装却出现安装失败的提示,具体内容是: 产品: Microsoft .NET ...

  4. Ubuntu将程序图标加到启动器

    问题: Ubuntu中安装一些程序的时候图标可能没有放到启动器中,不方便使用. 解决问题: 因为FileZilla这个程序是直接解压缩之后便可以使用的,每次都需要到文件所在目录Filezilla/bi ...

  5. File Operation using SHFileOperation

    SHFILEOPSTRUCT Original link: http://winapi.freetechsecrets.com/win32/WIN32SHFILEOPSTRUCT.htm Refere ...

  6. [转载]浅析STL allocator

    本文转载自水目沾博客:http://www.cnblogs.com/zhuwbox/p/3699977.html   向大师致敬 一般而言,我们习惯的 C++ 内存配置操作和释放操作是这样的: 1 c ...

  7. HDOJ 1042 N! -- 大数运算

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1042 Problem Description Given an integer N(0 ≤ N ≤ 1 ...

  8. 第四章 Web表单

    4.1 跨站请求伪造保护 安装flask-wtf app = Flask(__name__) app.config['SECRET_KEY'] = 'hard to guess string' 密钥不 ...

  9. hdu 5652 India and China Origins 并查集+逆序

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5652 题意:一张n*m个格子的点,0表示可走,1表示堵塞.每个节点都是四方向走.开始输入初始状态方格, ...

  10. 树莓派 raspberry 入门之安装操作系统以及配置

    最近新入手一树莓派,型号是2代B,屏幕是微雪的7 inch c型 显示屏.下面来教大家怎么点亮树莓派. 第一步,装好显示器,显示器的电源接在树莓派的usb口上,HDMI口不多说,连上.然后装好鼠标.键 ...