删除项目时,Recyclerview超出范围

时间:2018-07-13 12:36:03

标签: java android android-recyclerview android-edittext

我将尝试使用文本解释一切。

场景是我的Edit text的每个项目中都有recycler view。 我将edittext的输入保存在模型中,并将其存储在数组中。

我的错误是:

  1. 我的问题是当列表中有3个项目显示3个edittext时 当我尝试在第2项的Edittext中输入值时,请将第1项和第3项edittext留空。

我试图删除项目1,项目2编辑文本的值应该转移到项目1,但是项目2编辑文本输入已消失/删除。

  1. 当列表中有4个项目显示4个编辑文本时,当我尝试将值仅放在项目3个编辑文本上并将每个项目(n)编辑文本留空时。

我试图删除项目1。项目4将被删除,这是正确的,然后项目3的值将被转移到项目2,这也是正确的。

现在对于项1的另一次删除,突然出现了错误,该错误应将项2的值传输到项1,而项3应该删除。

这是logcat:

  

mobilebanking.arrayadapter.CreditCard_PostPayAdapter $ 6.onTextChanged(CreditCard_PostPayAdapter.java:179)           在android.widget.TextView.sendOnTextChanged(TextView.java:8226)           在android.widget.TextView.setText(TextView.java:4504)           在android.widget.TextView.setText(TextView.java:4358)           在android.widget.EditText.setText(EditText.java:93)           在android.widget.TextView.setText(TextView.java:4333)           在com.aub.mobilebanking.arrayadapter.CreditCard_PostPayAdapter.onBindViewHolder(CreditCard_PostPayAdapter.java:89)           在com.aub.mobilebanking.arrayadapter.CreditCard_PostPayAdapter.onBindViewHolder(CreditCard_PostPayAdapter.java:46)

这是我的适配器:

public class CreditCard_PostPayAdapter extends RecyclerView.Adapter<CreditCard_PostPayAdapter.MyViewHolder> {
    private static  String TAGedittext = "";
    //private final AccountHistoryTransactionActivity homeActivity;
    private Context mContext;
    CreditCardPostPayItem creditCardPostPayItem = new CreditCardPostPayItem();
    CreditCard_PostPayAdapter adapter;

    public CreditCard_PostPayAdapter(Context mContext, ArrayList<Integer> mHeaderTextList ) {
        this.mContext = mContext;
        this.mHeaderText = mHeaderTextList;
    }

    @Override
    public CreditCard_PostPayAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.creditcard_postpay_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final CreditCard_PostPayAdapter.MyViewHolder holder, int position) {
        final int pos = position + 1;
        final int mPosition = position;

        holder.mHeaderTitle.setText("Transaction " + pos);
        holder.mTransactionAmountEditText.setText(CreditCard_PostPayActivity.CreditCardPostPayItemArray.get(mPosition).getmTransactionAmount().toString());

            holder.mDeleteButton.setVisibility(View.VISIBLE); }
            holder.mDeleteButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mHeaderText.remove(mPosition);
                CreditCard_PostPayActivity.CreditCardPostPayItemArray.remove(mPosition);
                notifyDataSetChanged();
            }
        });

        holder.mTransactionAmountEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!holder.mTransactionAmountEditText.getText().toString().equalsIgnoreCase("")){
                    if (mPosition <= CreditCard_PostPayActivity.CreditCardPostPayItemArray.size()) {
                        CreditCardPostPayItem tmpExist = new CreditCardPostPayItem();
                        Double mTransactionAmountValue = Double.parseDouble(holder.mTransactionAmountEditText.getText().toString());
                        tmpExist.setmTransactionAmount(mTransactionAmountValue);
                        CreditCard_PostPayActivity.CreditCardPostPayItemArray.set(mPosition, tmpExist);  
                    }
                } else {
                    CreditCardPostPayItem tmp = new CreditCardPostPayItem();
                    tmp.setmTransactionAmount(0.0);
                    CreditCard_PostPayActivity.CreditCardPostPayItemArray.set(mPosition, tmp);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {

        public TextView mHeaderTitle,mMerchantName;
        public ImageButton mDeleteButton;
        public TextInputEditText 
                mTransactionAmountEditText;

        public MyViewHolder(View view) {
            super(view);
            this.mHeaderTitle = (TextView) view.findViewById(R.id.header_title);

            this.mDeleteButton = view.findViewById(R.id.mDeleteButton);

            this.mTransactionAmountEditText = view.findViewById(R.id.Transaction_amount);
        }
    }

}

logcat显示错误的错误是:

holder.mTransactionAmountEditText.setText(CreditCard_PostPayActivity.CreditCardPostPayItemArray.get(mPosition).getmTransactionAmount().toString());

CreditCard_PostPayActivity.CreditCardPostPayItemArray.set(mPosition, tmpExist);  

在我的主要活动中,我有一个按钮,它将向recyclerview添加一个项目,此代码可能有助于更好地理解: 这是我的主要活动:

public class CreditCard_PostPayActivity extends ActivityOnHome {
    public static ArrayList<CreditCardPostPayItem> CreditCardPostPayItemArray = new ArrayList<>();
    CreditCard_PostPayAdapter creditCard_postPayAdapter;
    private ArrayList<Integer> mBalanceHeaderText = new ArrayList<Integer>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_credit_card_post_pay);
        mAddBalanceImage = findViewById(R.id.addBalanceImageButton);
        mAddBalanceImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                AddAnotherBalance();
            }
        });

        mButton_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                for (int i = 0;CreditCardPostPayItemArray.size()-1
                        >= i; i++ ) {
                    Log.d("ARRAY",""+CreditCardPostPayItemArray .get(i).getmTransactionAmount() +"Position"+i);
                }
            }
        });
    }

    public void AddAnotherBalance() {
        if (mBalanceHeaderText.size() != Constants.POSTPAY_TRANSACTION_LIMIT) {
            mBalanceHeaderText.add(mBalanceHeaderText.size());
            CreditCardPostPayItem tmp = new CreditCardPostPayItem();
            tmp.setmTransactionAmount(0.0);
            CreditCard_PostPayActivity.CreditCardPostPayItemArray.add(mBalanceHeaderText.size() - 1 , tmp);
            creditCard_postPayAdapter.notifyDataSetChanged(); }
        else{
            showDialog("Maximum of " +Constants.POSTPAY_TRANSACTION_LIMIT+ " Transaction only."); } }
    }

}

0 个答案:

没有答案