Akash Agarwal
Akash Agarwal

Reputation: 2540

Can the usage of "R.id" before View's name be avoided?

I'm working on a TicTacToe app which uses 9 ImageViews. ImageViews are clickable and on their click, either the cross or circle image is shown, followed by game's logic. Since the resource file name needs to be updated for every ImageView, I need to write something like this:

@Override
public void onClick(View v) {

switch (v.getId()) {

case R.id.oneImage:
    // do your code
    break;

case R.id.twoImage:
    // do your code
    break;

case R.id.threeImage:
    // do your code
    break;
//And so on for 6 more cases
default:
    break;
    }

}

While the task I need to do is pretty much same for all views, I was looking for a way to do something generic, if possible like:

v.getId().setImageResource(R.drawable.cross);

or

int ivId= v.getId();
String name= "R.id." + ivId;
Integer.valueOf(name).setImageResource(R.drawable.cross);

Both techniques can't resolve the setImageResource() method and it's understandable why.

I figured that none of these methods would work and I also can't seem to think of the right query to search on Google or here, which is why I ended up asking my own question. Any help would be really appreciated. Thanks!

EDIT I'd like to be able to use something like a generic name. For example, right now I'm doing this:

final ImageView[] iv= new ImageView[9];


iv[0]= (ImageView)findViewById(R.id.imageView1);
iv[1]= (ImageView)findViewById(R.id.imageView2);
iv[2]= (ImageView)findViewById(R.id.imageView3);
iv[3]= (ImageView)findViewById(R.id.imageView4);
iv[4]= (ImageView)findViewById(R.id.imageView5);
iv[5]= (ImageView)findViewById(R.id.imageView6);
iv[6]= (ImageView)findViewById(R.id.imageView7);
iv[7]= (ImageView)findViewById(R.id.imageView8);
iv[8]= (ImageView)findViewById(R.id.imageView9);

for(ImageView i: iv)
        i.setOnClickListener(ivCl);//ivCl is the View.OnClickListener class

Wouldn't that have been helpful if I could use a for loop something like this:

for(x=1;x<10;x++){
        iv[x-1]=(ImageView)findViewById("R.id.imageView" + x);
//I understand the above method is wrong, because findViewById takes integer parameter, but this is something that I wanted to do.

Upvotes: 3

Views: 86

Answers (1)

Vyacheslav
Vyacheslav

Reputation: 27221

Use v.setImageResource() instead of v.getId().setImageResource(). v.getId() gives to you just a integer number inside R.id list class. No more. It is not possible to set setImageResource to Integer class :) But you can do it with View class. For your question:

ImageView img = (ImageView) v;
    img.setImageResource(R.drawable.cross);

For the second question I do not think this is a good idea. But try this: R.drawable.class.getField("cross").getInt(null)

I've tested this worked on my device:

private void test() {
        try {
            int ar1 = R.id.class.getField("ar1").getInt(null);
            Log.d(TAG,ar1+"");
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

Upvotes: 3

Related Questions