0

我尝试了几种不同的方法来解决这个问题,但它似乎并不想工作。我附上了所涉及的片段。当我使用搜索功能时,recyclerview 工作;但是,当我第一次加载页面时,我收到错误消息

E/RecyclerView: No adapter attached; skipping layout

我认为这可能是 onCreate 与 onCreateView 的问题,但我不确定该放在哪里。

新闻片段.java



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        v = inflater.inflate(R.layout.fragment_news, container, false);
        swipeRefreshLayout = v.findViewById(R.id.swipeRefresh);
        recyclerView = v.findViewById(R.id.news_recyclerView);


        etQuery = v.findViewById(R.id.etQuery);
        btnSearch = v.findViewById(R.id.btnSearch);

        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        final String country = getCountry();

        swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                retrieveJson("", country, API_KEY);
            }
        });
        retrieveJson("", country, API_KEY);


        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!etQuery.getText().toString().equals("")) {
                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            retrieveJson(etQuery.getText().toString(), country, API_KEY);
                        }
                    });
                    retrieveJson(etQuery.getText().toString(), country, API_KEY);
                } else {
                    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                        @Override
                        public void onRefresh() {
                            retrieveJson("", country, API_KEY);
                        }
                    });
                    retrieveJson("", country, API_KEY);
                }
            }
        });
        return v;

    }

    public void retrieveJson(String query ,String country, String apiKey){

        swipeRefreshLayout.setRefreshing(true);
        Call<Headlines> call;
        if (!etQuery.getText().toString().equals("")){
            call=NewsApiClient.getInstance().getApi().getSpecificData(query,apiKey);

        }else{
            call=NewsApiClient.getInstance().getApi().getHeadlines(country,apiKey);
        }

        call.enqueue(new Callback<Headlines>() {
            @Override
            public void onResponse(Call<Headlines> call, Response<Headlines> response) {
                if (response.isSuccessful() && response.body().getArticles() != null ){
                    swipeRefreshLayout.setRefreshing(false);
                    articles.clear();
                    articles = response.body().getArticles();
                    newsAdapter = new NewsAdapter(getContext(), articles);
                    recyclerView.setAdapter(newsAdapter);
                }
            }

            @Override
            public void onFailure(Call<Headlines> call, Throwable t) {
                swipeRefreshLayout.setRefreshing(false);
                Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
            }
        });

    }

    public String getCountry(){
        Locale locale = Locale.getDefault();
        String country = locale.getCountry();
        return country.toLowerCase();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    }


}
4

1 回答 1

0

在 onCreateView 上,您实际上没有设置任何适配器。相反,当您在 call.enqueue 回调中检索 json 数据时设置它,此回调被异步调用,因此第一次调用 onCreateView 时没有将适配器设置为 RecyclerView。为避免这种情况,请在 onCreateView 函数的开头设置一个带有空数据的适配器,如下所示(使 newsAdapter 成为当前片段的变量)

recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
newsAdapter = new NewsAdapter(getContext(), articles);
recyclerView.setAdapter(newsAdapter);

使文章成为当前片段的变量,并在您的 retriveJson 函数的回调中设置和适配器,只需使用数据更新文章数组并像这样调用 RecycleView 的notifydatasetchanged函数

public void onResponse(Call<Headlines> call, Response<Headlines> response) {
   if (response.isSuccessful() && response.body().getArticles() != null ){
      swipeRefreshLayout.setRefreshing(false);
      articles.clear();
      articles = response.body().getArticles();
      newsAdapter.setData(articles);
      newsAdapter.notifyDataSetChanged()
   }
}

setData 是您必须在 NewsAdapter 类中创建的函数。希望它有所帮助!并查看有关如何使用 RecycleViews的简短指南

于 2020-02-20T18:26:42.840 回答