在使用FirebaseRecyclerAdapter从recyclerView删除数据时,IndexOutOfBoundsException

时间:2017-03-17 08:46:23

标签: android firebase firebase-realtime-database android-recyclerview

我正在使用FirebaseRecyclerAdapter在我的应用程序中使用动态的recyclerView。在每个回收商项目中,我有一个删除按钮来删除特定的recyclerView项目。但有时我在删除值时会遇到异常。我尝试了堆栈溢出的大部分解决方案都没有改变。需要你的帮助。下面我发布适配器代码。

我得到的例外是:

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
                                                                            at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                                                                            at java.util.ArrayList.get(ArrayList.java:308)
                                                                            at com.firebase.ui.database.FirebaseArray.getItem(FirebaseArray.java:52)
                                                                            at com.firebase.ui.database.FirebaseRecyclerAdapter.getRef(FirebaseRecyclerAdapter.java:150)

slcListFragment.java:

 slcList.setLayoutManager(llm);
     adapter = new FirebaseRecyclerAdapter<Slc, viewHolder>(
            Slc.class,
            R.layout.single_slc_row,
            viewHolder.class,
            FirebaseDatabase.getInstance().getReference()) {
        @Override
        protected void populateViewHolder(final viewHolder viewHolder, final Slc model, final int position)
        {

            //expand collapse
            viewHolder.expand.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    // ViewAnimationsUtils.expand(viewHolder.inv);
                    viewHolder.collapse.setVisibility(View.VISIBLE);
                    viewHolder.inv.setVisibility(View.VISIBLE);
                    viewHolder.expand.setVisibility(View.GONE);
                }
            });
            viewHolder.collapse.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    // ViewAnimationsUtils.collapse(inv);
                    viewHolder.inv.setVisibility(View.GONE);
                    viewHolder.expand.setVisibility(View.VISIBLE);
                    viewHolder.collapse.setVisibility(View.GONE);
                }
            });
            String s=adapter.getRef(position).getKey();
            viewHolder.name.setText(s);

            //dimming get
            viewHolder.dimming.setProgress(model.getDimming_val());

            //on off get
            if(model.getOn_off()==1)
            {
                viewHolder.onOff.setChecked(true);
            }
            else if(model.getOn_off()==0)
            {
                viewHolder.onOff.setChecked(false);
            }

            //dimming set
            viewHolder.dimming.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
                {
                    adapter.getRef(position).child("dimming_val").setValue(progress);

                }

                @Override
                public void onStartTrackingTouch(SeekBar seekBar) {

                }

                @Override
                public void onStopTrackingTouch(SeekBar seekBar) {

                }
            });

            viewHolder.onOff.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,
                                             boolean isChecked) {

                    if(buttonView.isPressed() &&isChecked){
                        adapter.getRef(position).child("on_off").setValue(1);


                    }else{
                        adapter.getRef(position).child("on_off").setValue(0);

                    }

                }
            });
            viewHolder.delete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v)
                {
                    final Dialog dialog = new Dialog(myContext);
                    dialog.setTitle("Delete Slc?");
                    dialog.setContentView(R.layout.delete_slc);
                    dialog.setCancelable(false);
                    Window window = dialog.getWindow();
                    if(window == null) return;
                    WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
                    lp.copyFrom(dialog.getWindow().getAttributes());
                    lp.width = WindowManager.LayoutParams.MATCH_PARENT;
                    lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
                    lp.gravity = Gravity.CENTER;
                    dialog.getWindow().setAttributes(lp);
                    TextView delete = (TextView)dialog.findViewById(R.id.deleteSlc);
                    delete.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v)
                        {
                            int pos = viewHolder.getAdapterPosition();
                            ref.child(adapter.getRef(position).getKey()).removeValue();

                            notifyItemRemoved(pos);

                           // adapter.notifyDataSetChanged();

                            dialog.dismiss();
                        }
                    });


                    TextView dialogButton = (TextView) dialog.findViewById(R.id.canceld);
                    // if button is clicked, close the custom dialog
                    dialogButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v)
                        {
                            dialog.dismiss();
                        }
                    });

                    dialog.show();

                }
            });
        }
    };

    slcList.setAdapter(adapter);

0 个答案:

没有答案