본문 바로가기

ANDROID - JAVA

[ANDROID - JAVA] 커스텀 Listview 만들기

안드로이드 앱을 제작할 때 내가 원하는 방식으로 리스트뷰를 만들어야 할 경우가 굉장히 많다!


오늘은 안드로이드 앱 내에서 커스텀 리스트뷰 만들기!


대충 내용은 다음과 같다.

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를 작성해준다.



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
public class CustomListAdapter extends BaseAdapter {
    private ArrayList<Student> listViewItemList = new ArrayList<>();
 
    Context context;
    Activity activity;
 
    public LastListAdapter(Context context, int size) {
        this.size = size;
    }
 
    @Override
    public int getCount() {
        return listViewItemList.size();
    }
 
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final int pos = position;
        final Context context = parent.getContext();
        this.context = context;
        final Student student = listViewItemList.get(position);
 
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.list_item, parent, false); // list_item이 아닌 다른 이름으로 작성하였다면 그에 맞는 이름을 작성 할 것!
        }
 
        TextView nameTextView = (TextView) convertView.findViewById(R.id.list_item_name_textview); // list_item에서 선언해준 name을 적기위한 부분의 id
        TextView gradeTextView = (TextView) convertView.findViewById(R.id.list_item_grade_textview); // list_item에서 선언해준 grade를 적기위한 부분의 id
        
        nameTextView.setText(student.getName);
        gradeTextView.setText(student.getGrade);
        return convertView;
    }
 
 
    @Override
    public long getItemId(int position) {
        return position;
    }
 
    @Override
    public Object getItem(int position) {
        return listViewItemList.get(position);
    }
 
    public void addItem(Student student) {
        listViewItemList.add(student);
    }
}
 
cs



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