如何获取动态设置的 imageview 资源名称?

这是图像适配器代码:

  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]);
          System.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 };
}
}

最佳答案

您可以使用 setTag()getTag() 设置或获取图像资源名称以及 imgae

当您动态设置图像时,您可以添加以下行以使用图像设置图像资源名称

imageView.setTag("image resource name");

如果你想检索图像资源名称,你可以使用
String imageName = (String) imageView.getTag();

关于android - 如何获取图片资源名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6449654/

10-12 05:48