notifydatasetchanged在适配器上不起作用

时间:2018-09-28 09:14:13

标签: android android-recyclerview recycler-adapter bottom-sheet

我正在从此适配器显示付款对话框,但是付款提交后它没有更新。如何更新视图?

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

    private Context mContext;
    private final LayoutInflater mLayoutInflater;
    private final List<Debt> mDebtList;


    public PersonDebtDetailsAdapter(Context context,  List<Debt> debtList) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(context);
        mDebtList = debtList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = mLayoutInflater.inflate(R.layout.item_person_debt, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        final int debtType = mDebtList.get(position).getDebtType();
        if (debtType == Debt.DEBT_TYPE_IOWE){
            holder.mDebtType.setText("OWE BY ME");
        }else {
            holder.mDebtType.setText("OWE TO ME");
        }

        holder.mDebtNote.setText(mDebtList.get(position).getNote());
        holder.mDebtAmount.setText(String.valueOf(mDebtList.get(position).getAmount()));
        long dueDate = mDebtList.get(position).getDueDate();
        if (Calendar.getInstance().getTimeInMillis() > dueDate) {
            holder.mDebtDueDate.setTextColor(Color.RED);
        }
        holder.mDebtDueDate.setText("Due Date : " + getDate(dueDate));

        holder.amountToTrans = mDebtList.get(position).getAmount();
        holder.debtId = mDebtList.get(position).getId();
        holder.phoneNo = mDebtList.get(position).getPersonPhoneNumber();

    }

    private String getDate(long date) {
        SimpleDateFormat df = new SimpleDateFormat("EEEE,dd MMM,yyyy");
        String formattedDate = df.format(date);
        return formattedDate;
    }

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

     public class ViewHolder extends RecyclerView.ViewHolder{

        public final TextView mDebtAmount, mDebtNote, mDebtDueDate, mDebtType, mDebtPay;
         private long mDateLong;
         String  debtId, phoneNo;
         double amountToTrans;
         private Button mDate;
         private Calendar myCalendar;
         private TextView mAmount;
         private TextInputLayout mAmountLayout;

         public ViewHolder(View itemView) {
            super(itemView);

            mDebtAmount = (TextView) itemView.findViewById(R.id.tv_debt_amount);
            mDebtDueDate = (TextView) itemView.findViewById(R.id.tv_debt_due_date);
            mDebtNote = (TextView) itemView.findViewById(R.id.tv_debt_note);
            mDebtType = (TextView) itemView.findViewById(R.id.tv_debt_type);
            mDebtPay = (TextView) itemView.findViewById(R.id.tv_payment);
            mDebtPay.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ViewDialog dialog = new ViewDialog(mContext);
                    dialog.showDialogForPayment(amountToTrans, phoneNo, debtId);
                }
            });

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                }
            });
        }
     }
}

onClick in视图会放大对话框,对话框页面代码如下:

  public void showDialogForPayment(final double amount, final String phoneNo, final String debtId){
        final Dialog dialog = new Dialog(mContext);
        dialog.setContentView(R.layout.content_payment);
        dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        dialog.show();

        mDateHelper = new DateHelper();
        myCalender = Calendar.getInstance();

        mAmountLayout = (TextInputLayout) dialog.findViewById(R.id.til_amount_payment);
        mAmountEditText = (EditText) dialog.findViewById(R.id.et_amount_payment);
        mAmountEditText.setText(String.valueOf(amount));
        mNoteEditText = (EditText) dialog.findViewById(R.id.et_comment_payment);
        mDateButton = (Button) dialog.findViewById(R.id.btn_date_created_payment);
        mDateButton.setText("CREATED ON : " + getCurrentDate());
        mCloseDialog = (ImageView) dialog.findViewById(R.id.iv_close_dialog_payment);
        mCloseDialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        TextView proceed = (TextView) dialog.findViewById(R.id.tv_proceed);
        proceed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                double amountDouble = Double.parseDouble(mAmountEditText.getText().toString().trim());
                String noteString = mNoteEditText.getText().toString().trim();
                if (validateAmount(amountDouble, amount)) {
                    Payment payment = new Payment.Builder().amount(amountDouble)
                            .dateEntered(mDateLong).note(noteString).id(UUID.randomUUID().toString())
                            .personPhoneNumber(phoneNo).debtId(debtId).build();
                    DataManager dm = new DataManager(mContext);
                    dm.savePayment(payment);
                    Snackbar.make(v, "Payment Successful", Snackbar.LENGTH_SHORT).show();
                    dialog.dismiss();
                }
            }
        });

        final DatePickerDialog.OnDateSetListener dateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
                myCalender.set(Calendar.DATE, dayOfMonth);
                myCalender.set(Calendar.MONTH, month);
                myCalender.set(Calendar.YEAR, year);
                mDateLong = myCalender.getTimeInMillis();
                updateLabelDateLong();
            }
        };


        mDateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new DatePickerDialog(mContext, dateSetListener, myCalender
                        .get(Calendar.YEAR), myCalender.get(Calendar.MONTH),
                        myCalender.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    }

请根据代码中的更正建议向我提出建议。 并且发生了另一个问题,即BottomSheet减小了它的大小,直至活动工具栏。

2 个答案:

答案 0 :(得分:0)

添加notifyDataSetChanged()。

  mDebtPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ViewDialog dialog = new ViewDialog(mContext);
                        dialog.showDialogForPayment(amountToTrans, phoneNo, debtId);
                        notifyDataSetChanged();
                    }
                });

答案 1 :(得分:0)

点击

**onBindViewHolder**{
----------------------

     holder.mDebtPay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ViewDialog dialog = new ViewDialog(mContext);
                        dialog.showDialogForPayment(amountToTrans, phoneNo, debtId);
                    }
                });

    }

比对话框中

     proceed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
   ------------------------
   -------------------------
                    dialog.dismiss();
                notifyDataSetChanged();
                }
            }
        });

注意:如果未在arraylist中进行至少一次更改,则不会影响列表