获取错误的图像索引

时间:2018-04-16 11:12:12

标签: android typedarray

array.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <integer-array name="animal">
        <item>@drawable/animal1</item>
        <item>@drawable/animal10</item>
        <item>@drawable/animal100</item>
        <item>@drawable/animal101</item>
        <item>@drawable/animal102</item>
        <item>@drawable/animal103</item>
        <item>@drawable/animal104</item>
        <item>@drawable/animal105</item>
        <item>@drawable/animal106</item>
        <item>@drawable/animal107</item>
        <item>@drawable/animal108</item>
        <item>@drawable/animal109</item>
        <item>@drawable/animal11</item>
        <item>@drawable/animal110</item>
    </integer-array>
</resources>

MainActivity.class

在这里,我将动物数组的值变为imgs(TypedArray)

TypedArray imgs = getResources().obtainTypedArray(R.array.animal);

现在检索资源ID并存储到像这样的整数ArrayList中

ArrayList<Integer> myImageList = new ArrayList<>();
for(int i=0; i<mystikers.length(); i++){
    myImageList.add(imgs.getResourceId(i, -1));
}

现在,当设置图像时,它会提供错误的图像,同时图像也会多次出现

Glide.with(mContext).load(myImageList.get(5))
                    .override(150, 150)
                    .into(imageView);

如果我使用recycle()来回收图像(TypedArray)

TypedArray imgs = getResources().obtainTypedArray(R.array.animal);
ArrayList<Integer> myImageList = new ArrayList<>();
    for(int i=0; i<imgs.length(); i++){
        myImageList.add(imgs.getResourceId(i, -1));
    }
imgs.recycle();

显示此错误

java.lang.RuntimeException: Cannot make calls to a recycled instance!

我的网格视图适配器,我收到错误

public class StikAdpter extends BaseAdapter {
    private Context mContext;

    int size=0;
    ArrayList<Integer> img_List = new ArrayList<>();
    ArrayList<Integer> myImageList22 = new ArrayList<>();

    public StikAdpter(Context c,ArrayList<Integer> img_Detail_List,TypedArray mystikerishere){
        mContext= c;
        img_List=img_Detail_List;
        size = img_List.size();
        Log.v("count",String.valueOf(size));
        for(int i=0; i<mystikerishere.length(); i++){
            myImageList22.add(mystikerishere.getResourceId(i, -1));
            Log.v("allimages",img_List.get(i)+"");
        }
        mystikerishere.recycle();
    }
    @Override
    public int getCount() {
        return size;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }
    @Override
    public long getItemId(int position) {
        return 0;
    }
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView1;

        if (convertView == null) {
            gridView1 = new View(mContext);

            // get layout from mobile.xml
            gridView1 = inflater.inflate(R.layout.grid_items, null);

            // set image based on selected text
            ImageView imageView = (ImageView) gridView1
                    .findViewById(R.id.grid_item_image);

            Glide.with(mContext)
                    .load(myImageList22.get(position))
                    .override(150, 150)
                    .into(imageView);
            gridView1.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 200));
        }else {
            gridView1 = (View) convertView;
        }

        return gridView1;
    }
}

1 个答案:

答案 0 :(得分:2)

请尝试以下代码,将其与现有代码进行比较,并询问我是否会感到困惑。

public class StikAdpter extends BaseAdapter {
    private Context mContext;

    ArrayList<Integer> myImageList22 = new ArrayList<>();

    public StikAdpter(Context c, ArrayList<Integer> img_Detail_List, TypedArray mystikerishere) {
        mContext = c;
        for (int i = 0; i < mystikerishere.length(); i++) {
            myImageList22.add(mystikerishere.getResourceId(i, -1));
        }
        mystikerishere.recycle();
    }

    @Override
    public int getCount() {
        return myImageList22.size();
    }

    @Override
    public Object getItem(int position) {
        return myImageList22.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

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

        LayoutInflater inflater = (LayoutInflater) mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View gridView1;
        ImageView imageView;
        if (convertView == null) {
            gridView1 = new View(mContext);
            // get layout from mobile.xml
            gridView1 = inflater.inflate(R.layout.grid_items, null);
            imageView = (ImageView) gridView1
                    .findViewById(R.id.grid_item_image);

            // set image based on selected text
            gridView1.setLayoutParams(new GridView.LayoutParams(GridView.AUTO_FIT, 200));
        } else {
            gridView1 = (View) convertView;
        }


        Glide.with(mContext)
                .load(myImageList22.get(position))
                .override(150, 150)
                .into(imageView);

        return gridView1;
    }
}