开发者

How to get image resource name

开发者 https://www.devze.com 2023-03-15 00:47 出处:网络
How do I get an imageview resource name which has been set dynamically? This is the image Adapter code:

How do I get an imageview resource name which has been set dynamically?

This is the image Adapter code:

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

  // create a new ImageView for each item referenced by the Adapter
  public View getView(int position, View convertView, ViewGroup parent) {
    View v;

    if (convertView == null) { // if it's not recycled, initialize some
      // attributes

      LayoutInflater li = getLayoutInflater();
      v = li.inflate(R.layout.gridxml, null);
        imageView = (ImageView)v.findViewById(R.id.icon_image);

      imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
      //imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
      imageView.setPadding(5, 5, 5, 5);
    } else {
      imageView = (ImageView) convertView;
  }

    imageView.setImageResource(mThumbIds[position]);
        imageView.setTag(mThumbIds[position]);

    System.out.println(mThumbIds[0]);
          Syst开发者_高级运维em.out.println(mThumbIds[1]);
          System.out.println(mThumbIds[2]);
          System.out.println(mThumbIds[3]);
          System.out.println(mThumbIds[4]);
    return imageView;
  }

  // references to our images
  private Integer[] mThumbIds = { R.drawable.directory_xml,
      R.drawable.news_xml, R.drawable.calendar_xml,
      R.drawable.facilities_xml, R.drawable.employee_handbook_xml,R.drawable.settings_xml };
}
}


you can use setTag() and getTag() to set or get image resource name along with imgae

when you are setting image dynamically you can add the following line to set imageresource name with the image

imageView.setTag("image resource name");

if you want to retrieve image resource name you can use

String imageName = (String) imageView.getTag();


You can use this code. This code works fine for me.

String resName = getResourceNameFromClassByID(R.drawable.class, R.drawable.imagename);

and the method is

public String getResourceNameFromClassByID(Class<?> aClass, int resourceID) 
                    throws IllegalArgumentException{
    /* Get all Fields from the class passed. */
    Field[] drawableFields = aClass.getFields();

    /* Loop through all Fields. */
    for(Field f : drawableFields){
        try {
            /* All fields within the subclasses of R 
             * are Integers, so we need no type-check here. */

            /* Compare to the resourceID we are searching. */
            if (resourceID == f.getInt(null))
                return f.getName(); // Return the name.
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /* Throw Exception if nothing was found*/
    throw new IllegalArgumentException();
}

for more details , you can refer to http://www.anddev.org/viewtopic.php?t=506


Try this

String name = getResources().getResourceEntryName(image[position]);


ImageView imgvw = (ImageView)findViewById(R.id.imageview1);

imgvw.setTag("name");

String strImgName = (String) imgvw.getTag();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号