티스토리 뷰

리스트 뷰(List view)

리스트뷰란 ?  

사용자가 정의한 데이터 목록을 아이템 단위로 구성하여 화면에 출력하는 ViewGroup의 한 종류이다. 

일반 위젯 : 데이터를 직접 설정할 수 있다. , 체크박스 , 라디오 버튼

어댑터뷰(선태 위젯) : 데이터가 담긴 Adapter를 이용하여 설정





간단한 리스트뷰의 예시


소스코드(Activity)


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
package com.example.sh415.project4_1;
 
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
 
public class Listviewactivity extends AppCompatActivity
        implements AdapterView.OnItemClickListener{
        ListView lv;
        ArrayAdapter<String> myAdapter;
    String[] items = {"코난","장미","미란","키드","명한",
    "애리","도일","아름","박사"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listview);
        myAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,items);
        lv = (ListView)findViewById((R.id.lv));
        lv.setAdapter(myAdapter);
        lv.setOnItemClickListener(this);
    }
 
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Toast.makeText(this,items[position],Toast.LENGTH_SHORT).show();
    }
}
 
cs





소스코드(layout)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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="wrap_content">
 
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/lv">
 
    </ListView>
 
 
</LinearLayout>
cs




미란을 터치하였을때 간단한 토스트 메시지로 '미란'이 출력되는 모습이다.






명한을 터치하였을때 간단한 토스트 메시지로 '명한'이 출력되는 모습이다.





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