androidtypedarray

Getting wrong image index


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

Here I'm getting value of animal array into imgs (TypedArray)

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

And now retrieving Resource Id and storing into ArrayList of Integers like this

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

Now when set image, it gives wrong image, and same image multiple time as well

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

And if I use recycle() to recycle images (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();

It shows this error

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

My gridview adapter where I'm getting error

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;
    }
}

Solution

  • Please try below code, compare it with your existing code and ask me if you will getting confused.

    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;
        }
    }