【问题标题】:Takes 2 clicks of Enter to get a response into the recyclerview需要 2 次单击 Enter 才能获得对 recyclerview 的响应
【发布时间】:2020-04-25 20:10:57
【问题描述】:

我正在为我的一个课程制作一个应用程序,这是一个我们必须为自己选择的项目。现在它大部分都在工作,除了这里和那里的一些事情。其中之一是需要点击 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);
        }
    }

如果您需要任何其他代码来帮助我解决此问题,请告诉我,我认为一切都应该在这里。

【问题讨论】:

    标签: java android-studio arraylist android-recyclerview


    【解决方案1】:

    您没有正确比较字符串

    你不应该使用==你必须使用.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
    }
    .........
    .........
    

    【讨论】:

    • 这仍然给出相同的结果。更改后我还需要按两次回车键
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-19
    • 2019-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多