ListView 加载数据 Holder 数据项重复 , 乱序

Winson_Zhou 2013-11-26 05:27:13
我用的是BaseAdapter:
代码如下:
 mBaseAdapter=new BaseAdapter() {

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
Myholder myHolder;
if(arg1 == null){
myHolder=new Myholder();
arg1=LayoutInflater.from(MainActivity.this).inflate(R.layout.item, null);
myHolder.text_one=(TextView) arg1.findViewById(R.id.tv_title);
myHolder.text_two=(TextView) arg1.findViewById(R.id.tv_content);
arg1.setTag(myHolder);

}else{
myHolder=(Myholder) arg1.getTag();
}
HashMap<String,String> s = list.get(arg0);
Log.i("Tag", " load position : "+arg0+" , title : "+s.get("ItemTitle")+" , text : "+s.get("ItemText")+" , view1 : "+myHolder.text_one+" , view2 : "+myHolder.text_two);
myHolder.text_one.setText(list.get(arg0).get("ItemTitle"));
myHolder.text_two.setText(list.get(arg0).get("ItemText"));

return arg1;
}

这个是getView的代码 ,
但是数据项的log 很诡异 , 当我将listView往下面拖的时候的log:
11-26 16:54:49.596: I/Tag(28093): load position : 17 , title : 第17行标题 , text : 第17行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:49.626: I/Tag(28093): load position : 18 , title : 第18行标题 , text : 第18行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:49.896: I/Tag(28093): load position : 19 , title : 第19行标题 , text : 第19行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:50.497: I/Tag(28093): load position : 20 , title : 第20行标题 , text : 第20行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:50.967: I/Tag(28093): load position : 21 , title : 第21行标题 , text : 第21行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:51.658: I/Tag(28093): load position : 22 , title : 第22行标题 , text : 第22行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:52.078: I/Tag(28093): load position : 23 , title : 第23行标题 , text : 第23行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:54.521: I/Tag(28093): load position : 24 , title : 第24行标题 , text : 第24行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:56.382: I/Tag(28093): load position : 25 , title : 第25行标题 , text : 第25行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:54:57.413: I/Tag(28093): load position : 26 , title : 第26行标题 , text : 第26行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:55:01.698: I/Tag(28093): load position : 27 , title : 第27行标题 , text : 第27行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
11-26 16:55:02.879: I/Tag(28093): load position : 28 , title : 第28行标题 , text : 第28行内容 , view1 : android.widget.TextView@4076c1b0 , view2 : android.widget.TextView@4076c930
...全文
319 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
junjun071308 2015-09-24
  • 打赏
  • 举报
回复
关于listView或者GridView这种乱序问题:我的解决办法是,在getView里面重用控件,if进行过的操作,要在else里面重新进行过一次,两者的操作 部分语句是完全一样的或者完全对立的,但是一定要去这么写。 为什么呢? 因为重用控件的原因,这个控件在getView里面拿到的时候,有两种可能: 1.他是刚刚被findViewById得到的,很好,他现在很纯,什么属性都是默认的,所以你要去给他设置你需要的控件属性 2.他是被重用的,嗯,这个时候他不纯了,他的部分属性不是默认的,比如这个TextView很可能已经有Text文本或者color信息了,所以,你要再次去给他扳回来,强制给他设置为你自己想要的属性。 不过他是纯的还是不纯的,你都得给他设置属性,一个都不要少!!! 所以在if里面进行的属性操作,如果不在else分支语句里面进行同样的或者对立的操作,那么你就缺少了一个属性设置的可能性,带来的结果是:数据错乱
浅笑_JIE 2015-09-24
  • 打赏
  • 举报
回复
没看到你数据有什么错乱的啊
赵小样 2015-09-24
  • 打赏
  • 举报
回复
我想知道怎么解决??
MR__P 2013-11-27
  • 打赏
  • 举报
回复
myHolder.text_one.setText(list.get(arg0).get("ItemTitle")); myHolder.text_two.setText(list.get(arg0).get("ItemText")); 你这里怎么不用s.get("ItemTitle")? 刚贴错了
MR__P 2013-11-27
  • 打赏
  • 举报
回复
myHolder.text_one.setText(list.get(arg0).get("ItemTitle")); myHolder.text_two.setText(list.get(arg0).get("ItemText")); 你这里怎么不用s.get(arg0).get("ItemTitle")?
MR__P 2013-11-27
  • 打赏
  • 举报
回复
你这没问题啊,ID没变对的啊
qiuqingpo 2013-11-27
  • 打赏
  • 举报
回复
不知道哈..
Winson_Zhou 2013-11-27
  • 打赏
  • 举报
回复
慢慢的也一样,还是一个地方的view变化 ,其他的不变。不用holder方式的话就不会出现这种问题
DrSmart 2013-11-27
  • 打赏
  • 举报
回复
楼主,别慌,慢慢就好了,这个问题不大的
Winson_Zhou 2013-11-27
  • 打赏
  • 举报
回复
忘记说了Adapter的数据类型是 private ArrayList<HashMap<String,String>> list; =====================》 对于这个问题我想说是list往上拉的时候getView,从log可以看出来tag里面拿到的view都是同一个,导致更新的view都是同一个,那个需要更新的没有更新。 list里面显示的就是一个标题一个内容 都是TextView
删除之类操作需要全选功能,方便选择 public class MainActivity extends Activity { private ListView lv; private MyAdapter mAdapter; private ArrayList list; private Button bt_selectall; // private Button bt_cancel; // private Button bt_deselectall; private int checkNum; // 记录选中的条目数量 private TextView tv_show;// 用于显示选中的条目数量 /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* 实例化各个控件 */ lv = (ListView) findViewById(R.id.lv); bt_selectall = (Button) findViewById(R.id.bt_selectall); // bt_cancel = (Button) findViewById(R.id.bt_cancelselectall); // bt_deselectall = (Button) findViewById(R.id.bt_deselectall); tv_show = (TextView) findViewById(R.id.tv); list = new ArrayList(); // 为Adapter准备数据 initDate(); // 实例化自定义的MyAdapter mAdapter = new MyAdapter(list, this); // 绑定Adapter lv.setAdapter(mAdapter); // 全选按钮的回调接口 bt_selectall.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 遍历list的长度,将MyAdapter中的map值全部设为true for (int i = 0; i < list.size(); i++) { MyAdapter.getIsSelected().put(i, true); } // 数量设为list的长度 checkNum = list.size(); // 刷新listview和TextView的显示 dataChanged(); } }); // 反选按钮的回调接口 // bt_cancel.setOnClickListener(new OnClickListener() { // @Override // public void onClick(View v) { // // 遍历list的长度,将已选的设为未选,未选的设为已选 // for (int i = 0; i < list.siz

80,351

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧