[안드로이드 스튜디오] 메모장 어플 만들어보기 - 2 : ROOM을 이용한 데이터베이스
vnfmadl234.tistory.com/8 [안드로이드 스튜디오] 메모장 어플 만들어보기 안드로이드 스튜디오 공부를 시작하면서 내가 처음으로 만들 수 있는 것이 무엇이 있을까 하고 생각하던 중, 평소 할 일들을
vnfmadl234.tistory.com
지난 글
저번 메모장 ROOM DB에 이어서 메모를 길게 클릭 했을 때 수정 혹은 삭제를 구현해보기로 한다. 또 메모를 한번 클릭 했을 경우 해당 메모를 크게 볼 수 있게 만들어 줄 것이다.
일단 메모를 클릭 했을 때 보여줄 클래스를 만든다.
activity_note_result.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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp">
<ImageButton
android:id="@+id/backButton"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="@drawable/ic_keyboard_backspace_24px"
android:text="취소"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/titleResult"
android:layout_width="match_parent"
android:layout_height="69dp"
android:ems="10"
android:inputType="textPersonName"
android:text="제목"
android:textSize="24dp"
android:maxLength="100"
android:gravity="top"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/contentResult"
android:layout_width="match_parent"
android:layout_height="258dp"
android:ems="10"
android:inputType="textPersonName"
android:text="내용"
android:textSize="28dp"
android:maxLength="200"
android:gravity="top"/>
</LinearLayout>
</LinearLayout>
간단히 제목과 내용을 보여줄 텍스트뷰 두개와 돌아가기 버튼 한개로 구성했다.
MemoActivityResult.java
package com.example.notebook3;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
public class MemoResultActivity extends AppCompatActivity {
private TextView titleResult;
private TextView contextResult;
private String title;
private String content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_memo_result);
titleResult = (TextView) findViewById(R.id.titleResult);
contextResult = (TextView) findViewById(R.id.contentResult);
Memo result = getIntent().getParcelableExtra("data");
title = result.getTitle();
content = result.getContent();
titleResult.setText(title);
contextResult.setText(content);
ImageButton backButton = (ImageButton) findViewById(R.id.backButton);
backButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
메모를 클릭 했을 때 parcelable 인터페이스를 통해 데이터 값을 전달받아 보여줄 수 있게한다.
RecyclerviewAdapter.java
void onBind(Memo memo, int position) {
textView1.setText(memo.getTitle());
textView2.setText(memo.getContent());
//클릭 시 메모 보기
itemView.setOnClickListener(v -> {
Intent intent = new Intent(itemView.getContext(), MemoResultActivity.class);
intent.putExtra("data", memo);
itemView.getContext().startActivity(intent);
});
setOnClickListener로 메모를 클릭 했을 때 새로운 인텐트를 생성해 데이터 값을 보내주도록 한다.
마지막으로 리사이클러뷰에서 메모간의 구별을 위해 아이템의 테두리를 만들어 적용했다.
note_item_drawble.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<stroke android:width="1dp" android:color="#000080" /> //남색으로 지정
<solid android:color="#00000000"/>
</shape>
</item>
<item android:top="1dp" android:bottom="1dp" android:right="1dp" android:left="1dp" >
<shape android:shape="rectangle" >
<stroke android:width="1dp" android:color="#000080" />
<solid android:color="#00000000" />
</shape>
</item>
</layer-list>
구현 화면


이번에는 메모를 길게 클릭 하면 다이얼로그를 띄워 메모를 수정하거나 삭제하는 기능을 만들 것이다.
//길게 클릭 시 수정, 삭제 다이얼로그
itemView.setOnLongClickListener(v -> {
AlertDialog.Builder ab = new AlertDialog.Builder(context); //context 객체를 생성해야 함
ab.setTitle("메모").setMessage("메모 선택");
ab.setNeutralButton("수정", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(itemView.getContext(), MemoDetailActivity.class);
intent.putExtra("data", memo);
itemView.getContext().startActivity(intent);
}
});
ab.setPositiveButton("삭제", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
listData.remove(memo);
MemoDatabase.getInstance(itemView.getContext()).memoDao().delete(memo);
notifyDataSetChanged();
}
});
ab.show();
return false;
});
전 글과 같이 setOnLongClickListener로 다이얼로그를 생성하고 다이얼로그에 보여줄 제목과 내용을 각각 입력한 후 해당 버튼의 기능들을 구현해준다.
하지만 다이얼로그를 생성할 떄 AlertDialog.Builder() 에 Context를 받아와야하는데 Context를 받아오는 방법은 여러가지가 있지만 나는 전역변수로 선언을 해주고 해당 context를 따로 생성해주어 사용하였다.
//길게 클릭 시 수정, 삭제 다이얼로그
itemView.setOnLongClickListener(v -> {
AlertDialog.Builder ab = new AlertDialog.Builder(context); //context 객체를 생성해야 함
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ItemViewHolder> {
private ArrayList<Memo> listData = new ArrayList<>();
private Context context; //전역 변수로 선언
private MemoDatabase db;
public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item, parent, false);
context = parent.getContext(); //context 생성
return new ItemViewHolder(view);
}

다이얼로그와 리사이클러뷰도 직접 커스텀으로 화면을 구성할 수 있지만 가장 기본적인 화면으로만 어플을 만들었다.
이렇게 첫 안드로이드 어플인 메모어플을 간단하게 만들어봤다. 어플을 만들면서 배우는 점도 많았고, 다양한 기능들이
생각보다 간편하게 사용할 수 있어 안드로이드 스튜디오는 다루기 좋은 소프트웨어인 것 같다. 사실 직접 어플을 만들면
실제로 작동하는 모습을 보니 재밌게 즐기면서 했던 것 같다.
git 주소: github.com/m3k0813/NoteBook
'안드로이드 스튜디오' 카테고리의 다른 글
[안드로이드 스튜디오] 군인 커뮤니티 앱 만들기(KorArmy) 정리 (0) | 2021.10.22 |
---|---|
[안드로이드 스튜디오] 메모장 어플 만들어보기 - 2 : ROOM을 이용한 데이터베이스 (0) | 2021.01.17 |
[안드로이드 스튜디오] 메모장 어플 만들어보기 : RecyclerView로 메모 리스트 만들기 (0) | 2021.01.14 |