开发者

How to get the filename showed in a toast.maketext

I made some experiments with the gallery to learn how to use it and I need to understand how I can get the filename showed by the Toast.maketext instead of the position number I tried to use getString but the emulator stops after loading the app. Here's my code maybe someone got the same problem and can suggest me a new way to get the part of the file after R.drawable.

public class DisplayViewsExample extends Activity {

//---the images to display--- Integer[] 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 = (ImageV开发者_JS百科iew) 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;
    }
}    

}

Thank you Angelo


Resources class has methods: public String getResourceEntryName (int resid) and public String getResourceName (int resid) from which you can get resouce name from 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();
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜