【问题标题】:Could not Bind the data into the recyclerView无法将数据绑定到 recyclerView
【发布时间】:2019-12-10 04:31:54
【问题描述】:

我正在使用来自 URL 的 GET 请求获取值。当我尝试将值绑定到RecyclerView 时,我得到一个空值。从getValue() 方法,它返回空的“si”值。为什么?

public class ShipmentLists extends AppCompatActivity {

    List < ShipmentInfo > si;
    RecyclerView recList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.recycler_lists);
        si = new ArrayList < > ();
        recList = (RecyclerView) findViewById(R.id.cardList);
        LinearLayoutManager lim = new LinearLayoutManager(ShipmentLists.this);
        lim.setOrientation(LinearLayoutManager.VERTICAL);
        recList.setHasFixedSize(true);
        recList.setLayoutManager(lim);
        ShipmentAdapter sa = new ShipmentAdapter(ShipmentLists.this, getData());
        sa.notifyDataSetChanged();
        recList.setAdapter(sa);
    }

    private List < ShipmentInfo > getData() {
        String url = "URL HERE";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().url(url).header("Accept", "application/json")
            .header("Content-Type", "application/json").build();
        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String mMessage = e.getMessage().toString();
                Log.w("failure Response", mMessage);
                call.cancel();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String mMessage = response.body().string();

                try {
                    final JSONArray myResponse = new JSONArray(mMessage);
                    for (int i = 0; i < myResponse.length(); i++) {

                        String receiver = myResponse.getJSONObject(i).getString("receivername");
                        String address = myResponse.getJSONObject(i).getString("AddressLine1");
                        String city = myResponse.getJSONObject(i).getString("city");
                        String phone = myResponse.getJSONObject(i).getString("contactnumber");
                        String shipmentDate = myResponse.getJSONObject(i).getString("date");
                        String status = myResponse.getJSONObject(i).getString("status");
                        si.add(new ShipmentInfo(receiver, address, city, shipmentDate, phone, status));
                        Log.e("SI DATA", "" + si.size());
                    }


                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Log.e("RESULTS ARE", "" + mMessage);
            }
        });
        Log.e("LIST SIZE", "" + si.size());
        return si;
    }

【问题讨论】:

  • 这是您的 getData() 方法的错误。当new ShipmentAdapter(ShipmentLists.this, getData()) 被调用时,si(即List &lt;ShipmentInfo&gt;)为空。
  • 是的,我知道。请告诉我如何解决它并获取返回值。
  • 最好的做法是使用LiveData 真的是最好的。
  • 只在适配器构造函数中使用列表而不是 getData。并使用相同的列表更新成功,然后调用 notifyDataSetChanged()
  • 我建议您使用适当的架构,例如 MVVM 或类似的东西。

标签: java android android-recyclerview okhttp


【解决方案1】:
client.newCall(request).enqueue(new Callback()

asynchronous call。这意味着,它将在单独的线程中同时执行。所以 si 在添加响应之前会返回 null

this

这个question and answers 给出了一些关于异步的想法

【讨论】:

  • 好的。我知道了。现在会尝试。谢谢!
猜你喜欢
  • 2016-01-17
  • 2020-09-21
  • 2012-06-08
  • 2017-04-23
  • 1970-01-01
  • 2021-04-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-15
相关资源
最近更新 更多