1

i m trying giving integer value image using in gridview.bt i couldnt get .

this is my values/string.xml

<integer-array name="anim">
    <item >@drawable/goat1</item>
    <item >@drawable/lamb1</item>
    <item>@drawable/bear</item>
    <item>@drawable/lion</item>
</integer-array>

this is my java code:

public class GridViewActivity extends Activity {

     **int[] anim=getResources().getIntArray(R.array.anim);**

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(new ImageAdapter(this));
}

}

public class ImageAdapter extends BaseAdapter { private Context mContext;

public ImageAdapter(Context c) {
    mContext = c;
}

public int getCount() {
    return mThumbIds.length;
}

public Object getItem(int position) {
    return null;
}

public long getItemId(int position) {
    return 0;
}


public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;
    if (convertView == null) { 
        imageView = new ImageView(mContext);
        imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(8, 8, 8, 8);
    } else {
        imageView = (ImageView) convertView;
    }

    imageView.setImageResource(anim[position]);
    return imageView;
}

1 Answer 1

2
use TypedArray,
TypedArray icons;
Resources res = getResources();
icons = res.obtainTypedArray(R.array.img);

Then in image adapter, instead of imageView.setImageResource(anim[position]); :

Drawable drawable = icons.getDrawable(position);
imageview.setImageDrawable(drawable);
Sign up to request clarification or add additional context in comments.

1 Comment

If the answer helped you a good way to say thank you is by accepting the answer as correct. It will provide the user with points in addition to removing question from the unanswered questions list.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.