java - 无法将数据绑定(bind)到recyclerView中

标签 java android android-recyclerview okhttp

我使用 URL 中的 GET 请求获取值。当我尝试将值绑定(bind)到 RecyclerView 时,我得到一个空值。从 getValue() 方法中,它返回 null '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;
    }

最佳答案

client.newCall(request).enqueue(new Callback()

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

参见this

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

关于java - 无法将数据绑定(bind)到recyclerView中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59254211/

相关文章:

java - 在 JPA 中,实体是否可以访问其 Embedded 对象的 Embedded?

java - Apache MINA SFTP 示例

android - 如何将 Activity 从下拉菜单中的通知放在前面?

java - Android 回收器 View 未更新

java - 通过openshift中的javabridge端口连接php和java

java - 如何附加到 Java 异常?

python - Github Action 使用现有的 Python 脚本并获取字符串输出

android - 如何在 Android 的本地代码中构建 Mapbox GL Native

android - 从 firebase 获取最新的帖子/数据,在 android 中限制 10 条记录并填充到 Listview 中

android - Recycler View 在滚动时随机播放图像