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

Deadly 提交于 2020-08-20 06:50:09

问题


In my Activity I have an EditText, a Button and a GridView. The user selects the image from the GridView and enters the name in the EditText, and then clicks the done button. activity.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/l1"
   >
    <View
        android:layout_width="@dimen/btm_sht_line"
        android:layout_height="@dimen/btm_sht_height"
        android:layout_marginTop="@dimen/btm_top"
        android:layout_gravity="center_horizontal"
        android:background="@color/colorPrimaryDark"

        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/header_create_a_new_list"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:textStyle="bold"
        />
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:id="@+id/l2"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="@dimen/marin_top"
        >
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listname"
        android:hint="@string/list_title_name"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="@dimen/marin_top"
        android:layout_marginStart="@dimen/marin_top"
        android:inputType="text"
        android:autofillHints="@string/list_title_name" />
        <ImageButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/done"
            android:src="@drawable/ic_check_circle"
            android:layout_marginRight="15dp"
            android:layout_marginEnd="@dimen/marin_top"
            android:layout_marginLeft="@dimen/margin_left"
            android:layout_marginStart="@dimen/margin_left"
            tools:ignore="ContentDescription"
            />   </LinearLayout>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/l3"
            android:orientation="vertical"

            >

        <View style="@style/Divider"
            android:layout_marginTop="@dimen/marin_top"
            />

        </LinearLayout>
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/r1"
    >
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scroll"
        ><GridView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/gridview"
        android:columnWidth="90dp"
        android:numColumns="auto_fit"
        android:verticalSpacing="10dp"
        android:horizontalSpacing="10dp"
        android:stretchMode="columnWidth"
        android:gravity="center"
        />
    </ScrollView>
</RelativeLayout>
</LinearLayout>

I want to move the selected image and the entered text to another Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".CheckslateHome">
    <ImageView
        android:layout_width="90dp"
        android:layout_height="90dp"
        android:id="@+id/ima"
        />
<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/getlistname"
    android:text="hi welcome"
android:layout_below="@+id/ima"
    />


</RelativeLayout>

In my code when the user selects the image, before entering the name, it loads to a new empty window. Also I have 6 images in my drawable Folder and only 3 images are being displayed in the GridView.

Java class

public class NewListCreate extends BottomSheetDialogFragment {

    Integer[] images={R.drawable.menu,R.drawable.musicbox,R.drawable.shoppingbag,R.drawable.shoppingcart,R.drawable.wallet,R.drawable.weddingdress};
    GridView gridView;
    ArrayList<imageModel> arrayList;

    public NewListCreate() {}
    
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    
        final 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);
        arrayList = new ArrayList<imageModel>();
        for (int i = 0; i < images.length; i++) {
            imageModel imagemodel = new imageModel();
            imagemodel.setmThumbIds(images[i]);
            //add in array list
            arrayList.add(imagemodel);
        }
          final ImageAdapterGridView adapterGridView = new ImageAdapterGridView(getContext(), arrayList);

    gridView.setAdapter(adapterGridView);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            adapterGridView.setSelectedPosition(i);
            adapterGridView.notifyDataSetChanged();
            int imageRes = images[i];
           Intent intent = new Intent(getContext(),CheckslateHome.class);
          intent.putExtra("IMAGE_RES", imageRes);
           startActivity(intent);
        }
        });
        return view;
    }
}

AdapterClass

 public class ImageAdapterGridView extends BaseAdapter {
        private int selectedPosition = -1;
        Context context;
        ArrayList<imageModel> arrayList;
    
        public ImageAdapterGridView(Context context, ArrayList<imageModel> arrayList) {
            this.context = context;
            this.arrayList = arrayList;
        }
    
        @Override
        public int getCount() {
            return arrayList.size();
        }
    
        @Override
        public Object getItem(int i) {
            return arrayList.get(i);
        }
    
        @Override
        public long getItemId(int i) {
            return i;
        }
    
        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
    
            if (view==null)
            {
               view = LayoutInflater.from(context).inflate(R.layout.image_list, viewGroup, false);
            }
    
            ImageView imageView;
            imageView = (ImageView) view.findViewById(R.id.image);
            imageView.setImageResource(arrayList.get(i).getmThumbIds());
    
            if (i == selectedPosition) {
    
                view.setBackgroundColor(Color.WHITE);
            } else {
                view.setBackgroundColor(Color.TRANSPARENT);
            }
            return view;
    
    
        }
    
        public void setSelectedPosition(int position) {
            selectedPosition = position;
        }
    }

what i was Looking was Something Like this [![enter image description here][1]][1]


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/63414396/how-to-hold-the-selected-image-in-gridview-and-pass-to-next-activity-after-i-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!