如何获取在toast中显示的文件名



我用图库做了一些实验来学习如何使用它,我需要了解如何获得Toast.maketext显示的文件名,而不是我试图使用getString的位置号,但模拟器在加载应用程序后停止。这是我的代码,也许有人遇到了同样的问题,可以建议我一种新的方法来获得R.drawable.之后的文件部分

公共类DisplayViewsExample扩展Activity{
//---要显示的图像---Integer[]imageID={R.drawable.slovenia,R.drawable.peru,R.drawable.canarie,R.drawable.lanzarote,R.drawable.casasco,R.drawable.avid,R.drawable.danimarca,R.drawable.eritrea};

@Override    
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.displayview);
    Gallery gallery = (Gallery) findViewById(R.id.gallery1);
    gallery.setAdapter(new ImageAdapter(this));        
    gallery.setOnItemClickListener(new OnItemClickListener() 
    {
        public void onItemClick(AdapterView parent, 
        View v, int position, long id) 
        {              
            //---display the images selected---
            ImageView imageView = (ImageView) findViewById(R.id.image1);                
            imageView.setImageResource(imageIDs[position]);
            Toast.makeText(getBaseContext(), 
                    "pic" + (position + 1) + " selected", 
                    Toast.LENGTH_SHORT).show();
        }
    });
}
public class ImageAdapter extends BaseAdapter 
{
    private Context context;
    private int itemBackground;
    public ImageAdapter(Context c) 
    {
        context = c;
        //---setting the style---
        TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
        itemBackground = a.getResourceId(
            R.styleable.Gallery1_android_galleryItemBackground, 0);
        a.recycle();                    
    }
    //---returns the number of images---
    public int getCount() {
        return imageIDs.length;
    }
    //---returns the ID of an item--- 
    public Object getItem(int position) {
        return position;
    }            
    public long getItemId(int position) {
        return position;
    }
    //---returns an ImageView view---
    public View getView(int position, View convertView, ViewGroup parent) {
        ImageView imageView = new ImageView(context);
        imageView.setImageResource(imageIDs[position]);
        imageView.setScaleType(ImageView.ScaleType.FIT_XY);
        imageView.setLayoutParams(new Gallery.LayoutParams(120, 150));
        imageView.setBackgroundResource(itemBackground);
        return imageView;
    }
}    

}

谢谢Angelo

Resources类有方法:public String getResourceEntryName (int resid)public String getResourceName (int resid),您可以从中从resourceID获取资源名称。

public void onItemClick(AdapterView parent, View v, int position, long id) {              
        //---display the images selected---
        ImageView imageView = (ImageView) findViewById(R.id.image1);                
        imageView.setImageResource(imageIDs[position]);
        Toast.makeText(getBaseContext(), 
                "pic: " + getBaseContext().getResources().getResourceEntryName(imageIDs[position]) + " selected", 
                Toast.LENGTH_SHORT).show();
}

相关内容

  • 没有找到相关文章

最新更新