From 4bb63c0fd24b782ca982247d673033730def6f84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=82=8D=E3=82=80=E3=81=AD=E3=81=93?= Date: Thu, 11 Jan 2024 14:49:32 +0900 Subject: [PATCH] =?UTF-8?q?RecyclerAdapter=E3=81=AE=E3=83=AA=E3=83=95?= =?UTF-8?q?=E3=82=A1=E3=82=AF=E3=82=BF=E3=83=AA=E3=83=B3=E3=82=B0,=20?= =?UTF-8?q?=E3=83=AC=E3=82=A4=E3=82=A2=E3=82=A6=E3=83=88=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../example/childguard/RecyclerAdapter.java | 88 +++++++------------ app/src/main/res/layout/recycler_row.xml | 2 +- 2 files changed, 31 insertions(+), 59 deletions(-) diff --git a/app/src/main/java/com/example/childguard/RecyclerAdapter.java b/app/src/main/java/com/example/childguard/RecyclerAdapter.java index 2e3b719..b48c8fc 100644 --- a/app/src/main/java/com/example/childguard/RecyclerAdapter.java +++ b/app/src/main/java/com/example/childguard/RecyclerAdapter.java @@ -12,77 +12,49 @@ import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; -public class RecyclerAdapter extends RecyclerView.Adapter { +public class RecyclerAdapter extends RecyclerView.Adapter { - ArrayList arrayListDN=new ArrayList<>(); - ArrayList arrayListDA=new ArrayList<>(); - //RecyclerAdapterのコンストラクタ - public RecyclerAdapter(ArrayList arrayList) { - for (String[] deviceInfo:arrayList) { - this.arrayListDN.add(deviceInfo[0]); - Log.d("c",deviceInfo[0]); - } - for (String[] deviceInfo:arrayList) { - this.arrayListDA.add(deviceInfo[1]); - Log.d("c",deviceInfo[1]); - } + ArrayList deviceList; + + // Constructor + public RecyclerAdapter(ArrayList deviceList) { + Log.d("RecyclerAdapter", "Constructor"); + // Init + this.deviceList = deviceList; } - //新しいViewHolderを生成すると呼び出される @NonNull @Override - public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - - - - //recycler_row.xmlをactivity_main.xmlの部品にする - View view = LayoutInflater.from(parent.getContext()) - .inflate(R.layout.recycler_row, parent, false); - - //新しいViewHolderを作成 - //ItemViewHolderクラスを呼び出す - ItemViewHolder holder = new ItemViewHolder(view); - - //クリックイベントを登録 - holder.itemView.setOnClickListener(v -> { - - int position = holder.getAdapterPosition(); - Toast.makeText(v.getContext(),arrayListDA.get(position),Toast.LENGTH_SHORT).show(); - - - }); - - //生成したViewHolderを戻す - return holder; + public RecyclerAdapter.ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { + Log.d("RecyclerAdapter", "onCreateViewHolder"); + LayoutInflater inflater = LayoutInflater.from(parent.getContext()); + View itemView = inflater.inflate(R.layout.recycler_row, parent, false); + return new ItemViewHolder(itemView); } - //1行分のレイアウトの詳細設定 @Override public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) { - //指定された位置の値を取得 - holder.getTextView().setText(arrayListDN.get(position)); + Log.d("RecyclerAdapter", "onBindViewHolder"); + holder.textView.setText(deviceList.get(position)[0]); + holder.textView.setOnClickListener( v -> { + Toast.makeText(v.getContext(), deviceList.get(position)[1], Toast.LENGTH_SHORT).show(); + }); } - //ArrayListのデータ件数を取得 @Override public int getItemCount() { - return arrayListDN.size(); + Log.d("RecyclerAdapter", "getItemCount"); + return deviceList.size(); } + + static class ItemViewHolder extends RecyclerView.ViewHolder { + TextView textView; + + public ItemViewHolder(@NonNull View itemView) { + super(itemView); + textView = itemView.findViewById(R.id.textView1); + } + } + } -//RecyclerView.ViewHolderクラスを継承 -class ItemViewHolder extends RecyclerView.ViewHolder { - private final TextView textView; - - //ItemViewHolderのコンストラクタ - public ItemViewHolder(View view) { - super(view); - //ViewHolderのビューにテキストを定義する - textView = view.findViewById(R.id.textView1); - } - - //テキストの値を取得 - public TextView getTextView() { - return textView; - } -} diff --git a/app/src/main/res/layout/recycler_row.xml b/app/src/main/res/layout/recycler_row.xml index 61aa4a9..b1e2ab3 100644 --- a/app/src/main/res/layout/recycler_row.xml +++ b/app/src/main/res/layout/recycler_row.xml @@ -1,7 +1,7 @@ + android:layout_height="wrap_content">