java.lang.IllegalStateException:RecyclerView计算布局时无法调用此方法

时间:2019-09-21 10:46:02

标签: android android-studio android-recyclerview adapter recycler-adapter

有一个问题,当我打开活动并滚动到RecyclerView中的最后一行/值时。


notifyItemChanged(dataViewHolder.getAdapterPosition()); 

与此行 RecyclerView更改该值,但显示最后一个值后会导致应用程序崩溃。


 mRecyclerView.post(new Runnable() {
            @Override public void run() {
                notifyItemChanged(dataViewHolder.getAdapterPosition());
            }
                }); 

和我使用App的其他方法没有崩溃,但“最后一个值”是无限循环。我只想递增1像RecyclerView中的值最后一行的值是19,所以我使用整数+1; 和19 + num; = 20;可运行方法值中的螺母是无限循环。请帮忙 !预先感谢...

在下面查看完整代码...

public class TransactionHistoryAdapter extends 
RecyclerView.Adapter<TransactionHistoryAdapter.DataViewHolder> {

public Context context;
ArrayList<Transaction> Listtransactions ;
DataViewHolder _dataViewHolder;

public TransactionHistoryAdapter(Context context, ArrayList<Transaction> transactions) {
    this.context = context;
    this.Listtransactions = transactions;
}
@NonNull
@Override
public DataViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {


    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.payment_list,viewGroup,false);

  return new DataViewHolder(view);

}

private RecyclerView mRecyclerView;
@Override
public void onBindViewHolder(@NonNull final DataViewHolder dataViewHolder, final int position) {

    final Transaction Mtransaction = Listtransactions.get(position); //getList Data to Model
    int num = 0;
    dataViewHolder.entryDate.setText(Mtransaction.getTransactionEntryDate());
    dataViewHolder.RemainingAmount.setText(Mtransaction.getTransactionRemainingAmount());
    dataViewHolder.PaidAmount.setText(Mtransaction.getTransactionPaidAmount());

    int ListLastValue =Integer.parseInt( Listtransactions.get(Listtransactions.size() -1).getTransactionRemainingAmount());
    int ValuefrmRecycler =  Integer.parseInt( Listtransactions.get(dataViewHolder.getAdapterPosition()).getTransactionRemainingAmount());


    if(ListLastValue == ValuefrmRecycler)
    {
        num = 1;
        Listtransactions.get(dataViewHolder.getAdapterPosition()).setTransactionRemainingAmount(String.valueOf(ListLastValue + num));

            // METHOD NO.1 == App Crash   
            notifyItemChanged(dataViewHolder.getAdapterPosition());

        // METHOD NO.2 == InfiniteLoop
     /*   mRecyclerView.post(new Runnable() {
            @Override public void run() {
                //  notifyDataSetChanged();
                notifyItemChanged(dataViewHolder.getAdapterPosition());
            }
        }); */

        }
}



@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
    super.onAttachedToRecyclerView(recyclerView);
    mRecyclerView = recyclerView;
}



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


public class DataViewHolder extends RecyclerView.ViewHolder {

    TextView entryDate,PaidAmount,RemainingAmount;

    public DataViewHolder(View view) {
        super(view);

        entryDate=(TextView) view.findViewById(R.id.tvdate);
        PaidAmount = (TextView) view.findViewById(R.id.tvPaid);
        RemainingAmount = (TextView) view.findViewById(R.id.tvRemaining);

    }
}

}

0 个答案:

没有答案