2

I want to store drawable images in a 2D array

ImageView iv1[][] = new ImageView[2][20];
    int mylist[][]={{R.drawable.step_000,R.drawable.step_001,R.drawable.step_002,R.drawable.step_003,R.drawable.step_004,
        R.drawable.step_005,R.drawable.step_006,R.drawable.step_007,R.drawable.step_008,R.drawable.step_009,R.drawable.step_010,
        R.drawable.step_011,R.drawable.step_012,R.drawable.step_013,R.drawable.step_014,R.drawable.step_015,R.drawable.step_016,
        R.drawable.step_017,R.drawable.step_018,R.drawable.step_019}, 

        {R.drawable.step_100,R.drawable.step_101,R.drawable.step_102,R.drawable.step_103,R.drawable.step_104,
        R.drawable.step_105,R.drawable.step_106,R.drawable.step_107,R.drawable.step_108,R.drawable.step_109,R.drawable.step_110,
        R.drawable.step_111,R.drawable.step_112,R.drawable.step_113,R.drawable.step_114,R.drawable.step_115,R.drawable.step_116,
        R.drawable.step_117,R.drawable.step_118,R.drawable.step_119}};

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.data);
    iv=(ImageView)findViewById(R.id.imageView1);
    iv2=(ImageView)findViewById(R.id.left);
    iv3=(ImageView)findViewById(R.id.right);
    iv2.setVisibility(View.INVISIBLE);

    Bundle bundle=getIntent().getExtras();
    String value=bundle.getString("key");
    int step = getIntent().getExtras().getInt("steps");
    Toast.makeText(getApplicationContext(), ""+step,Toast.LENGTH_SHORT).show();

    int a;
    a=step;
    for(int i=0;i<20;i++)
    {
        for(int j=0; j<a; j++)
        iv1[i][j] = (ImageView)findViewById(mylist[i][j]);
    }


    if(value.contentEquals("cristiano"))
    {
    iv.setImageResource(mylist[i][j]);
    }
4
  • my 20 images are not running..only a single image is working.. Commented Mar 19, 2014 at 13:34
  • ArrayIndexOutBoundException in the best case Commented Mar 19, 2014 at 13:34
  • ImageView iv1[][] = new ImageView[2][20]; for(int i=0;i<20;i++) iv1[i][j] = ... There should be an error happening. Commented Mar 19, 2014 at 13:42
  • @user3432085 hows your if(value.contentEquals("cristiano")) { iv.setImageResource(mylist[i][j]); } not showing any error ? Have you declared i and j globally ? Commented Mar 19, 2014 at 13:44

1 Answer 1

1

It appears that you are setting everything in the iv1 ImageView array correctly, using the two for loops. The problem is that you are setting one ImageView on the screen to the entire array, so it is only going to show one. You should try doing a GridLayout of multiple ImageViews and then setting each of those one by one. I know it would be convenient to do it this way, but I don't think there is a way to make it work like that. In addition, I would think that iv.setImageResource(mylist[i][j]); would give you trouble as it is outside the loop and does not know what i or j are.

Sign up to request clarification or add additional context in comments.

Comments

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.