コメントと例外が起こった際の処理を入れた

This commit is contained in:
nero 2023-12-21 16:16:07 +09:00
parent 017748b4a6
commit c35c4e829e
2 changed files with 10 additions and 6 deletions

View File

@ -16,7 +16,12 @@ public class DocumentAdapter extends RecyclerView.Adapter<DocumentAdapter.Docume
private List<DocumentMeta> documentMetas; private List<DocumentMeta> documentMetas;
public DocumentAdapter(List<DocumentMeta> documentMetas) { public DocumentAdapter(List<DocumentMeta> documentMetas) {
// nullの場合に例外を発生させる
if (documentMetas == null) {
throw new IllegalArgumentException("DocumentMetas list cannot be null");
}
this.documentMetas = documentMetas; this.documentMetas = documentMetas;
} }
@ -31,6 +36,11 @@ public class DocumentAdapter extends RecyclerView.Adapter<DocumentAdapter.Docume
public void onBindViewHolder(@NonNull DocumentViewHolder holder, int position) { public void onBindViewHolder(@NonNull DocumentViewHolder holder, int position) {
// DocumentMeta から適切な情報を取得してセット // DocumentMeta から適切な情報を取得してセット
holder.title.setText(documentMetas.get(position).getTitle()); holder.title.setText(documentMetas.get(position).getTitle());
DocumentMeta documentMeta = documentMetas.get(position);
if (documentMeta != null) {
holder.title.setText(documentMeta.getTitle());
}
} }
@Override @Override

View File

@ -95,12 +95,6 @@ public class LibraryTopFragment extends Fragment {
Toast.makeText(getContext(), "Documents: " + Integer.toString(metas.size()), Toast.LENGTH_LONG).show(); Toast.makeText(getContext(), "Documents: " + Integer.toString(metas.size()), Toast.LENGTH_LONG).show();
//仮データを取得
// List<String> documentList = new ArrayList<>();
// documentList.add("Document A");
// documentList.add("Document B");
// documentList.add("Document C");
// Create and set the adapter // Create and set the adapter
DocumentAdapter adapter = new DocumentAdapter(metas); DocumentAdapter adapter = new DocumentAdapter(metas);
documentRecyclerView.setAdapter(adapter); documentRecyclerView.setAdapter(adapter);