get ImageView's image and send it to an activity with Intent

前端 未结 7 1528
孤街浪徒
孤街浪徒 2020-12-06 03:22

I have a grid of many products in my app. when the user selects one of the item in the grid, I am starting a new activity as DIALOG box and display the item\'s name,quantity

7条回答
  •  庸人自扰
    2020-12-06 04:07

    i had the same problem and solve it with passing the position of selected item via intent. as i think and read from professionals in stack its the best solution. just do that in FirstActivity.java that Contains GridView

    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) {
    
        Intent intent = new Intent(getActivity(),DetailActivity.class);
        intent.putExtra("id",position);
         startActivity(intent);    } });
    

    and then in your destination Activity you must get the position from extras and extract it from your database or where your images array are cast:

    Intent intent = getActivity().getIntent();
                int position = intent.getIntExtra("id",0);
                ImageAdapter imageAdapter = new ImageAdapter(getActivity());
                ImageView imageView = (ImageView)rootView.findViewById(R.id.movie_detail_image);
                imageView.setImageResource((int)imageAdapter.getItem(position));
    

    you must edit your image adapter getItem method to return selected image id.

提交回复
热议问题