ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [3] ListView
    Develpment/Android Using Sample 2020. 9. 13. 21:34

    1. ListView


     Layout

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout
        android:id="@+id/inflater_layer"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
     
        <TextView
            android:id="@+id/inflater_txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="inflater"
            android:textSize="20dp"/>
     
    </LinearLayout>
    cs

    * ListView의 항목으로 들어갈 Layout(activity_inflater.xml) 생성 (기존 : activity_main.xml)

    * id를 등록


     Activity

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class MainActivity extends AppCompatActivity {
     
        ListView list_view;
        MyAdapter adapter;
        ArrayList<String> arr;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            arr = new ArrayList<>();
            arr.add("test1");
            arr.add("test2");
     
            list_view = (ListView)findViewById(R.id.list_view);
     
            adapter = new MyAdapter(this, R.layout.activity_inflater, arr);
     
            list_view.setAdapter(adapter);
        }
     
    }
    cs

    * 각 객체 생성 및 연결.

    * ListView에 adapter 세팅.

    * 예제의 경우 String List값으로 세팅.


     MyAdapter

     

    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
    public class MyAdapter extends ArrayAdapter<String>{
     
        Context context;
        int resource;
        ArrayList<String> arr;
     
        public MyAdapter(Context context, int resource, ArrayList<String> arr) {
            super(context, resource, arr);
     
            this.context = context;
            this.resource = resource;
            this.arr = arr;
        }
     
        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
     
            LayoutInflater linf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = linf.inflate(resource, null);
     
            TextView textView = (TextView)convertView.findViewById(R.id.inflater_txt);
            textView.setText(arr.get(position));
     
            return convertView;
        }
    }
    cs

    * 상황에 맞는 adapter 상속받음.

    * list의 개수에 따라 getView가 자동으로 호출.

    * position : List의 위치

    * convertView : ListView의 각 항목


    2. 결과화면


     


    3. ItemClickListener


     MyAdapter

     

    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
    34
    35
    36
    public class MyAdapter extends ArrayAdapter<String>{
     
        Context context;
        int resource;
        ArrayList<String> arr;
     
        public MyAdapter(Context context, int resource, ArrayList<String> arr, ListView list_view) {
            super(context, resource, arr);
     
            this.context = context;
            this.resource = resource;
            this.arr = arr;
     
            list_view.setOnItemClickListener( click );
        }
     
        AdapterView.OnItemClickListener click = new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Toast.makeText(getContext(), String.format("position : %d, Text : %s", i, arr.get(i)), Toast.LENGTH_SHORT).show();
            }
        };
     
        @NonNull
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
     
            LayoutInflater linf = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = linf.inflate(resource, null);
     
            TextView textView = (TextView)convertView.findViewById(R.id.inflater_txt);
            textView.setText(arr.get(position));
     
            return convertView;
        }
    }
    cs

    * MyAdapter 생성자에서 ListView 객체를 인자로 전달받음.

    * ListView 객체에 ItemClickListener 등록.


     Activity 

     

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    public class MainActivity extends AppCompatActivity {
     
        ListView list_view;
        MyAdapter adapter;
        ArrayList<String> arr;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
     
            arr = new ArrayList<>();
            arr.add("test1");
            arr.add("test2");
     
            list_view = (ListView)findViewById(R.id.list_view);
     
            adapter = new MyAdapter(this, R.layout.activity_inflater, arr, list_view);
     
            list_view.setAdapter(adapter);
        }
     
    }
    cs

    * adapter 생성자에서 추가로 ListView 객체를 전달.


    4. 결과화면


     



    'Develpment > Android Using Sample' 카테고리의 다른 글

    [5] Image Button Effect (drawable)  (0) 2020.09.13
    [4] Custom Button  (0) 2020.09.13
    [2] Inflater  (0) 2020.09.13
    [1] Touch Event  (0) 2020.09.13
    [0] Button Event  (0) 2020.09.13

    댓글

Designed by Tistory.