先来看下项目主要内容:



ListView中填充数据:

重现添加数据后置顶,具体阐明了决解方案,如下:


刷新适配器后没有响应的错误现象,具体阐明了决解方案,如下:



正确示范一:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990/*** 正确示范一(正确运用,修改原始对象<Activity里面>对应引用<Adapter里面>也改变)** @author johnny**/publicclassThreeListViewActivity extendsActivity {privateListView mContentLv;privateOneAdapter adapter;privateArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_listview);mContentLv = (ListView) findViewById(R.id.lv_content);adapter=newOneAdapter(this,arrayList);mContentLv.setAdapter(adapter);initData();ToastUtils.show(getApplicationContext(), "6秒后延迟添加,刷新adapter");//开启异步线程setData();}privatevoidsetData() {newThread(newRunnable() {@Overridepublicvoidrun() {// TODO Auto-generated method stubtry{//做一些耗时的操作后添加数据,之后刷新adapterThread.sleep(6000);} catch(InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}//可能这个时候才重网络上获取到了新数据,,现在开始添加数据HashMap<String, String> hashMap;for(inti = 0; i < 5; i++) {hashMap = newHashMap<String, String>();if(i % 4== 0) {hashMap.put("name", "大海");hashMap.put("address", "上海");} elseif(i % 4== 1) {hashMap.put("name", "老马");hashMap.put("address", "深圳");} elseif(i % 4== 2) {hashMap.put("name", "小三");hashMap.put("address", "东莞");} elseif(i % 4== 3) {hashMap.put("name", "老哥");hashMap.put("address", "北京");}arrayList.add(hashMap);}handler.sendEmptyMessage(1);}}).start();}Handler handler = newHandler(){publicvoidhandleMessage(android.os.Message msg) {adapter.notifyDataSetChanged();//没有重现设置adapter,只做了刷新数据,listview不会到顶。ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"个数据,activity共有"+arrayList.size()+"个数据,刷新结束!");};};privatevoidinitData() {HashMap<String, String> hashMap;for(inti = 0; i < 15; i++) {hashMap = newHashMap<String, String>();if(i % 4== 0) {hashMap.put("name", "小明");hashMap.put("address", "上海");} elseif(i % 4== 1) {hashMap.put("name", "老马");hashMap.put("address", "深圳");} elseif(i % 4== 2) {hashMap.put("name", "小三");hashMap.put("address", "东莞");} elseif(i % 4== 3) {hashMap.put("name", "老哥");hashMap.put("address", "北京");}arrayList.add(hashMap);}}}


正确示范二:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798/*** 正确示范二(正确运用,Activity里面对象不用作Adapter原始对象,只做数据的备份。* 这也是本人比较喜欢的写法,原因:很多时候我们会不经意间改变Activity里面的数据源后导致ListView的改变,* 导致出错后排除错误难解<正确示范一,当然也有好处,比如:改变Activity数据后,不需要再做多余的Adapter数据的更改,方便>)** @注意 这里只是数据添加方案本人赞成写法,具体完整结合adapter请移步个人推荐一或二<有错误示范,才能慢慢完善改变,错误何尝不是一种成长>* @author johnny**/publicclassFourListViewActivity extendsActivity {privateListView mContentLv;privateOneAdapter adapter;@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_listview);mContentLv = (ListView) findViewById(R.id.lv_content);adapter=newOneAdapter(this);mContentLv.setAdapter(adapter);initData();ToastUtils.show(getApplicationContext(), "6秒后延迟添加,刷新adapter");//开启异步线程setData();}privatevoidsetData() {newThread(newRunnable() {@Overridepublicvoidrun() {// TODO Auto-generated method stubtry{//做一些耗时的操作后添加数据,之后刷新adapterThread.sleep(6000);} catch(InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();//可能这个时候才重网络上获取到了新数据,,现在开始添加数据HashMap<String, String> hashMap;for(inti = 0; i < 5; i++) {hashMap = newHashMap<String, String>();if(i % 4== 0) {hashMap.put("name", "大海");hashMap.put("address", "上海");} elseif(i % 4== 1) {hashMap.put("name", "老马");hashMap.put("address", "深圳");} elseif(i % 4== 2) {hashMap.put("name", "小三");hashMap.put("address", "东莞");} elseif(i % 4== 3) {hashMap.put("name", "老哥");hashMap.put("address", "北京");}//这里添加的只是在临时数据存储的对象,adapter里面不属于同一个对象arrayList.add(hashMap);}//和正确范例一区别在于此,每次都需要把数据copy到adapter里面adapter.setAddList(arrayList);handler.sendEmptyMessage(1);}}).start();}Handler handler = newHandler(){publicvoidhandleMessage(android.os.Message msg) {adapter.notifyDataSetChanged();//没有重现设置adapter,只做了刷新数据,ListView不会到顶。ToastUtils.show(getApplicationContext(), "adapter共有"+adapter.getAllList().size()+"个数据。");};};privatevoidinitData() {ArrayList<HashMap<String, String>> arrayList=newArrayList<HashMap<String,String>>();HashMap<String, String> hashMap;for(inti = 0; i < 15; i++) {hashMap = newHashMap<String, String>();if(i % 4== 0) {hashMap.put("name", "小明");hashMap.put("address", "上海");} elseif(i % 4== 1) {hashMap.put("name", "老马");hashMap.put("address", "深圳");} elseif(i % 4== 2) {hashMap.put("name", "小三");hashMap.put("address", "东莞");} elseif(i % 4== 3) {hashMap.put("name", "老哥");hashMap.put("address", "北京");}arrayList.add(hashMap);}adapter.setAddList(arrayList);}}



ListView 适配器推荐写法:


123456789101112131415161718192021222324252627282930313233@OverridepublicView getView(intposition, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView v = convertView;//判断是否为null,这是每个要做复用的都要写的if(v == null) {v = inflater.inflate(R.layout.lv_item, null);}//相比之下//写法太集中,没有细化/*if (null == convertView) {holder = new ViewHolder();convertView = inflater.inflate(R.layout.lv_item, null);holder.mNameTv = (TextView) convertView.findViewById(R.id.tv_name);holder.mAddressTv = (TextView) convertView.findViewById(R.id.tv_address);convertView.setTag(holder);} else {holder = (ViewHolder) convertView.getTag();}*///简洁,把初始化控件放到了ViewHolder里面,是的getView方法更清晰简洁,只需要做数据的赋值和复杂的逻辑,没有混为一滩ViewHolder holder = (ViewHolder) v.getTag(); if(holder == null) {holder = newViewHolder(v);v.setTag(holder);}HashMap<String, String> hashMap = getItem(position);holder.mNameTv.setText(hashMap.get("name"));holder.mAddressTv.setText(hashMap.get("address"));returnv;}



方法二:

12345678910111213141516171819@OverridepublicView getView(intposition, View convertView, ViewGroup parent) {// TODO Auto-generated method stubView v = convertView;//判断是否为null,这是每个要做复用的都要写的if(v == null) {v = inflater.inflate(R.layout.lv_item, null);}//简洁,方便,快速TextView mNameTv=ViewHolder.get(v, R.id.tv_name); TextView mAddressTv=ViewHolder.get(v, R.id.tv_address); HashMap<String, String> hashMap = getItem(position);mNameTv.setText(hashMap.get("name"));mAddressTv.setText(hashMap.get("address"));returnv;}




ListView中疑难杂症:

只做截图具体下载代码查看:不需要豆子


e


另链接单选删除帖子效果如下:


地址:点击进入


×××:

ListView中常用知识总结