티스토리 뷰

#ListView 만들기





1. activity_main.xml 작성


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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"

   android:paddingBottom="@dimen/activity_vertical_margin"

   android:paddingLeft="@dimen/activity_horizontal_margin"

   android:paddingRight="@dimen/activity_horizontal_margin"

   android:paddingTop="@dimen/activity_vertical_margin"

   tools:context="com.ktds.smahn.listviewapplication.MainActivity">

   <ListView

       android:id="@+id/listView"

       android:layout_width="match_parent"

       android:layout_height="match_parent" />

</RelativeLayout>

Colored by Color Scripter

cs



2. activity_my_list_adapter.xml 작성


1

2

3

4

5

6

7

8

9

10

11

12

13

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

   xmlns:tools="http://schemas.android.com/tools"

   android:orientation="vertical"

   android:layout_width="match_parent"

   android:layout_height="match_parent">

   <TextView

       android:id="@+id/textView"

       android:layout_width="match_parent"

       android:layout_height="100dp" />

</LinearLayout>

Colored by Color Scripter

cs



3. MainActivity.java 작성


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

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

package com.ktds.smahn.listviewapplication;

import android.content.Context;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.BaseAdapter;

import android.widget.ListView;

import android.widget.TextView;

import android.widget.Toast;

import java.util.ArrayList;

import java.util.List;

public class MainActivity extends AppCompatActivity {

   private ListView listView;

   @Override

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);

       setContentView(R.layout.activity_main);

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

       listView.setAdapter(new MyLisAdapter(this));

   }

   private class MyLisAdapter extends BaseAdapter {

       private List<String> list;

       private Context context;

       public MyLisAdapter(Context context) {

           this.context = context;

           list = new ArrayList<String>();

           list.add("첫번째");

           list.add("두번째");

           list.add("세번째");

           list.add("네번째");

           list.add("다섯번째");

           list.add("여섯번째");

           list.add("일곱번째");

           list.add("여덟번째");

       }

       @Override

       public int getCount() {

           return list.size();

       }

       @Override

       public Object getItem(int position) {

           return list.get(position);

       }

       @Override

       public long getItemId(int position) {

           return position;

       }

       @Override

       public View getView(int position, View convertView, ViewGroup parent) {

           if(convertView == null){

               LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

               convertView = inflater.inflate(R.layout.activity_my_list_adapter, parent, false);

           }

           TextView textView = (TextView) convertView.findViewById(R.id.textView);

           textView.setText((String) getItem(position));

           textView.setOnClickListener(new View.OnClickListener() {

               @Override

               public void onClick(View v) {

                   Toast.makeText(v.getContext(), ((TextView)v).getText() + " 선택함.", Toast.LENGTH_SHORT).show();

               }

           });

           return convertView;

       }

   }

}

Colored by Color Scripter

cs



4. 실행


공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/12   »
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
글 보관함