1

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

我想做的是当点击图像时,我希望它打开点击图像的不同版本。为了争论,假设 GridView 显示没有眼睛的面孔,当您按下其中一个时,它会显示一个有眼睛的版本。

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

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


MainActivity.java listImagesFragment 提取

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
    };
}
4

0 回答 0