从AsyncTask执行onPostExecute后无法更新Intent的RecyclerView

时间:2019-01-10 17:24:12

标签: android android-recyclerview android-asynctask

发出HTTP请求后,我尝试更新RecyclerView。使用下面要点中的代码,我可以打开android活动,呈现空白的RecyclerView,并从HTTP请求中获取数据。我无法使用HTTP请求中的数据更新RecyclerView。

下面是有关数据的两个类的链接

ItemSelection.java

ItemSelectionAdapter.java

当前,即使我有数据,在收到HTTP响应后,RecyclerView中也没有任何显示。 HTTP响应后如何填充RecyclerView?

2 个答案:

答案 0 :(得分:2)

我认为您在适配器视图中缺少列表项计数,请在ItemSelectionAdapter中更新以下方法

 @Override
public int getItemCount() {
    return data.size();
}

可能会帮助您解决问题,另一件事是您正在将适配器设置为在后执行方法中回收视图,因此在设置适配器后无需调用方法

 adapter.notifyDataSetChanged();

在将新适配器设置为回收视图之后,不需要调用上述行

答案 1 :(得分:1)

您基本上做错了,因为您是在onPostExecute中创建Recyclerview的,这是一种不好的做法,而不是像这样

像这样呼叫AsyncTask

 public class HomeFragment extends Fragment {

    // RecyclerView
    RecyclerView recyclerView;
    RecyclerView.Adapter rvAdapter;
    RecyclerView.LayoutManager rvLayoutManger;
    List<NewsData> news_list;

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        news_list = new ArrayList<NewsData>();
    }


    @Nullable
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.activity_home, null);

        // RecyclerView Setup
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view1);
        rvLayoutManger = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(rvLayoutManger);
        rvAdapter = new HomeRecyclerAdapter(getActivity(), news_list);
        recyclerView.setAdapter(rvAdapter);


        load_data_from_server(0);

        return view;
    }

    private void load_data_from_server(int id) {
        AsyncTask<Integer, Void, Void> task = new AsyncTask<Integer, Void, Void>() {
            @Override
            protected Void doInBackground(Integer... integers) {
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder().url("http://here is my url ").build();
                try {
                    Response response = client.newCall(request).execute();

                    JSONArray array = new JSONArray(response.body().toString());

                    for (int i = 0; i < array.length(); i++) {
                        JSONObject object = array.getJSONObject(i);

                        NewsData data = new NewsData(object.getInt("id"), object.getString("title"), object.getString("describtion"));
                        news_list.add(data);

                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (JSONException e) {
                    System.out.print("End of  content");
                }
                return null;
            }

            @Override
            protected void onPostExecute(Void aVoid) {
                rvAdapter.notifyDataSetChanged();
            }
        };

        task.execute(id);
    }

在对象类中传递数据

public class NewsData {

private int id;
private String title, describtion;

public NewsData(int id, String title, String describtion){
    this.id = id;
    this.title = title;
    this.describtion = describtion;
}


public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getTitle(){
    return title;
}

public void  setTitle(String title){
    this.title = title;
}

public String getDescribtion(){
    return describtion;
}

public void setDescribtion(String describtion) {
    this.describtion = describtion;
}

请参阅适配器将从上面获取数据,其中arraylist由数据组成定义适配器设置

rvAdapter = new HomeRecyclerAdapter(getActivity(), news_list);