Laibraryが表示できるようにコードを書き換えた。

・nullを検出して安全性を高めた
・RecyclerView.Adapterを使用するためのコードを追加
This commit is contained in:
nero 2023-12-19 10:15:33 +09:00
parent 6d71322982
commit ac2001142f
2 changed files with 63 additions and 17 deletions

View File

@ -1,4 +1,3 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="jbr-17" project-jdk-type="JavaSDK">

View File

@ -2,13 +2,17 @@ package one.nem.lacerta.feature.library;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
@ -60,27 +64,70 @@ public class LibraryTopFragment extends Fragment {
}
}
public class DocumentAdapter extends RecyclerView.Adapter<DocumentAdapter.DocumentViewHolder> {
private List<String> documentList;
public DocumentAdapter(List<String> documentList) {
this.documentList = documentList;
}
@NonNull
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
public DocumentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(android.R.layout.simple_list_item_1, parent, false);
return new DocumentViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DocumentViewHolder holder, int position) {
holder.bind(documentList.get(position));
}
@Override
public int getItemCount() {
return documentList.size();
}
class DocumentViewHolder extends RecyclerView.ViewHolder {
private final TextView textView;
DocumentViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(android.R.id.text1);
}
void bind(String document) {
textView.setText(document);
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_library_top, container, false);
// Use view.findViewById instead of findViewById
ListView documentListView = view.findViewById(R.id.document_list);
List<String> documentList = new ArrayList<>();
RecyclerView documentRecyclerView = view.findViewById(R.id.document_list);
if (documentRecyclerView != null) {
List<String> documentList = new ArrayList<>();
documentList.add("Document A");
documentList.add("Document B");
documentList.add("Document C");
// レイアウトリソースを指定する: simple_list_item_1
ArrayAdapter<String> adapter = new ArrayAdapter<>(
getActivity(), // 修正: requireActivity() ではなく getActivity() を使用
android.R.layout.simple_list_item_1,
documentList);
// Create and set the adapter
DocumentAdapter adapter = new DocumentAdapter(documentList);
documentRecyclerView.setAdapter(adapter);
documentListView.setAdapter(adapter);
// Use a LinearLayoutManager to specify the layout
LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
documentRecyclerView.setLayoutManager(layoutManager);
}
return view;
}