我的应用程序中有一个RecyclerView,我通过手动搜索在购物车中添加项目。每行都有一个名称以及一个可以输入值的编辑文本。我使用ArrayList来存储使用TextWatcher输入编辑文本的数据。例如,假设我将两个或三个项目(行)添加到RecyclerView并尝试输入值不在第一行中的第一个编辑文本,从第二个或第三个项目(行),我收到 IndexOutOfBoundsException 错误。我尝试了很多方法。什么都行不通。
这是我的Adapter类。
public class SelectItemAdapter extends RecyclerView.Adapter<SelectItemAdapter.ItemHolder> {
private List<String> itemsName, itemsQty, itemsPCode, itemPlant, _retData;
private OnItemClickListener onItemClickListener;
private LayoutInflater layoutInflater;
public ArrayList myItems = new ArrayList();
private Context context;
public SelectItemAdapter(Context context) {
layoutInflater = LayoutInflater.from(context);
itemsName = new ArrayList<String>();
itemsQty = new ArrayList<String>();
itemsPCode = new ArrayList<String>();
itemPlant = new ArrayList<String>();
_retData = new ArrayList<String>();
}
@Override
public SelectItemAdapter.ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = layoutInflater.inflate(R.layout.custom_row_selected_item, parent, false);
return new ItemHolder(itemView, this);
}
@Override
public void onBindViewHolder(final SelectItemAdapter.ItemHolder holder, final int position) {
holder.setItemName(itemsName.get(position));
holder.setItemQty(itemsQty.get(position));
holder.setItemPCode(itemsPCode.get(position));
holder.setItemPlant(itemPlant.get(position));
holder.numPicker.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
_retData.add(position, arg0.toString());
}
});
}
@Override
public int getItemViewType(int position) {
return position;
}
public String retrieveData(int i) {
return _retData.get(i);
}
@Override
public int getItemCount() {
return itemsName.size();
}
public Object getItemName(int position) {
return itemsName.get(position);
}
public Object getItemQty(int position) {
return itemsQty.get(position);
}
public Object getItemPCode(int position) {
return itemsPCode.get(position);
}
public Object getItemPlant(int position) {
return itemPlant.get(position);
}
public void setOnItemClickListener(OnItemClickListener listener) {
onItemClickListener = listener;
}
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;
}
public interface OnItemClickListener {
public void onItemClick(ItemHolder item, int position);
}
public void add(int location, String iName, String iQty, String iPCode, String iPlant) {
itemsName.add(location, iName);
itemsQty.add(location, iQty);
itemsPCode.add(location, iPCode);
itemPlant.add(location, iPlant);
notifyItemInserted(location);
}
public void remove(int location) {
if (location >= itemsName.size())
return;
itemsName.remove(location);
notifyItemRemoved(location);
}
public static class ItemHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
private SelectItemAdapter parent;
TextView textItemName, txtPCode, txtAvailableQty, txtTempQty, txtPlant;
Button bRemove;
EditText numPicker;
public ItemHolder(View itemView, SelectItemAdapter parent) {
super(itemView);
this.parent = parent;
textItemName = (TextView) itemView.findViewById(R.id.txtProductName);
txtAvailableQty = (TextView) itemView.findViewById(R.id.txtAvailbleQty);
txtPCode = (TextView) itemView.findViewById(R.id.txtPCode);
txtPlant = (TextView) itemView.findViewById(R.id.txtPlant);
bRemove = (Button) itemView.findViewById(R.id.bRemove);
numPicker = (EditText) itemView.findViewById(R.id.numberPicker);
bRemove.setOnClickListener(this);
}
public void setItemName(CharSequence name) {
textItemName.setText(name);
}
public void setItemQty(CharSequence name) {
txtAvailableQty.setText(name);
}
public void setItemPCode(CharSequence name) {
txtPCode.setText(name);
}
public void setItemPlant(CharSequence name) {
txtPlant.setText(name);
}
public String getQtyNumber() {
return numPicker.getText().toString();
}
public CharSequence getItemName() {
return textItemName.getText();
}
@Override
public void onClick(View v) {
final OnItemClickListener listener = parent.getOnItemClickListener();
if (listener != null) {
listener.onItemClick(this, getPosition());
}
}
}
public class RetItem {
public String _itemNumPic;
}
}
我的logCat。
05-31 19:39:57.394 30412-30412/com.ceatkelanisrilanka.dushanmadushanka.ceat E/InputEventSender: Exception dispatching finished signal.
05-31 19:39:57.394 30412-30412/com.ceatkelanisrilanka.dushanmadushanka.ceat E/MessageQueue-JNI: Exception in MessageQueue callback: handleReceiveCallback
05-31 19:39:57.397 30412-30412/com.ceatkelanisrilanka.dushanmadushanka.ceat E/MessageQueue-JNI: java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.add(ArrayList.java:147)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.SelectItemAdapter$1.afterTextChanged(SelectItemAdapter.java:73)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7695)
at android.widget.TextView$ChangeWatcher.afterTextChanged(TextView.java:9483)
at android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:972)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:516)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:454)
at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:33)
at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:121)
at android.widget.TextView.doKeyDown(TextView.java:5803)
at android.widget.TextView.onKeyDown(TextView.java:5616)
at android.view.KeyEvent.dispatch(KeyEvent.java:2619)
at android.view.View.dispatchKeyEvent(View.java:8411)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1495)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2361)
at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1709)
at android.app.Activity.dispatchKeyEvent(Activity.java:2702)
at android.support.v7.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:534)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:241)
at android.support.v7.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2276)
at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4020)
at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3982)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3713)
at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3874)
at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2208)
at android.view.inputmethod.InputMethodManager
答案 0 :(得分:1)
您在开头的变量_retData大小为0.当您尝试在第二个位置添加项目时,它超出了界限。
您必须确保列出的尺寸与回收商的尺寸相同
为了做到这一点,你必须采取措施:
1.在你的添加功能中添加:
_retData.add(location, new String());
删除功能中的添加:
_retData.remove(location);
2。第二种方式,我认为更好,就是创建一个名为MyItem的类,让它有字段:Name,Qty,Pcode,Plant,retData。
然后,在您的回收商中创建一个MyItem列表,然后您需要密切关注其大小。它也更方便。