Pick selected image and send to another Activity

0

I wonder if it's possible to grab a selected image and send it to another activity. I'm using fragments and wanted the layout to look like the image below.

I would like that when the user selects the image, pick this one and send it to another activity and present it on the screen. Class Fragment:

public class Fragment1 extends Fragment {

GridView gridView;
int [] listaImagens = new int[]{R.drawable.eu, R.drawable.perguntas, R.drawable.respostas};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState) {

    View view = inflater.inflate(R.layout.layout_frag_1, conteiner, false);

    gridView = (GridView) view.findViewById(R.id.imageFrag1);
    gridView.setAdapter(new Adaptador(view.getContext(), listaImagens));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(parent.getContext(), "Imagem " +listaImagens[position], Toast.LENGTH_SHORT).show();
        }
    });

    return (view);

}

}

Class Adapter:

public class Fragment1 extends Fragment {

GridView gridView;
int [] listaImagens = new int[]{R.drawable.eu, R.drawable.perguntas, R.drawable.respostas};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup conteiner, Bundle saveInstanceState) {

    View view = inflater.inflate(R.layout.layout_frag_1, conteiner, false);

    gridView = (GridView) view.findViewById(R.id.imageFrag1);
    gridView.setAdapter(new Adaptador(view.getContext(), listaImagens));

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Toast.makeText(parent.getContext(), "Imagem " +listaImagens[position], Toast.LENGTH_SHORT).show();
        }
    });

    return (view);

}

}

    
asked by anonymous 25.10.2016 / 20:11

1 answer

2

You can pass her position.

Example:

gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {

            Intent i = new Intent(getActivity(), Display_Image_Activity.class);
            i.putExtra("img", listaImagens[position]);
            getActivity().startActivity(i);

        }
    });

In the Activity you will get the value of it, for example:

String img;

Bundle extras = getIntent().getExtras();
if(extras != null) {
    img = extras.getString("img");
}

Resources res = getResources();
int resID = res.getIdentifier(img , "drawable", getPackageName());
Drawable drawable = res.getDrawable(resID);

ImageView iv = new ImageView(this);
iv.setImageDrawable(drawable);

The variable img will have the value of R.drawable.questions , for example.

    
25.10.2016 / 20:19