안드로이드 앱을 제작할 때 내가 원하는 방식으로 리스트뷰를 만들어야 할 경우가 굉장히 많다!
오늘은 안드로이드 앱 내에서 커스텀 리스트뷰 만들기!
대충 내용은 다음과 같다.
MainActivity에 연결되어있는 layout에 custom_listview 라는 id를 가진 리스트뷰를 생성한 후 MainActivity에 ListView를 선언.
학생 정보를 저장 할 Student Class를 생성한다.
ListView안에 들어 갈 layout을 생성 (ListView안에 내용을 커스텀 하기위해 각각의 리스트뷰 안에 들어가는 내용을 설정해 줄 layout이 필요함) list_item를 생성한 후
CustomListAdapter를 생성해서 ListView 안에 list_item의 내용이 나올 수 있도록 해준다.
시작!
1. MainActivity layout에 listview를 생성해준다.
1 2 3 4 5 6 7 | <ListView android:id="@+id/custom_listview" android:layout_width="match_parent" android:layout_height="match_parent"> </ListView> | cs |
2. MainActivity에 listview를 선언해준다.
1 2 3 4 5 6 7 8 9 | @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ... listview = findViewById(R.id.custom_listview); //MainActivity에 ListView listview = findViewById(R.id. custom_listview); 반드시 작성 해줄것! } } | cs |
3. file -> new -> javaclass를 선택해서학생정보가 들어갈 Student Class를 선언해준다. (왼쪽 창 java에서 우클릭도 가능)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | public class Student { String name; String grade; public String getName() { return code; } public void setName(String name) { this.name = name; } public String getGrade() { return grade; } public void setGrade(String grade) { this.grade = grade; } } | cs |
내용은 다음과 같이 수정해준다.
팁! String name, String grade 만 쓴 후 (Mac환경 기준) command + R 을 누른 후 Getter and Setter을 눌러주면 자동적으로 생성된다. (윈도우는... control + n...?)
4. file -> new -> layout resource file을 선택해서 list_item 만들어 준다.
내용은 자신이 ListView에 들어갈 원하는 내용으로 수정하여 사용한다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TextView android:id="@+id/list_item_name_textview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="20dp" android:gravity="left" android:textSize="15dp" /> <TextView android:id="@+id/list_item_grade_textview" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginBottom="10dp" android:layout_marginRight="10dp" android:gravity="right"/> </LinearLayout> | cs |
5. file -> new -> javaclass를 선택해서 CustomListAdapter를 만들어 준다. (왼쪽 창 java에서 우클릭도 가능)
6. CustomListAdapter를 작성해준다.
7. MainActivity에 ListView와 CustomListAdapter를 연결해준다. (10번 줄에서 student 안에 내용은 알아서 넣을것!)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class MainActivity extends AppCompatActivity { ListView listview; CustomListAdapter listAdapter = new CustomListAdapter(); Student [] students; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list); listview = findViewById(R.id.custom_listview); student = //(데이터를 넣어준다.) setListView(students); } public void setListView(final Student[] students) { list_listview = (ListView) findViewById(R.id.list_listview); list_listview.setAdapter(listAdapter); for (Student student : students) { listAdapter.addItem(student); } } } | cs |
'ANDROID - JAVA' 카테고리의 다른 글
[ANDROID - JAVA] GMT 시간 TimeZone 설정하기 (0) | 2019.03.15 |
---|---|
[ANDROID - JAVA] 디바이스에 데이터 저장하기 (SharedPreferences 사용하기) (0) | 2019.01.19 |
[ANDROID - JAVA] Picasso 이미지 라이브러리 사용하기 (URL로 이미지 보여주기) (0) | 2019.01.17 |
[ANDROID - JAVA] 원하는 크기의 팝업창, 다이얼로그 만들기 (0) | 2019.01.16 |
[ANDROID - JAVA] 여러개의 버튼 각각 다른 이벤트 주기 (0) | 2019.01.15 |