先来看下项目主要内容:
ListView中填充数据:
重现添加数据后置顶,具体阐明了决解方案,如下:
刷新适配器后没有响应的错误现象,具体阐明了决解方案,如下:
正确示范一:
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990/**
* 正确示范一(正确运用,修改原始对象
对应引用 也改变) *
* @author johnny
*
*/
public
class
ThreeListViewActivity
extends
Activity {
private
ListView mContentLv;
private
OneAdapter adapter;
private
ArrayList
> arrayList= new
ArrayList
>(); @Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mContentLv = (ListView) findViewById(R.id.lv_content);
adapter=
new
OneAdapter(
this
,arrayList);
mContentLv.setAdapter(adapter);
initData();
ToastUtils.show(getApplicationContext(),
"6秒后延迟添加,刷新adapter"
);
//开启异步线程
setData();
}
private
void
setData() {
new
Thread(
new
Runnable() {
@Override
public
void
run() {
// TODO Auto-generated method stub
try
{
//做一些耗时的操作后添加数据,之后刷新adapter
Thread.sleep(
6000
);
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//可能这个时候才重网络上获取到了新数据,,现在开始添加数据
HashMap
hashMap; for
(
int
i =
0
; i <
5
; i++) {
hashMap =
new
HashMap
(); if
(i %
4
==
0
) {
hashMap.put(
"name"
,
"大海"
);
hashMap.put(
"address"
,
"上海"
);
}
else
if
(i %
4
==
1
) {
hashMap.put(
"name"
,
"老马"
);
hashMap.put(
"address"
,
"深圳"
);
}
else
if
(i %
4
==
2
) {
hashMap.put(
"name"
,
"小三"
);
hashMap.put(
"address"
,
"东莞"
);
}
else
if
(i %
4
==
3
) {
hashMap.put(
"name"
,
"老哥"
);
hashMap.put(
"address"
,
"北京"
);
}
arrayList.add(hashMap);
}
handler.sendEmptyMessage(
1
);
}
}).start();
}
Handler handler =
new
Handler(){
public
void
handleMessage(android.os.Message msg) {
adapter.notifyDataSetChanged();
//没有重现设置adapter,只做了刷新数据,listview不会到顶。
ToastUtils.show(getApplicationContext(),
"adapter共有"
+adapter.getAllList().size()+
"个数据,activity共有"
+arrayList.size()+
"个数据,刷新结束!"
);
};
};
private
void
initData() {
HashMap
hashMap; for
(
int
i =
0
; i <
15
; i++) {
hashMap =
new
HashMap
(); if
(i %
4
==
0
) {
hashMap.put(
"name"
,
"小明"
);
hashMap.put(
"address"
,
"上海"
);
}
else
if
(i %
4
==
1
) {
hashMap.put(
"name"
,
"老马"
);
hashMap.put(
"address"
,
"深圳"
);
}
else
if
(i %
4
==
2
) {
hashMap.put(
"name"
,
"小三"
);
hashMap.put(
"address"
,
"东莞"
);
}
else
if
(i %
4
==
3
) {
hashMap.put(
"name"
,
"老哥"
);
hashMap.put(
"address"
,
"北京"
);
}
arrayList.add(hashMap);
}
}
}
正确示范二:
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798/**
* 正确示范二(正确运用,Activity里面对象不用作Adapter原始对象,只做数据的备份。
* 这也是本人比较喜欢的写法,原因:很多时候我们会不经意间改变Activity里面的数据源后导致ListView的改变,
* 导致出错后排除错误难解<正确示范一,当然也有好处,比如:改变Activity数据后,不需要再做多余的Adapter数据的更改,方便>)
*
* @注意 这里只是数据添加方案本人赞成写法,具体完整结合adapter请移步个人推荐一或二<有错误示范,才能慢慢完善改变,错误何尝不是一种成长>
* @author johnny
*
*/
public
class
FourListViewActivity
extends
Activity {
private
ListView mContentLv;
private
OneAdapter adapter;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_listview);
mContentLv = (ListView) findViewById(R.id.lv_content);
adapter=
new
OneAdapter(
this
);
mContentLv.setAdapter(adapter);
initData();
ToastUtils.show(getApplicationContext(),
"6秒后延迟添加,刷新adapter"
);
//开启异步线程
setData();
}
private
void
setData() {
new
Thread(
new
Runnable() {
@Override
public
void
run() {
// TODO Auto-generated method stub
try
{
//做一些耗时的操作后添加数据,之后刷新adapter
Thread.sleep(
6000
);
}
catch
(InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ArrayList
> arrayList= new
ArrayList
>(); //可能这个时候才重网络上获取到了新数据,,现在开始添加数据
HashMap
hashMap; for
(
int
i =
0
; i <
5
; i++) {
hashMap =
new
HashMap
(); if
(i %
4
==
0
) {
hashMap.put(
"name"
,
"大海"
);
hashMap.put(
"address"
,
"上海"
);
}
else
if
(i %
4
==
1
) {
hashMap.put(
"name"
,
"老马"
);
hashMap.put(
"address"
,
"深圳"
);
}
else
if
(i %
4
==
2
) {
hashMap.put(
"name"
,
"小三"
);
hashMap.put(
"address"
,
"东莞"
);
}
else
if
(i %
4
==
3
) {
hashMap.put(
"name"
,
"老哥"
);
hashMap.put(
"address"
,
"北京"
);
}
//这里添加的只是在临时数据存储的对象,adapter里面不属于同一个对象
arrayList.add(hashMap);
}
//和正确范例一区别在于此,每次都需要把数据copy到adapter里面
adapter.setAddList(arrayList);
handler.sendEmptyMessage(
1
);
}
}).start();
}
Handler handler =
new
Handler(){
public
void
handleMessage(android.os.Message msg) {
adapter.notifyDataSetChanged();
//没有重现设置adapter,只做了刷新数据,ListView不会到顶。
ToastUtils.show(getApplicationContext(),
"adapter共有"
+adapter.getAllList().size()+
"个数据。"
);
};
};
private
void
initData() {
ArrayList
> arrayList= new
ArrayList
>(); HashMap
hashMap; for
(
int
i =
0
; i <
15
; i++) {
hashMap =
new
HashMap
(); if
(i %
4
==
0
) {
hashMap.put(
"name"
,
"小明"
);
hashMap.put(
"address"
,
"上海"
);
}
else
if
(i %
4
==
1
) {
hashMap.put(
"name"
,
"老马"
);
hashMap.put(
"address"
,
"深圳"
);
}
else
if
(i %
4
==
2
) {
hashMap.put(
"name"
,
"小三"
);
hashMap.put(
"address"
,
"东莞"
);
}
else
if
(i %
4
==
3
) {
hashMap.put(
"name"
,
"老哥"
);
hashMap.put(
"address"
,
"北京"
);
}
arrayList.add(hashMap);
}
adapter.setAddList(arrayList);
}
}
ListView 适配器推荐写法:
- 123456789101112131415161718192021222324252627282930313233
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View 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 =
new
ViewHolder(v);
v.setTag(holder);
}
HashMap
hashMap = getItem(position); holder.mNameTv.setText(hashMap.get(
"name"
));
holder.mAddressTv.setText(hashMap.get(
"address"
));
return
v;
}
方法二:
-
12345678910111213141516171819
@Override
public
View getView(
int
position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
View 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
hashMap = getItem(position); mNameTv.setText(hashMap.get(
"name"
));
mAddressTv.setText(hashMap.get(
"address"
));
return
v;
}
ListView中疑难杂症:
只做截图具体下载代码查看:不需要豆子
另链接单选删除帖子效果如下:
地址:点击进入
×××:
ListView中常用知识总结