检查从RecyclerView中已经存在的AutoCompleteTextView中选择的项目 [英] Check the Item selected from the AutoCompleteTextView already exist in the 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){
产品结果= 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(ge tApplicationContext(),产品已经在列表中,Toast.LENGTH_SHORT).show();
}
} else {
myRecyclerViewAdapter.add(0,newName,newQty,newPCode,newPlant);
}
} else {
Toast.makeText(getApplicationContext(),Invalied Item!,Toast.LENGTH_SHORT).show();
}
acTextView.setText();
}
});
}
如果我更改了 / strong>到 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
进程:com.ceatkelanisrilanka.dushanmadushanka.ceat,PID:25361
java.lang.IndexOutOfBoundsException:无效索引1,大小为1
在java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
在java.util.ArrayList.get(ArrayList.java:308)
在com.ceatkelanisrilanka.dushanmadushanka.ceat.adapters.SelectItemAdapter.getItemName(SelectItemAdapter.java:54)
在com.ceatkelanisrilanka.dushanmadushanka .ceat.SelectItem $ 2.onItemClick(SelectItem.java:166)
在android.widget.AutoCompleteTextView.performCompletion(AutoCompleteTextView.java:905)
在android.widget.AutoCompleteTextView.access $ 500(AutoCompleteTextView.java :90)
在android.widget.AutoCompleteTextView $ Dro pDownItemClickListener.onItemClick(AutoCompleteTextView.java:1201)
在android.widget.AdapterView.performItemClick(AdapterView.java:305)
在android.widget.AbsListView.performItemClick(AbsListView.java:1146)
at android.widget.AbsListView $ PerformClick.run(AbsListView.java:3053)
在android.widget.AbsListView $ 3.run(AbsListView.java:3860)
在android.os.Handler。 handleCallback(Handler.java:739)
在android.os.Handler.dispatchMessage(Handler.java:95)
在android.os.Looper.loop(Looper.java:135)
在android.app.ActivityThread.main(ActivityThread.java :5254)
在java.lang.reflect.Method.invoke(本机方法)
在java.lang.reflect.Method.invoke(Method.java:372)
在com.android .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)
在com.android.internal.os.Zyg oteInit.main(ZygoteInit.java:698)
在评论中,如果要检查项目是否存在于 RecyclerView
中,那么尝试这样做:
if(myRecyclerViewAdapter.getItemCount()> 0){
if(!isPresent(newName,myRecyclerViewAdapter)){
myRecyclerViewAdapter.add(1,newName,newQty,newPCode,newPlant);
} else {
Toast.makeText(getApplicationContext(),产品已经在列表中,Toast.LENGTH_SHORT).show();
}
}
和 isPresent
可以这样定义:
private boolean isPresent(String name,MyRecyclerViewAdapter myRecyclerViewAdapter){
boolean isPresent = false; (int i = 0; i< myRecyclerViewAdapter.getItemCount(); i ++){
if(name.equalsIgnoreCase(myRecyclerViewAdapter.getItemName(i).toString())){
isPresent = true;
break;
}
}
return isPresent;
}
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:
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:
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;
}
这篇关于检查从RecyclerView中已经存在的AutoCompleteTextView中选择的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!