RecyclerAdapterのリファクタリング, レイアウト修正
This commit is contained in:
		
							parent
							
								
									b9546b4826
								
							
						
					
					
						commit
						4bb63c0fd2
					
				@ -12,77 +12,49 @@ import androidx.recyclerview.widget.RecyclerView;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
public class RecyclerAdapter extends RecyclerView.Adapter<ItemViewHolder> {
 | 
					public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemViewHolder> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    ArrayList<String> arrayListDN=new ArrayList<>();
 | 
					    ArrayList<String[]> deviceList;
 | 
				
			||||||
    ArrayList<String> arrayListDA=new ArrayList<>();
 | 
					
 | 
				
			||||||
    //RecyclerAdapterのコンストラクタ
 | 
					    // Constructor
 | 
				
			||||||
    public RecyclerAdapter(ArrayList<String[]> arrayList) {
 | 
					    public RecyclerAdapter(ArrayList<String[]> deviceList) {
 | 
				
			||||||
        for (String[] deviceInfo:arrayList) {
 | 
					        Log.d("RecyclerAdapter", "Constructor");
 | 
				
			||||||
            this.arrayListDN.add(deviceInfo[0]);
 | 
					        // Init
 | 
				
			||||||
            Log.d("c",deviceInfo[0]);
 | 
					        this.deviceList = deviceList;
 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
        for (String[] deviceInfo:arrayList) {
 | 
					 | 
				
			||||||
            this.arrayListDA.add(deviceInfo[1]);
 | 
					 | 
				
			||||||
            Log.d("c",deviceInfo[1]);
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //新しいViewHolderを生成すると呼び出される
 | 
					 | 
				
			||||||
    @NonNull
 | 
					    @NonNull
 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
 | 
					    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);
 | 
				
			||||||
        //recycler_row.xmlをactivity_main.xmlの部品にする
 | 
					        return new ItemViewHolder(itemView);
 | 
				
			||||||
        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;
 | 
					 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //1行分のレイアウトの詳細設定
 | 
					 | 
				
			||||||
    @Override
 | 
					    @Override
 | 
				
			||||||
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
 | 
					    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
 | 
				
			||||||
        //指定された位置の値を取得
 | 
					        Log.d("RecyclerAdapter", "onBindViewHolder");
 | 
				
			||||||
        holder.getTextView().setText(arrayListDN.get(position));
 | 
					        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
 | 
					    @Override
 | 
				
			||||||
    public int getItemCount() {
 | 
					    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;
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,7 @@
 | 
				
			|||||||
<?xml version="1.0" encoding="utf-8"?>
 | 
					<?xml version="1.0" encoding="utf-8"?>
 | 
				
			||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 | 
					<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 | 
				
			||||||
    android:layout_width="match_parent"
 | 
					    android:layout_width="match_parent"
 | 
				
			||||||
    android:layout_height="match_parent">
 | 
					    android:layout_height="wrap_content">
 | 
				
			||||||
    <TextView
 | 
					    <TextView
 | 
				
			||||||
        android:id="@+id/textView1"
 | 
					        android:id="@+id/textView1"
 | 
				
			||||||
        android:layout_width="match_parent"
 | 
					        android:layout_width="match_parent"
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user