简体   繁体   中英

Changing gridview item outside adapter

I have created a horizontal gridview using https://developer.android.com/reference/android/support/v17/leanback/widget/HorizontalGridView.html

my gridview contains an imageview. I want when the user clicks on a button on a screen (the button is not in the griview), a specific image from gridview gets changed. I have tried

adapter.notifyDataSetChanged()

but its not working.Here is my code:

      public class GridElementAdapter extends RecyclerView.Adapter<GridElementAdapter.SimpleViewHolder>{

            private Context context;


    public static ArrayList<File> elements;
        public static int mPosition=0;



            public GridElementAdapter(Context context, ArrayList<File> file){
                this.context = context;
                this.elements = file;


            }

            public static class SimpleViewHolder extends RecyclerView.ViewHolder {
               ImageView button;
                ImageView cancel;

                public SimpleViewHolder(View view) {
                    super(view);
                    button = (ImageView) view.findViewById(R.id.imageView2);
                    cancel = (ImageView) view.findViewById(R.id.check);
                }
            }

            @Override
            public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                final View view = LayoutInflater.from(this.context).inflate(R.layout.grid_element, parent, false);
                return new SimpleViewHolder(view);
            }

            @Override
            public void onBindViewHolder(final SimpleViewHolder holder, final int position) {
             Glide.with(context).load(elements.get(position).toString()).into(holder.button);
                holder.cancel.setImageResource(R.drawable.cancell);
                holder.cancel.setOnClickListener(new View.OnClickListener()
                {
                    @Override
                    public void onClick(View v) {
                        elements.remove(position);
                       notifyDataSetChanged();
                    }
                });



                holder.button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
mPosition=position;
                        Glide.with(context).load(elements.get(position).toString()).into(holder.button);

                    }
                });
            }


            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public int getItemCount() {
                return this.elements.size();
            }
        }

Here is the function in the mainActivity from where i have to notify the adapter.

 public void save_image(View v) throws IOException {

       File file = new File(new_name);


        GridElementAdapter.elements.remove(GridElementAdapter.mPosition);
        GridElementAdapter.elements.add(GridElementAdapter.mPosition, file);
       View v1=new View(context);
         v1 = horizontalGridView.getChildAt(GridElementAdapter.mPosition);


        ImageView someText = (ImageView) v1.findViewById(R.id.imageView2);
        Glide.with(this).load(GridElementAdapter.elements.get(GridElementAdapter.mPosition)).into(someText);
        adapter.notifyDataSetChanged();
     }

I have tried this solution but it is not working. Changing gridVIew's imageView outside of the adapter

The adapter's elements property should be an instance variable.

So instead of public static ArrayList<File> elements; it should be public ArrayList<File> elements;

Then in your save_image method you'll use

 adapter.elements.remove(GridElementAdapter.mPosition);
 adapter.elements.add(GridElementAdapter.mPosition, file);

instead of:

GridElementAdapter.elements.remove(GridElementAdapter.mPosition);
GridElementAdapter.elements.add(GridElementAdapter.mPosition, file);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM