Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
949 views
in Technique[技术] by (71.8m points)

java - How to get drawable name from imageView

I have an app that create a random image when the activity opens. There are like 30 images in the random image array. The image is generated, but what I want is to write the drawable name into a textview. It looks like this:

    ImageView RImage= (ImageView) findViewById(R.id.image);
    RImage.setImageResource(generator());

    Drawable myDrawable = RImage.getDrawable();

    TextView writeID = (TextView) findViewById(R.id.idtext);
    writeID.setText(String.valueOf(generator()));
}

private int generator() {
    TypedArray imgs = getResources().obtainTypedArray(R.array.list3);
    int imgid = imgs.getResourceId(new Random().nextInt(imgs.length()), -1);
    imgs.recycle();
    return imgid;
}

Currently I receive numbers, and I cant figure out how can I convert the int to a string, or how can I use the generator to generate a string, display in the textview and then convert it to int to display it in the imageview.

I created an another activity where I can pick the image I want to see from a spinner, it simply send the selected option with string to the next activity on button press, and in the other activity i remove the .png and res/drawable/ from the string, and convert the string into an int to display the image.

   Bundle extras = getIntent().getExtras();
    String transportItemChosen = extras.getString("SpinnerValue");
    transportItemChosen = transportItemChosen.replace(".png", "");
    transportItemChosen = transportItemChosen.replace("res/drawable/", "");

    String uri = ("@drawable/" + transportItemChosen);
    int id = getResources().getIdentifier(uri, null, getPackageName());

   ImageView mImageView;
    mImageView = (ImageView) findViewById(R.id.selectedimage);
    mImageView.setImageResource(id);

So I need the generator to generate to string and then convert it to int. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change your code by converting your int ids into String names by Using the method

getResourceEntryName(int id);

Change the line:

writeID.setText(String.valueOf(generator()));

To Something like:

writeID.setText(getResources().getResourceEntryName(generator()));

And for Viceversa:

And if you are having a String lets assume this String you get from a textView or using above method to convert it back to an int id you do the following:

int id= getResources().getIdentifier("your_string_here", "drawable", getPackageName());

And if you are calling the code from a Fragment not an Activity remember to put getActivity() before calling getResources()or before calling getPackageName(). I have not tested the code as I am typing, but I am sure it will work (may be a typo) for more information consider visting the Documentation on Resources from this official link.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...