Louie NRT Story

[안드로이드] DataBinding 본문

전기차충전기

[안드로이드] DataBinding

hyeok0724.kim@gmail.com 2021. 12. 9. 19:22
반응형

작성일: 21년 12월 9일

 

Index

1. 기존 코드

2. dataBinding 설정

 

1. 기존 코드

 1) MainActivity 코드

  - 데이터 바인등을 통해 설정한 정보를 볼수 있도록 함

public class MainActivity extends AppCompatActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        fetchUserProfile();
    }

    private void fetchUserProfile() {
        UserProfile userProfile = new UserProfile();
        userProfile.name = "홍길동";
        userProfile.phone = "01012345678";
        userProfile.address = "서울시 서초구";
        
        updateUI(userProfile);
    }

	//데이터 바인딩
    private void updateUI(UserProfile userProfile) {
        binding.name.setText(userProfile.name);
        binding.phone.setText(userProfile.phone);
        binding.address.setText(userProfile.address);
    }
}

 

 2) UserProfile 코드

public class UserProfile {
    public String name;
    public String phone;
    public String address;

    public UserProfile(){
        this.name = name;
        this.phone = phone;
        this.address = address;
    }
}

 

 3) main_activity.xml 코드

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  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"
  android:orientation="vertical"
  tools:context=".MainActivity">

<TextView
  android:id="@+id/name"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
  
<TextView
  android:id="@+id/phone"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
  
<TextView
  android:id="@+id/address"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"/>
</LinearLayout>

 

 4) 실행 결과물

  - ViewBinding을 통하여 아래와 같이 화면을 볼 수 있음

 

 

2. dataBinding 설정

 1) build.gradle 모듈 추가 

  - dataBinding{ enabled true }

 

 2) MainActivity 코드

  - updateUI() 라는 데이터바인딩 코드가 삭제됨

  - binding.setUserProfile(userProfile) 함수를 통하여 데이터 바인딩 하도록함

public class MainActivity extends AppCompatActivity {

    private static final String LOG_TAG = MainActivity.class.getSimpleName();
    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        fetchUserProfile();
    }

    private void fetchUserProfile() {
        UserProfile userProfile = new UserProfile();
        userProfile.name = "홍길동";
        userProfile.phone = "01012345678";
        userProfile.address = "서울시 서초구";

        binding.setUserProfile(userProfile);
    }
}

 

 3) main_activity.xml 코드

  - 전달된 userProfile 데이터를 여기서 바인딩 함

<?xml version="1.0" encoding="utf-8"?>
<layout 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">
    <data>
        <variable
            name="userProfile"
            type="com.example.logging_exam.UserProfile" />
    </data>
    
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userProfile.name}"/>
            
        <TextView
            android:id="@+id/phone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userProfile.phone}"/>
            
        <TextView
            android:id="@+id/address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{userProfile.address}"/>
            
    </LinearLayout>
</layout>

 

 4) 실행 결과물

  - ViewBinding을 통하여 아래와 같이 화면을 볼 수 있음

 

 

 

Referece:

https://youtu.be/l6FgSwEgu2E

 

반응형
Comments