Android 动态刷新listview中的数据
之前一直困扰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中的数据的更多相关文章
- C#中清空ListView中的数据
我的显示数据的方式通过button按钮点击事件,当点击之后查询数据库库并将数据显示出来. 代码如下: private void button6_Click(object sender, EventAr ...
- c#导出ListView中的数据到Excel表格
1.添加组件:Microsoft.Office.Interop.Excel 步骤:右键点击“引用”--->添加引用--->COM--->Microsoft.Office.Intero ...
- android代码优化----ListView中自定义adapter的封装(ListView的模板写法)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/4 ...
- 43.Android之ListView中BaseAdapter学习
实际开发中个人觉得用的比较多是BaseAdapter,尽管使用起来比其他适配器有些麻烦,但是使用它却能实现很多自己喜欢的列表布局,比如ListView.GridView.Gallery.Spinner ...
- Android 关于ListView中按钮监听的优化问题(方法一)
在Android应用开发过程中经常会用到ListView,并且每次在item中都要对点击事件进行监听.在给按钮添加OnClickListener时,一般会下意识的在getView()中找到每一个But ...
- 如何在Android的ListView中构建CheckBox和RadioButton列表(支持单选和多选的投票项目示例)
引言 我们在android的APP开发中有时候会碰到提供一个选项列表供用户选择的需求,如在投票类型的项目中,我们提供一些主题给用户选择,每个主题有若干选项,用户对这些主题的选项进行选择,然后提交. 本 ...
- 【Android】ListView中EditText焦点问题
一.描述: 近期一个项目中需要开发一种类似表格的界面来显示和配置参数,Android并无直接类似表格的控件支持,我采用了ListView中布局EditText和TextView来实现,其中TextVi ...
- Android 实现ListView中Item被单击后背景色保持高亮
今天为了解决一个需求,就是我有一个slidingDrawer,里面是一个ListView.然后,单击其中的Item,默认只是显示一个橙色背景后就恢复了.客户便有着个需求,需要单击这个Item的背景高亮 ...
- 42.Android之ListView中ArrayAdapter简单学习
今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...
随机推荐
- Java实现SSO
摘要:单点登录(SSO)的技术被越来越广泛地运用到各个领域的软件系统当中.本文从业务的角度分析了单点登录的需求和应用领域:从技术本身的角度分析了单点登录技术的内部机制和实现手段,并且给出Web-SSO ...
- [LaTex]Visio文件转EPS文件[转]
在LaTeX系统中,由于DVI 文件经常被转为PostScript 文件,所以LATEX 支持最好的是EPS 格式(Encapsulated Post-Script ,是PostScript 语言的子 ...
- WCF编程系列(一)初识WCF
WCF编程系列(一)初识WCF Windows Communication Foundation(WCF)是微软为构建面向服务的应用程序所提供的统一编程模型.WCF的基本概念: 地址:定义服务的 ...
- Jquery 1.11.1 Checkbox checked 判断
if($(this).prop('checked'))
- Kafka-0.10.0.0入门
搭建环境略(伪集群即可以),但要注意Kafka的配置必须配置的,少配了也一样可以用,但是只能单机使用,外部机器无法连接,网上也有说. host.name=192.168.1.30 advertised ...
- 第32条:用EnumSet代替位域
如果一个枚举类型的元素主要用在集合中,一般使用int枚举模式,将2的不同倍数赋予每个常量: public class Text { public static final int STYLE_BOLD ...
- [翻译][MVC 5 + EF 6] 11:实现继承
原文:Implementing Inheritance with the Entity Framework 6 in an ASP.NET MVC 5 Application 1.选择继承映射到数据库 ...
- async:false同步请求,浏览器假死
// 异步请求导致数据错乱 // function get_num(){ // $("input[name='monitor']").eq(1).attr('checked',tr ...
- Linux内核中的通用双向循环链表
开发中接触Linux越来越多,休息放松之余,免不了翻看翻看神秘的Linux的内核.看到双向链表时,觉得挺有意思的,此文记下. 作为众多基础数据结构中的一员,双向循环链表在各种“教科书”中的实现是相当的 ...
- 判断浏览器是否支持某个css属性
方法:直接判断浏览器是否支持某个CSS属性才是王道,document.documentElement.style 如:判断是否支持 transform if( 'MozTransform' in do ...