RecyclerView的适配器始终返回null

时间:2019-06-15 05:39:07

标签: java android json

有人可以告诉我为什么我的适配器始终保持为空。 我正在调用API以从服务器获取附件,并从API获得成功后,我正在填充回收器视图。 有人可以告诉我为什么我的适配器始终保持为空。 我正在调用API以从服务器获取附件,并从API获得成功后,我将填充回收器视图。

private void loadFiles() {
    GetDataService getDataService = RetrofitClientInstance.getRetrofitInstance(Constants.BASE_URL)
            .create(GetDataService.class);
    Call<Object> call = getDataService.getAttachments("mUsername", "mPassword",
            "com.attachmants.office", "Mobile",
            "8888888888");
    call.enqueue(new Callback<Object>() {
        @Override
        public void onResponse(Call<Object> call, Response<Object> response) {
            ArrayList<Files> fileList = new ArrayList<>();
            if (response != null) {
                Gson gson = new Gson();
                String json = gson.toJson(response.body());
                try {
                    JSONObject objParent = new JSONObject(json);
                    JSONArray jsonArray = objParent.getJSONArray("Files");
                    if (jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            fileList.add(parser.convertJSONtoFiles(jsonObject1));

                        }
                    } else {
                        Toast.makeText(MainActivity.this,"No files are available!",Toast.LENGTH_LONG).show();
                    }
                    filesAdapter = new FilesAdapter(fileList);
                    recyclerView.setAdapter(filesAdapter);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }

        @Override
        public void onFailure(Call<Object> call, Throwable t) {
            Toast.makeText(MainActivity.this, t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });
}

适配器类:

public class FilesAdapter extends RecyclerView.Adapter<FilesAdapter.ViewHolder>{
    private ArrayList<Files> files;
    private Context context;

    public FilesAdapter(ArrayList<Files> files) {
        this.files = files;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, parent, false);
        context = view.getContext();
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String textDocType, textDocDisplayName, textDocOriginalName, textDocId, textStudentId, textUserId;

        textDocType = files.get(position).getDocumentType();
        textDocDisplayName = files.get(position).getDocumentDisplayName();
        textDocOriginalName = files.get(position).getDocumentOrigianlName();
        textDocId = files.get(position).getDocumentsID();
        textStudentId = files.get(position).getSTDID();
        textUserId = files.get(position).getUSERID();

        holder.setData(textDocType, textDocDisplayName, textDocOriginalName, textDocId, textStudentId, textUserId);
    }

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

    public class ViewHolder extends RecyclerView.ViewHolder {
        // Widgets
        TextView textDocType, textDocDisplayName, textDocOriginalName, textDocId, textStudentId, textUserId;
        // Vars
        private View view;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            textDocType = view.findViewById(R.id.textDocType);
            textDocDisplayName = view.findViewById(R.id.textDocDisplayName);
            textDocOriginalName = view.findViewById(R.id.textDocOriginalName);
            textDocId = view.findViewById(R.id.textDocId);
            textStudentId = view.findViewById(R.id.textStdId);
            textUserId = view.findViewById(R.id.textUserId);
        }

        private void setData(String docType, String name, String origName, String docId,
                             String studentId, String userId) {
            textDocType.setText(docType);
            textDocDisplayName.setText(name);
            textDocOriginalName.setText(origName);
            textDocId.setText(docId);
            textStudentId.setText(studentId);
            textUserId.setText(userId);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

在类顶部定义fileList,而不是onResponse方法。

ArrayList<Files> files = new ArrayList<>();

在onCreate中初始化并设置适配器

filesAdapter = new FilesAdapter(files);
recyclerView.setAdapter(filesAdapter);

现在onResponse方法中的操作如下:

 call.enqueue(new Callback<Object>() {
        @Override
        public void onResponse(Call<Object> call, Response<Object> response) {
            ArrayList<Files> fileList = new ArrayList<>();
            if (response != null) {
                Gson gson = new Gson();
                String json = gson.toJson(response.body());
                try {
                    JSONObject objParent = new JSONObject(json);
                    JSONArray jsonArray = objParent.getJSONArray("Files");
                    if (jsonArray.length() > 0) {
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                            fileList.add(parser.convertJSONtoFiles(jsonObject1));
                        }
                    } else {
                        Toast.makeText(MainActivity.this,"No files are available!",Toast.LENGTH_LONG).show();
                    }
                    files.clear();
                    files.addAll(fileList)
                    filesAdapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        }