简体   繁体   English

在适配器外部更改GridView项目

[英]Changing gridview item outside adapter

I have created a horizontal gridview using https://developer.android.com/reference/android/support/v17/leanback/widget/HorizontalGridView.html 我已经使用https://developer.android.com/reference/android/support/v17/leanback/widget/Horizo​​ntalGridView.html创建了一个水平gridview

my gridview contains an imageview. 我的gridview包含一个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. 我想要当用户单击屏幕上的按钮(该按钮不在griview中)时,gridview中的特定图像被更改。 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. 这是mainActivity中必须通知适配器的函数。

 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 在适配器外部更改gridVIew的imageView

The adapter's elements property should be an instance variable. 适配器的elements属性应该是一个实例变量。

So instead of public static ArrayList<File> elements; 因此,代替public static ArrayList<File> elements; it should be public ArrayList<File> elements; 它应该是public ArrayList<File> elements;

Then in your save_image method you'll use 然后在您的save_image方法中使用

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

instead of: 代替:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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