繁体   English   中英

如何在Android中的ListView中的imageview中设置图像?

[英]How do I set an image in imageview inside ListView in Android?

我有一个listview,其中包括2个textviews和1个imageview。 现在,将在某些条件下设置imageview中的图像,否则应将其保留为空白而没有图像。

问题是图像没有进入正确的行。 说,例如,图像应显示在第3行,但其显示在第4行。而且,在某些情况下,每行将具有相同的图像。

有人遇到过这样的问题吗? 我真的不知道背后的原因。 谁能指出这个问题?


private static class VilleAdapter extends BaseAdapter {
    private LayoutInflater mInflater = null;

    // private Context context = null;

    public VilleAdapter(Context context) {
        // Cache the LayoutInflate to avoid asking for a new one each time.
        mInflater = LayoutInflater.from(context);
    }

    /**
     * The number of items in the list is determined by the number of
     * speeches in our array.
     * 
     * @see android.widget.ListAdapter#getCount()
     */
    public int getCount() {
        // return BabbleMainListParse.getNumOfBabbles();
        return VilleMainListParse.getVilleCount();
    }

    /**
     * Since the data comes from an array, just returning the index is
     * sufficent to get at the data. If we were using a more complex data
     * structure, we would return whatever object represents one row in the
     * list.
     * 
     * @see android.widget.ListAdapter#getItem(int)
     */
    public Object getItem(int position) {
        return position;
    }

    /**
     * Use the array index as a unique id.
     * 
     * @see android.widget.ListAdapter#getItemId(int)
     */
    public long getItemId(int position) {
        return position;
    }

    /**
     * Make a view to hold each row.
     * 
     * @see android.widget.ListAdapter#getView(int, android.view.View,
     *      android.view.ViewGroup)
     */
    public View getView(int position, View convertView, ViewGroup parent) 
    {
        // A ViewHolder keeps references to children views to avoid
        // unneccessary calls
        // to findViewById() on each row.
        ViewHolder holder;

        // When convertView is not null, we can reuse it directly, there is
        // no need
        // to reinflate it. We only inflate a new View when the convertView
        // supplied
        // by ListView is null.
        if (convertView == null) 
        {
            convertView = mInflater.inflate(R.layout.villerow, parent,false);

            // Creates a ViewHolder and store references to the two children
            // views
            // we want to bind data to.
            holder = new ViewHolder();
            holder.txtVilleListTitle = (TextView) convertView.findViewById(R.id.txtVilleListTitle);
            holder.txtVilleDescription = (TextView) convertView.findViewById(R.id.txtVilleDescription);
            holder.imgvillepwd = (ImageView) convertView.findViewById(R.id.imgvillepwd);

            convertView.setTag(holder);
        }
        else 
        {
            // Get the ViewHolder back to get fast access to the TextView
            holder = (ViewHolder) convertView.getTag();
        }

        // Bind the data efficiently with the holder.
        holder.txtVilleListTitle.setText(VilleMainListParse.getMsgTitle(position));

        if(VilleMainListParse.getMsgDesc(position).length() > 30)
        {
            String temp = VilleMainListParse.getMsgDesc(position).substring(0, 30);
            temp = temp +".....";
            holder.txtVilleDescription.setText(temp);
        }
        else
            holder.txtVilleDescription.setText(VilleMainListParse.getMsgDesc(position));

        if(!VilleMainListParse.getPwd(position).equals(""))
        {
            if(VilleMainListParse.getUnlock(position))
            {
                holder.imgvillepwd.setBackgroundResource(R.drawable.lock);
            }   
            else if(!VilleMainListParse.getUnlock(position))
            {
                holder.imgvillepwd.setBackgroundResource(R.drawable.lock_open);
            }
        }

        return convertView;
    }

    static class ViewHolder
    {
        TextView txtVilleListTitle;
        TextView txtVilleDescription;
        ImageView imgvillepwd;
    }
}

上面的代码仅适用于适配器。 当单击登录按钮时,最初会有一个登录屏幕,该数据是从Internet加载的,然后此列表视图会加载此数据。 此数据最初已正确加载。

有人可以让我知道此代码有什么问题吗?

当您重复使用相同的视图时,如果不应该显示该图像,则应清除该图像。 否则,先前的图片将显示在此处。 因此,我建议进行以下修复:

if(!VilleMainListParse.getPwd(position).equals(""))
{
    if(VilleMainListParse.getUnlock(position))
    {
        holder.imgvillepwd.setBackgroundResource(R.drawable.lock);
    }   
    else if(!VilleMainListParse.getUnlock(position))
    {
        holder.imgvillepwd.setBackgroundResource(R.drawable.lock_open);
    }
}
else
    holder.imgvillepwd.setBackgroundDrawable(null);

您也可以考虑使用setImageResource / setImageBitmap方法代替setBackgroundXxx。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM