Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to get image resource name?
    primarykey
    data
    text
    <p>In my <strong>MainActivity</strong>, I have a Fragment called <strong>listImagesFragment</strong> which uses GridView to layout my images in a grid. Within that Fragment, I have an ItemClickListerner, so when one of the images is clicked, it starts another activity and displays that image.</p> <p>What I would like to do is when an image is clicked, I would like it to open up a different version of the image clicked. For arguments sake, lets say the GridView shows faces without the eyes, and when you press one of them, it shows a version of it with eyes.</p> <p>The way I planned to do this was to somehow get the resource name of the image displayed in the gridview, and then concatenate it with a string. Pass this new name to my single image activity (guesser.java) which would load up the other image.<br> For example. GridView image resource might be called ic_noface1 and the resource of the other image would be ic_noface1_withface. </p> <p>How would I go about getting the resource name of an image I have pressed? My current code is below:</p> <hr> <p><strong>MainActivity.java</strong> <em>listImagesFragement extract</em></p> <pre><code>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&lt;?&gt; parent, View v, int position, long id) { Intent i = new Intent(getActivity(), Guesser.class ); i.putExtra("id", position); startActivity(i); } }); return view; } } </code></pre> <p><strong>Guesser.java</strong> </p> <pre><code>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]); } } </code></pre> <p><strong>RestaurantImageAdapter.java</strong> <em>Generates the gridview</em></p> <pre><code>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 }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload