我的应用中有AutoCmpleteTextView和RecyclerView。当我从AutCompleteTextView搜索并选择一个项目时,它将添加到RecyclerView。它工作得很好。但我想知道所选项目是否已存在于RecyclerView中。避免重复项目。
这是我的代码。
final AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
adapter = new MaterialSuggestionAdapter(getApplicationContext());
acTextView.setAdapter(adapter);
acTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Product result = adapter.getItem(position);
String newName = result.getMatName().toString();
String newQty = String.valueOf(result.getMatQuantity());
String newPCode = result.getMatNo().toString();
String newPlant = result.getMatPlant().toString();
if (!newName.equals("")) {
if (myRecyclerViewAdapter.getItemCount() > 0) {
if (newName != myRecyclerViewAdapter.getItemName(position).toString()) {
myRecyclerViewAdapter.add(1, newName, newQty, newPCode, newPlant);
} else {
Toast.makeText(getApplicationContext(), "Product Already in the List", Toast.LENGTH_SHORT).show();
}
} else {
myRecyclerViewAdapter.add(0, newName, newQty, newPCode, newPlant);
}
} else {
Toast.makeText(getApplicationContext(), "Invalied Item!", Toast.LENGTH_SHORT).show();
}
acTextView.setText("");
}
});
}
如果我在此行中将位置更改为 1 if(newName!= myRecyclerViewAdapter.getItemName(position).toString())
LogCat给我这个错误
04-06 16:08:01.681 25361-25361/com.ceatkelanisrilanka.dushanmadushanka.ceat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ceatkelanisrilanka.dushanmadushanka.ceat, PID: 25361
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.SelectItemAdapter.getItemName(SelectItemAdapter.java:54)
at com.ceatkelanisrilanka.dushanmadushanka.ceat.SelectItem$2.onItemClick(SelectItem.java:166)
at android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:905)
at android.widget.AutoCompleteTextView.access$500(AutoCompleteTextView.java:90)
at android.widget.AutoCompleteTextView$DropDownItemClickListener.onItemClick(AutoCompleteTextView.java:1201)
at android.widget.AdapterView.performItemClick(AdapterView.java:305)
at android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:3053)
at android.widget.AbsListView$3.run(AbsListView.java:3860)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
根据评论,如果您想检查RecyclerView
中是否存在该项目,请尝试这样做:
if (myRecyclerViewAdapter.getItemCount() > 0) {
if (!isPresent(newName, myRecyclerViewAdapter)) {
myRecyclerViewAdapter.add(1, newName, newQty, newPCode, newPlant);
} else {
Toast.makeText(getApplicationContext(), "Product Already in the List", Toast.LENGTH_SHORT).show();
}
}
isPresent
可以像这样定义:
private boolean isPresent(String name, MyRecyclerViewAdapter myRecyclerViewAdapter){
boolean isPresent = false;
for(int i = 0; i < myRecyclerViewAdapter.getItemCount(); i++){
if(name.equalsIgnoreCase(myRecyclerViewAdapter.getItemName(i).toString())){
isPresent = true;
break;
}
}
return isPresent;
}