如何获取图像资源名称?

时间:2013-09-13 11:59:33

标签: java android

在我的 MainActivity 中,我有一个名为 listImagesFragment 的片段,它使用GridView在网格中布局我的图像。在那个Fragment中,我有一个ItemClickListerner,所以当点击其中一个图像时,它会启动另一个活动并显示该图像。

我想要做的是点击图片时,我希望打开点击图片的不同版本。为了论证,让我们说GridView显示没有眼睛的面孔,当你按下其中一个时,它会用眼睛显示它的一个版本。

我计划这样做的方式是以某种方式获取gridview中显示的图像的资源名称,然后将其与字符串连接。将此新名称传递给我的单个图像活动(guesser.java),这将加载另一个图像 例如。 GridView图像资源可能被称为ic_noface1,而另一个图像的资源可能是ic_noface1_withface。

如何获取我按下的图像的资源名称? 我目前的代码如下:


MainActivity.java listImagesFragement extract

public class listImagesFragment extends Fragment {

    @Override
    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.gridview,container,false);
        GridView gridView = (GridView) view.findViewById(R.id.gridview);
        gridView.setAdapter(new RestaurantImageAdapter(view.getContext()));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                Intent i = new Intent(getActivity(), Guesser.class );
                i.putExtra("id", position);
                startActivity(i);
            }
        });
        return view;
    }
}

Guesser.java

public class Guesser extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Get the view from singleitemview.xml
        setContentView(R.layout.guesser);

        // Get position from intent passed from MainActivity.java
        Intent i = getIntent();

        int position = i.getExtras().getInt("id");

        // Open the Image adapter
        RestaurantImageAdapter imageAdapter = new RestaurantImageAdapter(this);

        // Locate the ImageView in single_item_view.xml
        ImageView imageView = (ImageView) findViewById(R.id.imageView);
        // Get image and position from ImageAdapter.java and set into ImageView
        imageView.setImageResource(imageAdapter.mThumbIds[position]);
    }

}

RestaurantImageAdapter.java 生成gridview

public class RestaurantImageAdapter extends BaseAdapter {

    private Context mContext;

    public RestaurantImageAdapter(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) {
        ImageView imageView;
        if (convertView == null) {  // if it's not recycled, initialize some attributes
            imageView = new ImageView(mContext);
        } else {
            imageView = (ImageView) convertView;
        }

        imageView.setImageResource(mThumbIds[position]);
        return imageView;
    }

    // references to our images
    public Integer[] mThumbIds = {
            R.drawable.r_tgi_blank, R.drawable.r_krispykreme_blank,
            R.drawable.r_greggs_blank, R.drawable.r_nandos_blank,
            R.drawable.r_dennys_blank, R.drawable.r_ihop_blank,
            R.drawable.r_fb_blank, R.drawable.r_wagamama_blank,
            R.drawable.r_subway_blank, R.drawable.r_bk_blank,
            R.drawable.r_zizzi_blank, R.drawable.r_taco_blank,
            R.drawable.r_starbucks_blank, R.drawable.r_bennigans_blank,
            R.drawable.r_applebees_blank
    };
}

0 个答案:

没有答案