我有一份国家/地区列表以及有关其货币的双倍价值。当我编辑其中一种货币时,我只想更新列表中的所有其他货币,但我收到此错误:
java.lang.IllegalStateException: Cannot call this method while RecyclerView is computing a layout or scrolling android.support.v7.widget.RecyclerView{f4f8145 VFED..... .F....ID 0,32-736,1022 #7f070054 app:id/recyc}, adapter:app.ehsan.currencyapp.CurrencyAdapter@e9b409a, layout:android.support.v7.widget.LinearLayoutManager@6e59dcb, context:app.ehsan.currencyapp.MainActivity@7b7f00f
at android.support.v7.widget.RecyclerView.assertNotInLayoutOrScroll(RecyclerView.java:2663)
at android.support.v7.widget.RecyclerView$RecyclerViewDataObserver.onChanged(RecyclerView.java:5055)
at android.support.v7.widget.RecyclerView$AdapterDataObservable.notifyChanged(RecyclerView.java:11540)
at android.support.v7.widget.RecyclerView$Adapter.notifyDataSetChanged(RecyclerView.java:6762)
由于这个错误,我在代码中使用了Handler,如下所示:
G.handler.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
但是当我尝试编辑一行应用时,这个更改将不会以任何方式响应(应用程序将锁定!)。
这是我的适配器代码:
public class CurrencyAdapter extends RecyclerView.Adapter<CurrencyAdapter.ViewHolder> {
private Context mContext;
private List<CurrencyModel> mList;
private MainActivity activity;
public CurrencyAdapter(Context mContext, List<CurrencyModel> mList) {
this.mContext = mContext;
this.mList = mList;
this.activity = (MainActivity) mContext;
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView txtName;
ImageView imgIcon;
EditText edtInput;
public ViewHolder(View v) {
super(v);
txtName = (TextView) v.findViewById(R.id.txt_name);
imgIcon = (ImageView) v.findViewById(R.id.img_icon);
edtInput = (EditText) v.findViewById(R.id.edt_input);
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// Create a new View
final View v = LayoutInflater.from(mContext).inflate(R.layout.list, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
@Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
holder.txtName.setText(mList.get(position).getName());
switch (mList.get(position).getName()) {
case "dollar":
holder.imgIcon.setImageResource(R.drawable.dollar);
break;
case "pound":
holder.imgIcon.setImageResource(R.drawable.pound);
break;
case "euro":
holder.imgIcon.setImageResource(R.drawable.euro);
break;
}
holder.edtInput.setText("" + (int) mList.get(position).getValue());
holder.edtInput.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) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!holder.edtInput.getText().toString().isEmpty())
convertor(position, holder);
}
});
}
private void convertor(int position, ViewHolder holder) {
double value = 0;
switch (mList.get(position).getName()) {
case "dollar":
value = Integer.parseInt(String.valueOf(holder.edtInput.getText())) * 3200;
break;
case "pound":
value = Integer.parseInt(String.valueOf(holder.edtInput.getText())) * 4100;
break;
case "euro":
value = Integer.parseInt(String.valueOf(holder.edtInput.getText())) * 3400;
break;
}
for (int i = 0; i < mList.size(); i++) {
if (position != i) {
switch (mList.get(position).getName()) {
case "dollar":
mList.get(i).setValue(value / 3200);
break;
case "pound":
mList.get(i).setValue(value / 4100);
break;
case "euro":
mList.get(i).setValue(value / 3400);
break;
}
}
}
G.handler.post(new Runnable() {
@Override
public void run() {
notifyDataSetChanged();
}
});
}
@Override
public int getItemCount() {
return mList.size();
}
}