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"> <project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" /> <component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="jbr-17" project-jdk-type="JavaSDK"> <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 android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment; import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater; import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.ListView; import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -60,29 +64,72 @@ 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 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 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
Bundle savedInstanceState) {
// Inflate the layout for this fragment // Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_library_top, container, false); View view = inflater.inflate(R.layout.fragment_library_top, container, false);
// Use view.findViewById instead of findViewById // Use view.findViewById instead of findViewById
ListView documentListView = view.findViewById(R.id.document_list); RecyclerView documentRecyclerView = view.findViewById(R.id.document_list);
List<String> documentList = new ArrayList<>();
documentList.add("Document A");
documentList.add("Document B");
documentList.add("Document C");
// レイアウトリソースを指定する: simple_list_item_1 if (documentRecyclerView != null) {
ArrayAdapter<String> adapter = new ArrayAdapter<>( List<String> documentList = new ArrayList<>();
getActivity(), // 修正: requireActivity() ではなく getActivity() を使用 documentList.add("Document A");
android.R.layout.simple_list_item_1, documentList.add("Document B");
documentList); documentList.add("Document C");
documentListView.setAdapter(adapter); // Create and set the adapter
DocumentAdapter adapter = new DocumentAdapter(documentList);
documentRecyclerView.setAdapter(adapter);
return view; // Use a LinearLayoutManager to specify the layout
} LinearLayoutManager layoutManager = new LinearLayoutManager(requireContext());
documentRecyclerView.setLayoutManager(layoutManager);
}
return view;
}
} }