我正在为我的一个类(class)制作一个应用程序,这是一个我们必须为自己选择的项目。目前,除了一些零星的事情外,它大部分都在工作。其中之一是,需要点击 2 或 3 次 Enter 键才能让应用程序使用更新的信息重做 recyclerview。
public View.OnKeyListener search = new View.OnKeyListener()
{
@Override
public boolean onKey(View v, int keyCode, KeyEvent event)
{
searchItem = "";//clears it out before the use of it
if((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER))//only executes when the ENTER key is hit
{
if(searchItem != searchBar.getText().toString())//as long as the bar isn't empty
{
searchItem = searchBar.getText().toString();//grabs the search term and puts it in the variable
}
// hide the virtual keyboard
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(v.getWindowToken(), 0);
//clear out the recyclerview
clearRecycler();
//API Call one
goGetSearch(searchItem, searchType);
return true;
}
return false;
}
};
这是我调用的clearRecycler方法。我使用 for 循环删除了项目,因为 .clear 由于某种原因无法正常工作。
public void clearRecycler()
{
int size = details.size();
if (size > 0)
{
for(int i = 0; i < size; i++)
{
details.remove(0);//arraylist
}
for(int i = 0; i < OnlyIds.size(); i++)
{
OnlyIds.remove(0);//arraylist
}
myAdapter.notifyItemRangeRemoved(0, size);
}
}
如果您需要任何其他代码来帮助我解决此问题,请告诉我,我认为一切都应该在这里。
最佳答案
您没有正确比较字符串
您不应该使用==
,而必须使用.equals()
:
.........
if(!searchBar.getText().toString().equals(searchItem))//as long as the bar isn't empty
{
searchItem = searchBar.getText().toString();//grabs the search term and puts it in the variable
}
.........
.........
关于java - 单击 2 次 Enter 即可在回收器 View 中获得响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61431962/