How to hold the selected image in GridView and pass to next activity after I click Button

前端 未结 2 332
借酒劲吻你
借酒劲吻你 2021-01-24 09:50

In my Activity I have an EditText, a Button and a GridView. The user selects the image from the GridView and enters the name

相关标签:
2条回答
  • 2021-01-24 10:29

    I think what you're looking for is passing data through navigation.

    It will allow you send your image and name data from one activity/fragment to the next one.

    Check out data-binding as well, might be useful.

    0 讨论(0)
  • 2021-01-24 10:50

    Copy and paste this hope it solve ur Problem

    public class NewListCreate extends BottomSheetDialogFragment {
        
        
        
            int[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
            int imageRes = images[0];
        
            public NewListCreate() {
            }
        
            @Nullable
            @Override
            public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        
                 View view = inflater.inflate(R.layout.new_list_create, container, false);
        
                ImageButton done = view.findViewById(R.id.done);
                final EditText listname = (EditText) view.findViewById(R.id.listname);
                final GridView gridView = (GridView) view.findViewById(R.id.gridview);
        
                final CustomAdpter customAdpter = new CustomAdpter(images,getContext());
                gridView.setAdapter(customAdpter);
                gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        customAdpter.selectedImage = i;
                        customAdpter.notifyDataSetChanged();
                        imageRes = images[i];
      
                    }
                });
        
        
        
        
                done.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
        
        
                        String itemname = listname.getText().toString();
                        if (!TextUtils.isEmpty(listname.getText().toString())) {
        
                            startActivity(new Intent(getContext(), CheckslateHome.class).putExtra("data", itemname).putExtra("image",imageRes));
                            dismiss();
                        } else {
                            Toast.makeText(getContext(), "List Name not Empty ", Toast.LENGTH_SHORT).show();
                        }
        
                    }
        
               });
        
                
                return view;
        
        
            }
        
        
        
        
            public class CustomAdpter extends BaseAdapter{
        
                private int[] icons;
                private Context context;
                private LayoutInflater layoutInflater;
                public int selectedImage = 0;
        
                public CustomAdpter(int[] icons, Context context) {
                    this.icons = icons;
                    this.context = context;
                    this.layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                }
        
                @Override
            public int getCount() {
                return icons.length;
            }
        
            @Override
            public Object getItem(int i) {
                return null;
            }
        
            @Override
            public long getItemId(int i) {
                return 0;
            }
        
            @Override
            public View getView(int i, View view, ViewGroup viewGroup) {
        
                    if (view == null)
                    {
                       view =  layoutInflater .inflate(R.layout.image_list,viewGroup,false);
        
                    }
        
                    ImageView imageicons = view.findViewById(R.id.image);
                if (i < icons.length) {
        
                    imageicons.setImageResource(icons[i]);
        
                    if (i != selectedImage) {
                       imageicons.setImageAlpha(50);
                    }
                    imageicons.setScaleType(ImageView.ScaleType.CENTER_CROP);
                   // imageicons.setLayoutParams(new GridView.LayoutParams(150, 150));
                    if (i ==  selectedImage) {
        
                        view.setBackgroundColor(Color.WHITE);
                    } else {
                        view.setBackgroundColor(Color.TRANSPARENT);
                    }
                };
        
        
                return view;
            }
        }
    
    0 讨论(0)
提交回复
热议问题