ListView在滚动时复制位图图像

时间:2013-10-25 19:11:58

标签: android

我正在开展一个项目。我能够从SQLite数据库中获取所有信息和图片。当我向上和向下滚动时,运行APP时,分配给一个联系人的图像在滚动时会复制到另一个联系人。有人可以帮助我。

私有类MainCursorAdapter扩展了SimpleCursorAdapter {

    private class ViewHolder {
        Cursor cursor;
        TextView contact_fullname;
        String CONTACT_fullname;
        TextView contact_meetat;
        String CONTACT_meetat;
        TextView contact_phonenumber;
        String CONTACT_phonenumber;
        String CONTACT_status;
        Bitmap bmp;
        ImageView avatar_image;
        byte[] bb;
    }

    public MainCursorAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to, int flags) {
        super(context, layout, c, from, to, flags);

    }

    public View getView(int position, View convertView, ViewGroup parent) {

        // get reference to the row
        View view = super.getView(position, convertView, parent);
        ViewHolder holder = new ViewHolder();
        // check for odd or even to set alternate colors to the row
        // background
        if (convertView == null) {
            holder.contact_fullname = (TextView) view
                    .findViewById(R.id.rowfirstname);
            holder.contact_meetat = (TextView) view
                    .findViewById(R.id.rowmeetat);
            holder.contact_phonenumber = (TextView) view
                    .findViewById(R.id.rowprimarynumber);
            holder.avatar_image = (ImageView) view
                    .findViewById(R.id.select_imgview);

            holder.cursor = (Cursor) getItem(position);
            holder.CONTACT_fullname = holder.cursor.getString(holder.cursor
                    .getColumnIndex(SQLiteAdapter.CONTACT_FULLNAME));
            holder.CONTACT_meetat = holder.cursor.getString(holder.cursor
                    .getColumnIndex(SQLiteAdapter.CONTACT_MEETAT));
            holder.CONTACT_phonenumber = holder.cursor
                    .getString(holder.cursor
                            .getColumnIndex(SQLiteAdapter.PHONE_NUMBER));
            holder.CONTACT_status = holder.cursor.getString(holder.cursor
                    .getColumnIndex(SQLiteAdapter.CONTACT_STATUS));
            holder.bb = holder.cursor.getBlob(holder.cursor
                    .getColumnIndex(SQLiteAdapter.CONTACT_AVATAR));

            if (holder.CONTACT_status.equals("Block")) {

                holder.contact_fullname.setTextColor(Color.WHITE);
                holder.contact_meetat.setTextColor(Color.WHITE);
                holder.contact_phonenumber.setTextColor(Color.WHITE);
                holder.contact_meetat
                        .setWidth(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setBackgroundColor(Color.RED);

            } else if (holder.CONTACT_status.equals("Silent")) {

                holder.contact_fullname.setTextColor(Color.WHITE);
                holder.contact_meetat.setTextColor(Color.WHITE);
                holder.contact_phonenumber.setTextColor(Color.WHITE);
                holder.contact_meetat
                        .setWidth(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setBackgroundColor(getResources().getColor(
                        R.color.pink));

            } else {

                holder.contact_fullname.setTextColor(Color.BLACK);
                holder.contact_meetat.setTextColor(Color.BLACK);
                holder.contact_phonenumber.setTextColor(Color.BLACK);
                holder.contact_meetat
                        .setWidth(android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
                view.setBackgroundColor(Color.WHITE);
            }

            try {
                if (holder.bb.equals(null) || holder.bb.equals("")) {
                    // no image to display
                    holder.avatar_image.setVisibility(View.GONE);
                    holder.avatar_image.setImageResource(R.drawable.addicon);
                } else {

                    holder.avatar_image.setVisibility(View.VISIBLE);
                    holder.bmp = BitmapFactory.decodeByteArray(holder.bb,
                            0, holder.bb.length);
                    holder.avatar_image.setImageBitmap(ImageAdapter
                            .getRoundedCornerBitmap(holder.bmp, 50));

                }

            } catch (Exception e) {

            }
            System.out.println("If: "+view.getTag());
            view.setTag(holder);

        } else {
            System.out.println("Else: " + view.getTag());
            holder = (ViewHolder) view.getTag();
        }


        return view;
    }
}

1 个答案:

答案 0 :(得分:0)

正如您在此example中看到的那样,您的问题是当转换视图不为空时,您应该获取标记,然后再确定值。

ViewHolder holder;
if(null == convertview){
     //inflate
     //create holder
     //all the find views
     convertview.setTag(holder);
} else{
     ViewHolder holder = convertview.getTag();
}
holder.something.method(value);

希望这会有所帮助