본문 바로가기
수업내용

20230725 Android 다른화면으로 넘어가기

by titlejjk 2023. 7. 25.

 

activity_datil.xml에 아래와 같이 layout을 추가해준다.

 

<?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"
    tools:context=".DetailActivity">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="0dp"
        android:layout_height="300dp"
        android:layout_marginBottom="115dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/writer"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="작성자:천옥희"
        android:textSize="20sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/imageView" />

    <TextView
        android:id="@+id/caption"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="어쩌구 저쩌구..."
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/writer" />

    <TextView
        android:id="@+id/regdate"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="2023.03.07 13:00"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/caption" />

    <Button
        android:id="@+id/deleteBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="249dp"
        android:text="삭제"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/regdate"
        android:visibility="invisible"/>
</androidx.constraintlayout.widget.ConstraintLayout>

Intent에 GalleryDto를 담아 주기 위해 implements Serializable 해준다.

package com.example.step04gallery;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        ImageView imageView = findViewById(R.id.imageView);
        TextView writer=findViewById(R.id.writer);
        TextView caption=findViewById(R.id.caption);
        TextView regdate=findViewById(R.id.regdate);

        //DetailActivity가 활성화 될 때 전달 받은 Intent 객체의 참조값 얻어오기
        //MainActivity에서 생성한 Intent 객체이기 때문에
        //"dto"라는 키값으로 GalleryDto객체가 들어있다.
        Intent intent=getIntent();
        GalleryDto dto=(GalleryDto) intent.getSerializableExtra("dto");
        writer.setText(dto.getWriter());
        caption.setText(dto.getCaption());
        regdate.setText(dto.getRegdate());
        //이미지 출력(Glide를 활용)
        Glide.with(this)
                .load(dto.getImagePath())
                .fitCenter()
                .placeholder(R.drawable.ic_launcher_background)
                .into(imageView);

    }
}

 

'수업내용' 카테고리의 다른 글

20230726 Android Fragment  (0) 2023.07.26
20230726 Kotlin  (0) 2023.07.26
20230725 Android  (0) 2023.07.25
20230724 Android HttpRequest2  (0) 2023.07.24
20230721 Android HttpRequest  (0) 2023.07.21

댓글