I have AutoCmpleteTextView and RecyclerView in my app. When I searched and selected an item from the AutCompleteTextView it will added to the RecyclerView. It worked very well. But I want to know whether the selected item is already exist in the 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())
LogCat give me this error
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:And
isPresent
can be defined like this: