๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
์‹œ๋„/Smart_Fan

[Smart_Fan] 08 - Android Studio : Fragment์•ˆ์— ListView ๊ตฌํ˜„ํ•˜๊ธฐ 01

by ๐Ÿ‡๋ฐ•๋ด‰๋ด‰๐Ÿ‡ 2020. 8. 8.

 

 

Fragment์•ˆ์— ListView ๊ตฌํ˜„ํ•˜๊ธฐ

[2020-07-13 ~ 2020-07-14]

 

๊ธฐ์กด์— ์šฐ๋ฆฌ๋Š” Navigation Drawer Menu์—์„œ

Fragment๋ฅผ ์‚ฌ์šฉํ•ด ๋ฉ”๋‰ด์˜ ํ•ญ๋ชฉ๋“ค์„ ํด๋ฆญํ•  ๋•Œ๋งˆ๋‹ค

๊ฐ ํŽ˜์ด์ง€๋ฅผ ์ด๋™ํ•˜๋„๋ก ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

 

๊ทธ ์ดํ›„์— Fragment์ฐฝ ์•ˆ์—์„œ ๋ฆฌ์ŠคํŠธ ํ•ญ๋ชฉ๋“ค์„ ์ž‘์„ฑํ•˜๊ธฐ ์œ„ํ•ด์„œ

๊ธฐ์กด์— Activity์—์„œ ListView๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•๋Œ€๋กœ ListView๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

์ด๋ฅผ ๊ณ„๊ธฐ๋กœ ์—ฌ๋Ÿฌ ์˜์ƒ๊ณผ ์‚ฌ์ดํŠธ๋“ค์„ ์ฐพ์•„ ๊ตฌํ˜„ํ•ด๋‚ด๋Š” ๋ฐฉ๋ฒ•์„ ์•Œ๊ฒŒ ๋˜์—ˆ๊ณ ,

ListView๋ฅผ ํ•œ ๋ฒˆ์— ์ž‘์„ฑํ•˜๊ธฐ์—๋Š” ์˜ค๋ฅ˜๊ฐ€ ์—ผ๋ ค๋˜์–ด

์ด 3๋ฒˆ์— ๊ฑธ์ณ์„œ ListView๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์•˜๋‹ค.

 

 

#01. fragment ์•ˆ์— ListView ํ˜•ํƒœ ์ž…๋ ฅํ•˜๊ธฐ

 

 

  • fragment_settings.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SettingsFragment">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/mainMenu"/>

</FrameLayout>

 

๋จผ์ € ๊ธฐ์กด์— ListView๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ• ๊ทธ๋Œ€๋กœ ๋ ˆ์ด์•„์›ƒ ์•ˆ์— ListView๋ฅผ ์ž…๋ ฅํ•ด ์ค€๋‹ค.

 

์ด๋•Œ, FrameLayout์•ˆ์— ListView๋ฅผ ๋„ฃ์—ˆ๋‹ค๋Š” ๊ฒƒ์„ ์žŠ์ง€ ์•Š๋„๋ก ํ•œ๋‹ค.

 

์ดํ›„์—๋Š” ์ž๋ฐ”ํŒŒ์ผ์„ ์ž…๋ ฅํ•ด์ฃผ๋ฉด ๋˜๋Š”๋ฐ

 

 

error: no suitable constructor found for ArrayAdapter(MainActivityFragmet, int, String [])

...

error: cannot find symbol method findViewById(int)

๊ธฐ์กด์— Activity์—์„œ ์ž…๋ ฅํ–ˆ๋˜ ๋ฐฉ๋ฒ•์œผ๋กœ ListView๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด

๋‘ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๊ฒŒ ๋œ๋‹ค.

 

 

์ฒซ ๋ฒˆ์งธ ์—๋Ÿฌ
error: no suitable constructor found for ArrayAdapter(MainActivityFragment, int, String [])

ํ•ด๋‹น ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ์›์ธ์„ ์•Œ๊ธฐ ์ „์— ArrayAdapter์— ๋Œ€ํ•ด์„œ ๊ฐ„๋‹จํžˆ ์‚ดํŽด๋ณด์ž๋ฉด

ArrayAdapter์˜ ์ƒ์„ฑ์ž๋Š” ์ด 3๊ฐœ์˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๊ฐ€์ง€๊ฒŒ ๋˜๋Š”๋ฐ

context, resource, objects์ด๋‹ค.

 

 

ArrayAdapter ( Context context, int resource, T [ ] objects )

context : ์•ˆ๋“œ๋กœ์ด๋“œ ์‹œ์Šคํ…œ์—์„œ ์ œ๊ณต๋˜๋Š” ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ „์—ญ ํ™˜๊ฒฝ ์ •๋ณด์— ๋Œ€ํ•œ ์ธํ„ฐํŽ˜์ด์Šค

 

resource : ๋ทฐ๋ฅผ ์ธ์Šคํ„ด์Šคํ™” ํ•  ๋•Œ ์‚ฌ์šฉํ•  ๋ ˆ์ด์•„์›ƒ์ด ํฌํ•จ๋œ ๋ ˆ์ด์•„์›ƒ ํŒŒ์ผ์˜ ๋ฆฌ์†Œ์Šค ID

 

objects : ๋ฐฐ์—ด๋กœ ์„ ์–ธ๋œ ์‚ฌ์šฉ์ž ๋ฐ์ดํ„ฐ

 

 

๊ธฐ์กด์— Activity๋Š” android.content.Context๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์—

Context๋ฅผ ๊ทธ๋Œ€๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด onCreate() ํ•จ์ˆ˜์—์„œ ArrayAdapter ์ƒ์„ฑ์ž์˜ ์ฒซ ๋ฒˆ์งธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ this๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์—ˆ์ง€๋งŒ,

Fragment๋Š” java.lang.Object๋กœ๋ถ€ํ„ฐ ์ƒ์†๋ฐ›์€ ํด๋ž˜์Šค์ด๊ธฐ ๋•Œ๋ฌธ์— 

Context์™€๋Š” ๊ด€๊ณ„๊ฐ€ ์—†์–ด this๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๊ฒŒ ๋œ๋‹ค.

 

๊ทธ๋ž˜์„œ ์ด๋ฅผ ํ•ด๊ฒฐํ•ด ์ฃผ๊ธฐ ์œ„ํ•ด์„œ

Fragment์—์„œ ์ž์‹ ์ด ์†ํ•œ Activity์˜ ์ฐธ์กฐ๋ฅผ ์–ป์–ด์˜จ ๋‹ค์Œ์—

ArrayAdapter์˜ ์ƒ์„ฑ์ž๋กœ ์ „๋‹ฌํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

์ฆ‰, Fragment์—์„œ Activity์˜ ์ฐธ์กฐ๋ฅผ ํš๋“ํ•  ์ˆ˜ ์žˆ๋Š” getActivity() ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

๋‘ ๋ฒˆ์งธ ์—๋Ÿฌ
error: cannot find symbol method findViewById(int)

๋‘ ๋ฒˆ์งธ ์—๋Ÿฌ์˜ ์›์ธ์€ findViewById() ํ•จ์ˆ˜์ธ๋ฐ

ํ•ด๋‹น ํ•จ์ˆ˜๋Š” Fragment์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜๊ฐ€ ์•„๋‹Œ View์˜ ๋ฉค๋ฒ„ ํ•จ์ˆ˜ ์ด๊ธฐ ๋•Œ๋ฌธ์— 

Fragment์—์„œ findViewById() ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋‹น์—ฐํ•˜๊ฒŒ ์—๋Ÿฌ๊ฐ€ ๋ฐœ์ƒํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

์ด๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” LayoutInflater๋ฅผ ์‚ฌ์šฉํ•ด Resource Layout์„ View๋กœ ๋ณ€ํ™˜ํ•ด์ค€ ํ›„

findViewByid()๋ฅผ ํ˜ธ์ถœํ•ด ์ฃผ๋ฉด ๋œ๋‹ค.

 

 

  • SettingsFragment.java
import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingsFragment extends Fragment {


    public SettingsFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
	// LayoutInflater ์‚ฌ์šฉํ•ด Resource Layout์„ View๋กœ ๋ณ€ํ™˜ํ•ด์ค€ ํ›„ findViewById() ํ˜ธ์ถœ
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_settings, container, false);
	
	// ListView ์•„์ดํ…œ์— ํ‘œ์‹œ๋  ์‚ฌ์šฉ์ž ํ…Œ์ดํ„ฐ ์ •์˜
        String[] menuItems = {"number one", "number two", "number three", "number four"};

        ListView listView = (ListView) view.findViewById(R.id.mainMenu);

	// ๋ฐ์ดํ„ฐ ์ž…๋ ฅ๋ฐ›์„ Adapter ์ƒ์„ฑ
    	// fragment์—์„œ๋Š” 'this' ์‚ฌ์šฉ์ด ๋ถˆ๊ฐ€ํ•˜๋ฏ€๋กœ, Activity์˜ ์ฐธ์กฐ ํš๋“์ด ๊ฐ€๋Šฅํ•œ getActivity()ํ•จ์ˆ˜ ์‚ฌ์šฉ
        ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, menuItems);

        listView.setAdapter(listViewAdapter);

        return view;
    }
}

 

๋‘ ๊ฐ€์ง€์˜ ์—๋Ÿฌ๋ฅผ ํ•ด๊ฒฐํ•œ ํ›„ ์ œ๋Œ€๋กœ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•œ ๋ชจ์Šต์ด๋‹ค.

 

 

์• ๋ฎฌ๋ ˆ์ดํ„ฐ๋ฅผ ํ†ตํ•ด ํ™•์ธํ•ด ๋ณด๋ฉด 

fragment์•ˆ์— listView๊ฐ€ ๋ณด์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

 


 

 

fragment์—์„œ listView๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค๋Š” ๊ฒƒ์„ ์ด๋ฒˆ์— ์ฒ˜์Œ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.

 

์ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ธฐ ์œ„ํ•ด์„œ ๊ต‰์žฅํžˆ ์˜ค๋žœ ์‹œ๊ฐ„์ด ๊ฑธ๋ ธ์ง€๋งŒ

ํ•ด๋‹น ๋ฌธ์ œ๊ฐ€ ํ•ด๊ฒฐ๋˜๊ณ  ๋‚˜๋‹ˆ ๊ต‰์žฅํžˆ ๋ฟŒ๋“ฏํ•˜๋‹ค.

 

๋‹ค์Œ์—๋Š” ๋ฆฌ์ŠคํŠธ๋ทฐ์˜ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•˜๊ณ  ํด๋ฆญํ•˜๋ฉด

๋‹ค์Œ ํŽ˜์ด์ง€๋กœ ๋„˜์–ด๊ฐˆ ์ˆ˜ ์žˆ๊ฒŒ ํ•ด ๋ณด์•„์•ผ๊ฒ ๋‹ค.

 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€