[英]Check the Item selected from the AutoCompleteTextView already exist in the RecyclerView
I have AutoCmpleteTextView and RecyclerView in my app. 我的应用程序中有AutoCmpleteTextView和RecyclerView。 When I searched and selected an item from the AutCompleteTextView it will added to the RecyclerView.
当我从AutCompleteTextView中搜索并选择一个项目时,它将添加到RecyclerView中。 It worked very well.
效果很好。 But I want to know whether the selected item is already exist in the RecyclerView.
但我想知道RecyclerView中是否已存在所选项目。 Avoid item duplication.
避免重复项目。
This is my code. 这是我的代码。
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("");
}
});
}
If I changed position to 1 in this line if (newName != myRecyclerViewAdapter.getItemName(position).toString()) 如果我改变了位置 ,以1这一行,如果(了newName!= myRecyclerViewAdapter.getItemName(位置)的ToString())
LogCat give me this error 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)
Depending on the comments, if you want to check if the item if present in RecyclerView
then try to do like this: 根据注释,如果要检查
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();
}
}
And isPresent
can be defined like this: 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.