从另一个适配器刷新recyclerview适配器的值

时间:2016-10-11 05:37:30

标签: android notifydatasetchanged

我必须在一个活动中使用两个具有不同适配器的数据集,并且我想使用notifyDataSetChanged()但是它不适用于另一个适配器。所以在我的adapterFirst课程中,我LongCLickListener正在使用notifyDataSetChanged(),我想调用adapterSecond来通知adapterFirst是否更新。按LongClick

时,AdapterSecond数据未更新
 public class AdapterFirst extends RecyclerView.Adapter <AdapterFirst .ViewHolder> {

            private List<Object> objectList;
            private Context mContext;
            AdapterSecond adapterSecond;

            public AdapteFirst(List<Object> list) { objectList= list; }

            @Override
            public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                View view = LayoutInflater.from(parent.getContext())
                     .inflate(R.layout.item_list, parent, false);
                return new ViewHolder(view);
            }

          @Override
            public void onBindViewHolder(final ViewHolder holder, int position) {

            holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View v) {
                         //query method
                         notifyDataSetChanged(); //working

                         //here it gets the nullpointerexception error
                         adapterSecond = new AdapteSecond();
                         adapterSecond.notifyDataSetChanged(); 
                    }
            });
      }

        @Override
            public int getItemCount() {
                return (objectList != null? objectList.size():0);
            }

            public class ViewHolder extends RecyclerView.ViewHolder {
                public TextView data;

                public ViewHolder(View itemView) {
                    super(itemView);
                    data= (TextView) itemView.findViewById(R.id.text);        
                }
            }
        }

AdapterSecond

    public class AdapterSecond extends 
                  RecyclerView.Adapter <AdapterSecond.ViewHolder> {

        private List<Object> objectList;
        private Context mContext;

        public AdapterSecond () {}

        public AdapterSecond (List<Object> list) { objectList= list; }

        @Override
        public AdapterSecond.ViewHolder onCreateViewHolder(ViewGroup parent, 
                        int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                   .inflate(R.layout.item_list_2, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(final AdapterSecond.ViewHolder holder, int position) {

            final Object object = objectList.get(holder.getAdapterPosition());
            holder.data2.setText(object.getData());

         }    
        @Override
        public int getItemCount() {
            return (objectList != null? objectList.size():0);
        }

        public class ViewHolder extends RecyclerView.ViewHolder {
            public TextView data2;

            public ViewHolder(View itemView) {
                super(itemView);
                data2= (TextView) itemView.findViewById(R.id.text2);
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

似乎适配器的初始化顺序有一些问题。

确保在firstAdapter

之前初始化secondAdapter
secondAdapter= new SecondAdapter();
firstAdapter = new FirstAdapter();

只需检查它们初始化的顺序!