質問編集履歴

1

RepoAdapterをjavaからkotlinに変更しました

2024/05/17 11:27

投稿

soruto
soruto

スコア1

test CHANGED
File without changes
test CHANGED
@@ -83,70 +83,49 @@
83
83
  ```
84
84
 
85
85
  ```RepoAdapter
86
- package com.example.yourprojectname;
86
+ package com.example.yourprojectname
87
87
 
88
- import android.view.LayoutInflater;
88
+ import android.view.LayoutInflater
89
- import android.view.View;
89
+ import android.view.View
90
- import android.view.ViewGroup;
90
+ import android.view.ViewGroup
91
- import android.widget.TextView;
91
+ import android.widget.TextView
92
+ import androidx.paging.PagingDataAdapter
93
+ import androidx.recyclerview.widget.DiffUtil
94
+ import androidx.recyclerview.widget.RecyclerView
95
+ import com.example.github.R
92
96
 
93
- import androidx.annotation.NonNull;
94
- import androidx.paging.PagingDataAdapter;
97
+ class RepoAdapter(private val repos: List<Repo>) : PagingDataAdapter<Repo, RepoAdapter.RepoViewHolder>(DIFF_CALLBACK) {
95
- import androidx.recyclerview.widget.DiffUtil;
96
- import androidx.recyclerview.widget.RecyclerView;
97
- import com.example.github.R;
98
98
 
99
- import kotlin.Unit;
100
-
101
- public class RepoAdapter extends PagingDataAdapter<Repo, RepoAdapter.RepoViewHolder> {
99
+ override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RepoViewHolder {
102
-
100
+ val view = LayoutInflater.from(parent.context).inflate(R.layout.repo_item, parent, false)
103
- public RepoAdapter() {
101
+ return RepoViewHolder(view)
104
- super(DIFF_CALLBACK);
105
-
106
102
  }
107
103
 
108
-
109
- @NonNull
110
- @Override
111
- public RepoViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
112
- View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.repo_item, parent, false);
113
- return new RepoViewHolder(view);
114
- }
115
-
116
- @Override
117
- public void onBindViewHolder(@NonNull RepoViewHolder holder, int position) {
104
+ override fun onBindViewHolder(holder: RepoViewHolder, position: Int) {
118
- Repo repo = getItem(position);
105
+ val repo = getItem(position)
119
106
  if (repo != null) {
120
- holder.nameTextView.setText(repo.name);
107
+ holder.nameTextView.text = repo.name
121
- holder.descriptionTextView.setText(repo.description);
108
+ holder.descriptionTextView.text = repo.description
122
109
  }
123
110
  }
124
111
 
125
- public static class RepoViewHolder extends RecyclerView.ViewHolder {
112
+ class RepoViewHolder(view: View) : RecyclerView.ViewHolder(view) {
113
+ val nameTextView: TextView = view.findViewById(R.id.repo_name)
114
+ val descriptionTextView: TextView = view.findViewById(R.id.repo_description)
115
+ }
126
116
 
117
+ companion object {
118
+ val DIFF_CALLBACK = object : DiffUtil.ItemCallback<Repo>() {
119
+ override fun areItemsTheSame(oldItem: Repo, newItem: Repo): Boolean {
127
- private final TextView nameTextView;
120
+ return oldItem.id == newItem.id
128
- private final TextView descriptionTextView;
121
+ }
129
122
 
130
- public RepoViewHolder(@NonNull View itemView) {
131
- super(itemView);
132
- nameTextView = itemView.findViewById(R.id.repo_name);
133
- descriptionTextView = itemView.findViewById(R.id.repo_description);
123
+ override fun areContentsTheSame(oldItem: Repo, newItem: Repo): Boolean {
124
+ return oldItem == newItem
125
+ }
134
126
  }
135
127
  }
136
-
137
- private static final DiffUtil.ItemCallback<Repo> DIFF_CALLBACK = new DiffUtil.ItemCallback<Repo>() {
138
- @Override
139
- public boolean areItemsTheSame(@NonNull Repo oldItem, @NonNull Repo newItem) {
140
- return oldItem.id == newItem.id;
141
- }
142
-
143
- @Override
144
- public boolean areContentsTheSame(@NonNull Repo oldItem, @NonNull Repo newItem) {
145
- return oldItem.equals(newItem);
146
- }
147
- };
148
128
  }
149
-
150
129
  ```
151
130
 
152
131
  ### 試したこと・調べたこと