android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目

android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目,第1张

概述我有一个使用ArrayAdapter的动态ListView.从微调器中选择名称时,名称以及显示它们是男性还是女性的图标添加到ListView中. 通常一切都很好(名称正确地添加到列表中,与图标一起).但是显示性别的图标会被添加到ListView中的错误项目中.名称将添加到列表的底部,但图标将放置在列表顶部的名称处.我不知道这是否是我使用ViewHolder的方式,但在Android websit 我有一个使用ArrayAdapter的动态ListVIEw.从微调器中选择名称时,名称以及显示它们是男性还是女性的图标将添加到ListVIEw中.

通常一切都很好(名称正确地添加到列表中,与图标一起).但是显示性别的图标会被添加到ListVIEw中的错误项目中.名称将添加到列表的底部,但图标将放置在列表顶部的名称处.我不知道这是否是我使用VIEwHolder的方式,但在Android website中没有文档.

// ListvIEw inflaterinflater = (LayoutInflater) (this).getSystemService(LAYOUT_INFLATER_SERVICE);// List Array.mAdapter = new ArrayAdapter<String>(this,R.layout.player_simple_List,R.ID.label,mStrings) {    @OverrIDe    public VIEw getVIEw(int position,VIEw convertVIEw,VIEwGroup parent) {        Log.i("ANDY","VIEw getVIEw Called");        // A VIEwHolder keeps references to children vIEws to         // avoID unneccessary calls to findVIEwByID() on each row.        VIEwHolder holder;        if (null == convertVIEw) {            Log.i("ANDY","position not prevIoUsly used,so inflating");            convertVIEw = inflater.inflate(R.layout.player_simple_List,null);            // Creates a VIEwHolder and store references to the            // two children vIEws we want to bind data to.            holder = new VIEwHolder();            holder.text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.label);            holder.icon = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.icon);            if (sexmale == true) {                holder.icon.setimageBitmap(maleicon);            }            else {                holder.icon.setimageBitmap(femaleicon);            }            convertVIEw.setTag(holder);        } else {            // Get the VIEwHolder back to get fast access to the TextVIEw            // and the ImageVIEw.            holder = (VIEwHolder) convertVIEw.getTag();        }        // Bind the data efficIEntly with the holder.        holder.text.setText(getItem(position));        // Change icon depending is the sexmale variable is true or false.        Log.i("ANDY","getCount = "+mAdapter.getCount());        return convertVIEw;    }};setlistadapter(mAdapter);
解决方法 您必须在if-else-if之后设置图标以创建或绑定持有者.否则,图标将仅在列表中的前几个项目中正确显示,即直到未填充ListVIEw.
public VIEw getVIEw(int position,VIEwGroup parent) {    Log.i("ANDY","VIEw getVIEw Called");    // A VIEwHolder keeps references to children vIEws    // to avoID unneccessary calls to findVIEwByID() on each row.    VIEwHolder holder;        if (null == convertVIEw) {            Log.i("ANDY",null);            // Creates a VIEwHolder and store references to            // the two children vIEws we want to bind data to.            holder = new VIEwHolder();            holder.text = (TextVIEw) convertVIEw.findVIEwByID(R.ID.label);            holder.icon = (ImageVIEw) convertVIEw.findVIEwByID(R.ID.icon);            convertVIEw.setTag(holder);        } else {            // Get the VIEwHolder back to get fast access to the TextVIEw            // and the ImageVIEw.            holder = (VIEwHolder) convertVIEw.getTag();        }        // Bind the data efficIEntly with the holder.        holder.text.setText(getItem(position));        // Change icon depending is the sexmale variable is true or false.        if (sexmale == true) {            holder.icon.setimageBitmap(maleicon);        }        else {            holder.icon.setimageBitmap(femaleicon);        }        Log.i("ANDY","getCount = "+mAdapter.getCount());        return convertVIEw;}
总结

以上是内存溢出为你收集整理的android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目全部内容,希望文章能够帮你解决android – ListView with ArrayAdapter和ViewHolder将图标添加到错误的项目所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1138358.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存