处理一个列表项中的多个图像

时间:2013-10-16 12:40:08

标签: android list listview android-listview listitem

我要做的是在一个listview项目中显示8个图像,但我似乎遇到的问题是,当我显示图像时,单元格重新创建在错误的单元格中显示错误的图像次。

我基本上试图复制Instagram新闻页面(请仔细阅读页面),见下图:

multiple image in one item, Instagram

另见下面我遇到的问题图片:

image of problem

正如您所看到的,底部列表中有11个项目,顶部有2个项目,但顶部的项目显示了8个图像,这些图像是从底部项目中获取的。

如果有人之前遇到这种情况并且知道解决问题的方法,我会非常感激。

我可以在这里放置代码,但目前它太大了,对任何特定的内容发表评论,我会尽可能地编辑这个问题。

由于

修改

我之前应该说的一件事是图像来自手机存储/自定义数据库。适配器代码也非常大,请参见下文:

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

    if (convertView == null) {

        convertView = mInflater.inflate(R.layout.list_item, parent, false);
        holder = new ViewHolder();

        holder.txtName = (TextView) convertView
                .findViewById(R.id.textView1);
        holder.txtCount = (TextView) convertView
                .findViewById(R.id.item_count);
        holder.add_item_text = (TextView) convertView
                .findViewById(R.id.add_item_text);

        holder.image1 = (ImageView) convertView
                .findViewById(R.id.imageView1);
        holder.image2 = (ImageView) convertView
                .findViewById(R.id.imageView2);
        holder.image3 = (ImageView) convertView
                .findViewById(R.id.imageView3);
        holder.image4 = (ImageView) convertView
                .findViewById(R.id.imageView4);
        holder.image5 = (ImageView) convertView
                .findViewById(R.id.imageView5);
        holder.image6 = (ImageView) convertView
                .findViewById(R.id.imageView6);
        holder.image7 = (ImageView) convertView
                .findViewById(R.id.imageView7);
        holder.image8 = (ImageView) convertView
                .findViewById(R.id.imageView8);

        holder.image1Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView1Overlay);
        holder.image2Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView2Overlay);
        holder.image3Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView3Overlay);
        holder.image4Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView4Overlay);
        holder.image5Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView5Overlay);
        holder.image6Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView6Overlay);
        holder.image7Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView7Overlay);
        holder.image8Overlay = (RelativeLayout) convertView
                .findViewById(R.id.imageView8Overlay);


        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    String count = "0";

    holder.txtName.setText(searchArrayList.get(position).getName());
    if (searchArrayList.get(position).getCount() != null) {
        count = String.valueOf(searchArrayList.get(position).getCount());
    }
    holder.txtCount.setText(count);

    holder.image1.setVisibility(View.GONE);
    holder.image2.setVisibility(View.GONE);
    holder.image3.setVisibility(View.GONE);
    holder.image4.setVisibility(View.GONE);
    holder.image5.setVisibility(View.GONE);
    holder.image6.setVisibility(View.GONE);
    holder.image7.setVisibility(View.GONE);
    holder.image8.setVisibility(View.GONE);

    holder.image1Overlay.setVisibility(View.GONE);
    holder.image2Overlay.setVisibility(View.GONE);
    holder.image3Overlay.setVisibility(View.GONE);
    holder.image4Overlay.setVisibility(View.GONE);
    holder.image5Overlay.setVisibility(View.GONE);
    holder.image6Overlay.setVisibility(View.GONE);
    holder.image7Overlay.setVisibility(View.GONE);
    holder.image8Overlay.setVisibility(View.GONE);

    holder.add_item_text.setVisibility(View.VISIBLE);

    if (!(searchArrayList.get(position).getCount() == null)) {
        if (searchArrayList.get(position).getCount().equals("0")) {
            holder.image1.setVisibility(View.GONE);
            holder.image2.setVisibility(View.GONE);
            holder.image3.setVisibility(View.GONE);
            holder.image4.setVisibility(View.GONE);
            holder.image5.setVisibility(View.GONE);
            holder.image6.setVisibility(View.GONE);
            holder.image7.setVisibility(View.GONE);
            holder.image8.setVisibility(View.GONE);

            holder.image1Overlay.setVisibility(View.GONE);
            holder.image2Overlay.setVisibility(View.GONE);
            holder.image3Overlay.setVisibility(View.GONE);
            holder.image4Overlay.setVisibility(View.GONE);
            holder.image5Overlay.setVisibility(View.GONE);
            holder.image6Overlay.setVisibility(View.GONE);
            holder.image7Overlay.setVisibility(View.GONE);
            holder.image8Overlay.setVisibility(View.GONE);

            holder.add_item_text.setVisibility(View.VISIBLE);

        } else {
            holder.image1.setVisibility(View.VISIBLE);
            holder.image2.setVisibility(View.VISIBLE);
            holder.image3.setVisibility(View.VISIBLE);
            holder.image4.setVisibility(View.VISIBLE);
            holder.image5.setVisibility(View.VISIBLE);
            holder.image6.setVisibility(View.VISIBLE);
            holder.image7.setVisibility(View.VISIBLE);
            holder.image8.setVisibility(View.VISIBLE);

            bitmapImage = null;

            MainDataSouce datasource = new MainDataSouce(con);
            final ArrayList<MainFragmetnArray> array = createArray
                    .getArrayData(datasource, searchArrayList.get(position)
                            .getName(), con.getResources()
                            .getDisplayMetrics().density, 100);

            int size = array.size();

            System.out.println("SWITCH SIZE >>>>> " + size);

            switch (size) {

            case 0:

                System.out.println("CASE 0");

                bitmapImage = null;

                holder.image1.setImageBitmap(bitmapImage);
                holder.image2.setImageBitmap(bitmapImage);
                holder.image3.setImageBitmap(bitmapImage);
                holder.image4.setImageBitmap(bitmapImage);
                holder.image5.setImageBitmap(bitmapImage);
                holder.image6.setImageBitmap(bitmapImage);
                holder.image7.setImageBitmap(bitmapImage);
                holder.image8.setImageBitmap(bitmapImage);

                holder.image1.setVisibility(View.GONE);
                holder.image2.setVisibility(View.GONE);
                holder.image3.setVisibility(View.GONE);
                holder.image4.setVisibility(View.GONE);
                holder.image5.setVisibility(View.GONE);
                holder.image6.setVisibility(View.GONE);
                holder.image7.setVisibility(View.GONE);
                holder.image8.setVisibility(View.GONE);

                holder.image1Overlay.setVisibility(View.GONE);
                holder.image2Overlay.setVisibility(View.GONE);
                holder.image3Overlay.setVisibility(View.GONE);
                holder.image4Overlay.setVisibility(View.GONE);
                holder.image5Overlay.setVisibility(View.GONE);
                holder.image6Overlay.setVisibility(View.GONE);
                holder.image7Overlay.setVisibility(View.GONE);
                holder.image8Overlay.setVisibility(View.GONE);

                holder.add_item_text.setVisibility(View.VISIBLE);

                break;

            case 1:

                System.out.println("CASE 1");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }
                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                break;

            case 2:

                System.out.println("CASE 2");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                // System.gc();

                break;

            case 3:

                System.out.println("CASE 3");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(2).getImage().equalsIgnoreCase("text item")) {
                    holder.image3.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(2).getImage(),
                            holder.image3);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(2).getGotIt().equals("true")) {
                    holder.image3Overlay.setVisibility(View.VISIBLE);
                }

                // System.gc();

                break;

            case 4:

                System.out.println("CASE 4");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(2).getImage().equalsIgnoreCase("text item")) {
                    holder.image3.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(2).getImage(),
                            holder.image3);
                }

                if (array.get(3).getImage().equalsIgnoreCase("text item")) {
                    holder.image4.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(3).getImage(),
                            holder.image4);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(2).getGotIt().equals("true")) {
                    holder.image3Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(3).getGotIt().equals("true")) {
                    holder.image4Overlay.setVisibility(View.VISIBLE);
                }

                // System.gc();

                break;
            case 5:

                System.out.println("CASE 5");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(2).getImage().equalsIgnoreCase("text item")) {
                    holder.image3.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(2).getImage(),
                            holder.image3);
                }

                if (array.get(3).getImage().equalsIgnoreCase("text item")) {
                    holder.image4.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(3).getImage(),
                            holder.image4);
                }

                if (array.get(4).getImage().equalsIgnoreCase("text item")) {
                    holder.image5.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(4).getImage(),
                            holder.image5);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(2).getGotIt().equals("true")) {
                    holder.image3Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(3).getGotIt().equals("true")) {
                    holder.image4Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(4).getGotIt().equals("true")) {
                    holder.image5Overlay.setVisibility(View.VISIBLE);
                }

                break;
            case 6:

                System.out.println("CASE 6");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(2).getImage().equalsIgnoreCase("text item")) {
                    holder.image3.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(2).getImage(),
                            holder.image3);
                }

                if (array.get(3).getImage().equalsIgnoreCase("text item")) {
                    holder.image4.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(3).getImage(),
                            holder.image4);
                }

                if (array.get(4).getImage().equalsIgnoreCase("text item")) {
                    holder.image5.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(4).getImage(),
                            holder.image5);
                }

                if (array.get(5).getImage().equalsIgnoreCase("text item")) {
                    holder.image6.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(5).getImage(),
                            holder.image6);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(2).getGotIt().equals("true")) {
                    holder.image3Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(3).getGotIt().equals("true")) {
                    holder.image4Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(4).getGotIt().equals("true")) {
                    holder.image5Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(5).getGotIt().equals("true")) {
                    holder.image6Overlay.setVisibility(View.VISIBLE);
                }

                // System.gc();

                break;
            case 7:

                System.out.println("CASE 7");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(2).getImage().equalsIgnoreCase("text item")) {
                    holder.image3.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(2).getImage(),
                            holder.image3);
                }

                if (array.get(3).getImage().equalsIgnoreCase("text item")) {
                    holder.image4.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(3).getImage(),
                            holder.image4);
                }

                if (array.get(4).getImage().equalsIgnoreCase("text item")) {
                    holder.image5.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(4).getImage(),
                            holder.image5);
                }

                if (array.get(5).getImage().equalsIgnoreCase("text item")) {
                    holder.image6.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(5).getImage(),
                            holder.image6);
                }

                if (array.get(6).getImage().equalsIgnoreCase("text item")) {
                    holder.image7.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(6).getImage(),
                            holder.image7);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(2).getGotIt().equals("true")) {
                    holder.image3Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(3).getGotIt().equals("true")) {
                    holder.image4Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(4).getGotIt().equals("true")) {
                    holder.image5Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(5).getGotIt().equals("true")) {
                    holder.image6Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(6).getGotIt().equals("true")) {
                    holder.image7Overlay.setVisibility(View.VISIBLE);
                }

                break;

            default:

                System.out.println("CASE DEFAULT");

                holder.add_item_text.setVisibility(View.GONE);

                if (array.get(0).getImage().equalsIgnoreCase("text item")) {
                    holder.image1.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(0).getImage(),
                            holder.image1);
                }

                if (array.get(1).getImage().equalsIgnoreCase("text item")) {
                    holder.image2.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(1).getImage(),
                            holder.image2);
                }

                if (array.get(2).getImage().equalsIgnoreCase("text item")) {
                    holder.image3.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(2).getImage(),
                            holder.image3);
                }

                if (array.get(3).getImage().equalsIgnoreCase("text item")) {
                    holder.image4.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(3).getImage(),
                            holder.image4);
                }

                if (array.get(4).getImage().equalsIgnoreCase("text item")) {
                    holder.image5.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(4).getImage(),
                            holder.image5);
                }

                if (array.get(5).getImage().equalsIgnoreCase("text item")) {
                    holder.image6.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(5).getImage(),
                            holder.image6);
                }

                if (array.get(6).getImage().equalsIgnoreCase("text item")) {
                    holder.image7.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(6).getImage(),
                            holder.image7);
                }

                if (array.get(7).getImage().equalsIgnoreCase("text item")) {
                    holder.image8.setImageResource(R.drawable.green);
                } else {
                    imageLoaderFile.DisplayImage(array.get(7).getImage(),
                            holder.image8);
                }

                if (array.get(0).getGotIt().equals("true")) {
                    holder.image1Overlay.setVisibility(View.VISIBLE);
                }
                if (array.get(1).getGotIt().equals("true")) {
                    holder.image2Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(2).getGotIt().equals("true")) {
                    holder.image3Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(3).getGotIt().equals("true")) {
                    holder.image4Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(4).getGotIt().equals("true")) {
                    holder.image5Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(5).getGotIt().equals("true")) {
                    holder.image6Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(6).getGotIt().equals("true")) {
                    holder.image7Overlay.setVisibility(View.VISIBLE);
                }

                if (array.get(7).getGotIt().equals("true")) {
                    holder.image8Overlay.setVisibility(View.VISIBLE);
                }

                break;
            }
        }
    }

    return convertView;
}

由于

0 个答案:

没有答案