如何在适配器类之外更新RecyclerView适配器

huangapple go评论78阅读模式
英文:

How to update the RecyclerView Adapter outside the Adapter Class

问题

我正在制作一个安卓应用,它允许用户在编辑框中输入关键词,当他们点击提交按钮时,下方的可滚动列表(RecyclerView)将显示来自 API 请求的结果。

我在我的 RecyclerView 适配器类中有一个 updateList() 方法:

list = savedInfo.getResult(); // 从单例类中获取更新后的列表
notifyDataSetChanged(); // 更新 RecyclerView

在成功进行 API 请求后,我调用此方法。然而,它目前不起作用,RecyclerView 没有更新。

mSearchBox 是一个编辑框,允许用户输入关键词,下面是 onEditorAction 的代码,它将进行 API 调用,如果调用成功,将调用 UpdateList(),然后适配器将获取更新后的列表并调用 notifyDataSetChanged():

mSearchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
        if (i == EditorInfo.IME_ACTION_DONE) {
            HttpRequest httpRequest = new HttpRequest();
            mHomeText.setText(textView.getText());
            try {
                if (httpRequest.makeCall(textView.getText().toString())) {
                    adapter.updateList();
                } else {
                    // 显示错误消息
                }
            } catch (IOException e) {
                e.printStackTrace();
                return false;
            }
            return true;
        }
        return false;
    }
});

另外,以下是设置适配器的步骤:

final ResultListAdapter adapter = new ResultListAdapter();
mResult.setAdapter(adapter);
mResult.setLayoutManager(new LinearLayoutManager(getContext()));

调试步骤:我尝试设置断点并发现 API 请求和我的单例类都在正常工作,问题仅出现在 RecyclerView 部分。

非常感谢!

英文:

I am making an android app and it allows user to enter the keywords in a editText and when they hit submit the recyclerview down below will show the result from an API request.

I have a updateList() method inside my recyclerView adapter class

list = savedInfo.getResult(); // get updated list from a singleton class
notifyDataSetChanged(); // update the recyclerView

I call this method after making the API Request successfully. However, it is now working, the recyclerView is not updated.

mSearchBox is the editText allows users to enter keywords and here is the onEditorAction, it will make an API call and if it called successfully, it will call UpdateList(), then the adapter will get the updated list and do notifyDataSetChanged()

        mSearchBox.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
            if (i == EditorInfo.IME_ACTION_DONE) {
                HttpRequest httpRequest = new HttpRequest();
                mHomeText.setText(textView.getText());
                try {
                    if (httpRequest.makeCall(textView.getText().toString())){
                        adapter.updateList();
                    }
                    else {
                        // showing error message
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                    return false;
                }
                return true;
            }
            return false;
        }
    });

Also, here is the steps to set my adapter

    final ResultListAdapteradapter = new ResultListAdapter();
    mResult.setAdapter(adapter);
    mResult.setLayoutManager(new LinearLayoutManager(getContext()));

Debugging Steps: I tried to set break points and found out the API Request and my Singleton class are both working, the issue is just the RecyclerView.

Thank you so much!

答案1

得分: 2

当你执行以下代码:

list = savedInfo.getResult();
notifyDataSetChanged();

每次都会创建一个新的list实例,旧的list没有被任何地方引用。所以,你应该改成以下方式:

list.clear()
list.addAll(savedInfo.getResult());
notifyDataSetChanged();

不要忘记在之前初始化list

英文:

When you do

list = savedInfo.getResult();
notifyDataSetChanged();

It is every time creating new list instance and the old one is not referenced anywhere. So instead of assigning to list you should do

list.clear()
list.addAll(savedInfo.getResult());
notifyDataSetChanged();

Don't forget to initialize list if not done before.

huangapple
  • 本文由 发表于 2020年8月28日 11:55:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63627231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定