728x90
https://pasongsong.tistory.com/164?category=1090989
Recycler view item에 만들 class를 만들고 다음과 같이 작업 진행
저번에 만들었던 Recycler view Adapter에 추가한 변수를 넣을 것임
recycler view item
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/IV_food"
android:layout_width="0dp"
android:layout_height="420dp"
app:layout_constraintBottom_toTopOf="@+id/button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:srcCompat="@tools:sample/avatars" />
<Button
android:id="@+id/btn_camera"
android:layout_width="0dp"
android:layout_height="50dp"
android:text="카메라"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/IV_food" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout5"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button2">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout"
android:layout_width="0dp"
android:layout_height="64dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="@+id/text_1"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="식사"
app:layout_constraintBottom_toTopOf="@+id/text_day"
app:layout_constraintEnd_toStartOf="@id/text_2"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.511" />
<TextView
android:id="@+id/text_2"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="나트륨(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_na"
app:layout_constraintEnd_toStartOf="@+id/text_3"
app:layout_constraintStart_toEndOf="@+id/text_1"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_3"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="단백질(g)"
app:layout_constraintBottom_toTopOf="@+id/text_dan"
app:layout_constraintEnd_toStartOf="@+id/text_4"
app:layout_constraintStart_toEndOf="@+id/text_2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_4"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="당류(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_dang"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_3"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_day"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_na"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_1" />
<TextView
android:id="@+id/text_na"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_dan"
app:layout_constraintStart_toEndOf="@+id/text_day"
app:layout_constraintTop_toBottomOf="@+id/text_2" />
<TextView
android:id="@+id/text_dan"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_dang"
app:layout_constraintStart_toEndOf="@+id/text_na"
app:layout_constraintTop_toBottomOf="@+id/text_3" />
<TextView
android:id="@+id/text_dang"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_dan"
app:layout_constraintTop_toBottomOf="@+id/text_4" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout2"
android:layout_width="0dp"
android:layout_height="64dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.461"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout">
<TextView
android:id="@+id/text_5"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="마그네슘(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_ma"
app:layout_constraintEnd_toStartOf="@+id/text_6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_6"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="아연(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_zin"
app:layout_constraintEnd_toStartOf="@+id/text_7"
app:layout_constraintStart_toEndOf="@+id/text_5"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_7"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="에너지(kcal)"
app:layout_constraintBottom_toTopOf="@+id/text_ene"
app:layout_constraintEnd_toStartOf="@+id/text_8"
app:layout_constraintStart_toEndOf="@+id/text_6"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_8"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="인(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_p"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_7"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_ma"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_zin"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_5" />
<TextView
android:id="@+id/text_zin"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_ene"
app:layout_constraintStart_toEndOf="@+id/text_ma"
app:layout_constraintTop_toBottomOf="@+id/text_6" />
<TextView
android:id="@+id/text_ene"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_p"
app:layout_constraintStart_toEndOf="@+id/text_zin"
app:layout_constraintTop_toBottomOf="@+id/text_7" />
<TextView
android:id="@+id/text_p"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_ene"
app:layout_constraintTop_toBottomOf="@+id/text_8" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout3"
android:layout_width="0dp"
android:layout_height="64dp"
app:layout_constraintBottom_toTopOf="@+id/constraintLayout4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout2">
<TextView
android:id="@+id/text_9"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="중량(g)"
app:layout_constraintBottom_toTopOf="@+id/text_wei"
app:layout_constraintEnd_toStartOf="@+id/text_10"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_10"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="지방(g)"
app:layout_constraintBottom_toTopOf="@+id/text_fat"
app:layout_constraintEnd_toStartOf="@+id/text_11"
app:layout_constraintStart_toEndOf="@+id/text_9"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_11"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="철(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_fe"
app:layout_constraintEnd_toStartOf="@+id/text_12"
app:layout_constraintStart_toEndOf="@+id/text_10"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_12"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="칼륨(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_k"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_11"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_wei"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_fat"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_9" />
<TextView
android:id="@+id/text_fat"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_fe"
app:layout_constraintStart_toEndOf="@+id/text_wei"
app:layout_constraintTop_toBottomOf="@+id/text_10" />
<TextView
android:id="@+id/text_fe"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_k"
app:layout_constraintStart_toEndOf="@+id/text_fat"
app:layout_constraintTop_toBottomOf="@+id/text_11" />
<TextView
android:id="@+id/text_k"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_fe"
app:layout_constraintTop_toBottomOf="@+id/text_12" />
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/constraintLayout4"
android:layout_width="0dp"
android:layout_height="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/constraintLayout3">
<TextView
android:id="@+id/text_13"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="칼슘(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_ca"
app:layout_constraintEnd_toStartOf="@+id/text_14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_14"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="콜레스테롤(mg)"
app:layout_constraintBottom_toTopOf="@+id/text_chol"
app:layout_constraintEnd_toStartOf="@+id/text_15"
app:layout_constraintStart_toEndOf="@+id/text_13"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_15"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="탄수화물(g)"
app:layout_constraintBottom_toTopOf="@+id/text_tan"
app:layout_constraintEnd_toStartOf="@+id/text_16"
app:layout_constraintStart_toEndOf="@+id/text_14"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_16"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="트랜스지방(g)"
app:layout_constraintBottom_toTopOf="@+id/text_trans"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_15"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_ca"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_chol"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_13" />
<TextView
android:id="@+id/text_chol"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_tan"
app:layout_constraintStart_toEndOf="@+id/text_ca"
app:layout_constraintTop_toBottomOf="@+id/text_14" />
<TextView
android:id="@+id/text_tan"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/text_trans"
app:layout_constraintStart_toEndOf="@+id/text_chol"
app:layout_constraintTop_toBottomOf="@+id/text_15" />
<TextView
android:id="@+id/text_trans"
android:layout_width="0dp"
android:layout_height="0dp"
android:gravity="center"
android:text="empty"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="@+id/text_tan"
app:layout_constraintTop_toBottomOf="@+id/text_16" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Adaper
- int형 자료를 textview에 넣고싶다면 str형 변환이 필요함
public class DayMealAdapter extends RecyclerView.Adapter<DayMealAdapter.ViewHolder> {
private ArrayList<One_food> food_list;
/**
* Provide a reference to the type of views that you are using
* (custom ViewHolder).
*/
@NonNull
@Override
public DayMealAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull DayMealAdapter.ViewHolder holder, int position) {
holder.onBind(food_list.get(position));
}
public void setFoodList(ArrayList<One_food> list){
this.food_list = list;
notifyDataSetChanged();
}
@Override
public int getItemCount() {
return food_list.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
ImageView food_img;
TextView food_day;
TextView food_na;
TextView food_dan;
TextView food_dang;
TextView food_ma;
TextView food_zin;
TextView food_ene;
TextView food_p;
TextView food_wei;
TextView food_fat;
TextView food_fe;
TextView food_k;
TextView food_ca;
TextView food_chol;
TextView food_tan;
TextView food_trans;
public ViewHolder(@NonNull View itemView) {
super(itemView);
food_img = (ImageView) itemView.findViewById(R.id.IV_food);
food_day = (TextView) itemView.findViewById(R.id.text_day);
food_na = (TextView) itemView.findViewById(R.id.text_na);
food_dan = (TextView) itemView.findViewById(R.id.text_dan);
food_dang = (TextView) itemView.findViewById(R.id.text_dang);
food_ma = (TextView) itemView.findViewById(R.id.text_ma);
food_zin = (TextView) itemView.findViewById(R.id.text_zin);
food_ene = (TextView) itemView.findViewById(R.id.text_ene);
food_p = (TextView) itemView.findViewById(R.id.text_p);
food_wei = (TextView) itemView.findViewById(R.id.text_wei);
food_fat = (TextView) itemView.findViewById(R.id.text_fat);
food_fe = (TextView) itemView.findViewById(R.id.text_fe);
food_k = (TextView) itemView.findViewById(R.id.text_k);
food_ca = (TextView) itemView.findViewById(R.id.text_ca);
food_chol = (TextView) itemView.findViewById(R.id.text_chol);
food_tan = (TextView) itemView.findViewById(R.id.text_tan);
food_trans = (TextView) itemView.findViewById(R.id.text_trans);
}
void onBind(One_food item){
food_img.setImageResource(item.getFood_resourceId());
food_day.setText(item.getDay());
food_na.setText(item.getNa());
food_dan.setText(item.getDan());
food_dang.setText(item.getDang());
food_ma.setText(item.getMa());
food_zin.setText(item.getZin());
food_ene.setText(item.getEne());
food_p.setText(item.getP());
food_wei.setText(item.getWei());
food_fat.setText(item.getFat());
food_fe.setText(item.getFe());
food_k.setText(item.getK());
food_ca.setText(item.getCa());
food_chol.setText(item.getChol());
food_tan.setText(item.getTan());
food_trans.setText(item.getTrans());
}
}
}
728x90
'Android' 카테고리의 다른 글
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView에서 Intent 사용하기 (0) | 2022.10.09 |
---|---|
[Android Studio] 안드로이드 스튜디오 JAVA RecyclerView Button 클릭 이벤트 (0) | 2022.10.09 |
[Android Studio] 안드로이드 스튜디오 Java class 생성 (0) | 2022.09.19 |
[Android Studio] 안드로이드 스튜디오 Java firebase DB 연동 (0) | 2022.09.19 |
[Android Studio] 안드로이드 스튜디오 Java Intent, 창 변환 (0) | 2022.09.19 |