Android App

RecyclerViewWithEditText in Android Studio

YunSeong 2021. 8. 2. 14:34
728x90
반응형

RecyclerViewWithEditText

 

https://yunseong.tistory.com/entry/Android-App-Study5-recyclerView

 

Android App Study_5 (recyclerView)

RecyclerView https://youtu.be/kNq9w1_nhL4 일단 recyclerView는 여러 정보들을 item으로 묶어서 각각 표시해주는 그런 것 같다. 예를 들어서 받은 이메일들이 위 아래로 쭉 정렬되있는 것등을 구현할 때 사용되.

yunseong.tistory.com

위 링크의 페이지에서 만든 recyclerView에서는 정해져 있는 내용만 띄울 수 있었기 때문에 이번에 EditText를 이용해서 입력한 내용을 recyclerView에 띄울 수 있도록 했다. 

 

 

 

1 activity_main.xml로 토대 만들기

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            ...
 
    <androidx.recyclerview.widget.RecyclerView
            ...
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <LinearLayout
            android:layout_width="10dp"
            android:layout_weight="1"
            android:layout_height="wrap_content"
            android:orientation="vertical">
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/et_1"
                android:hint="name"/>
            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="content"
                android:id="@+id/et_2"/>
        </LinearLayout>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:id="@+id/btn_add"
            android:text="추가"/>
    </LinearLayout>
</LinearLayout>
cs

위와 같이 LinearLayout들을 활용해서 밑의 사진과 같이 위젯들을 배치했다.

 

 

 

2 MainActivity.java에 코드 추가하기 

위 링크 페이지에서 버튼을 누르면 미리 만들어놨던 MainData class의 형태로 recyclerView에 item으로 띄울 정보들을 저장한 후 arrayList에 저장해서 adapter에 전달했다. 그렇기 때문에 MainData형태로 정보를 저장하는 부분에서 

 

((EditText)findViewById(R.id.et_1)).getText().toString()을 이용해서 

밑의 코드와 같이 버튼을 눌렀을 때 이벤트를 만들어줬다.

1
2
3
4
5
6
7
8
btn_add.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        MainData mainData = new MainData(R.mipmap.ic_launcher, et_1.getText().toString(), et_2.getText().toString());
        arrayList.add(mainData);
        mainAdapter.notifyDataSetChanged();
    }
});
cs

 

 

3 결과

그렇게 하면 밑의 사진과 같이 원하는 정보들을 recyclerView에 저장할 수 있다.

 

 

 

 

728x90
반응형