如何在toast.maketext中显示文件名

时间:2011-06-19 20:57:17

标签: android text gallery toast

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

  

public class DisplayViewsExample extends Activity   {
      // ---要显示的图像---       整数[] imageIDs = {               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;
    }
}    

}

谢谢 安吉洛

1 个答案:

答案 0 :(得分:1)

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

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