-
[3] ListViewDevelpment/Android Using Sample 2020. 9. 13. 21:34
1. ListView
Layout
1234567891011121314151617<?xml version="1.0" encoding="utf-8"?><LinearLayoutandroid: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"><TextViewandroid: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
1234567891011121314151617181920212223public class MainActivity extends AppCompatActivity {ListView list_view;MyAdapter adapter;ArrayList<String> arr;@Overrideprotected 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
123456789101112131415161718192021222324252627public 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@Overridepublic 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
123456789101112131415161718192021222324252627282930313233343536public 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() {@Overridepublic 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@Overridepublic 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
1234567891011121314151617181920212223public class MainActivity extends AppCompatActivity {ListView list_view;MyAdapter adapter;ArrayList<String> arr;@Overrideprotected 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