Bài giảng Lập trình Android: Bài 3 - TTTH ĐH KHTN

13 10 0
Bài giảng Lập trình Android: Bài 3 - TTTH ĐH KHTN

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Bài 3 trình bày các nội dung liên quan đến ListView và cách tạo Custom Layout cho ListView. Thông qua bài giảng này người học có thể biết được ListView là gì? Các bước thực hiện để sử dụng một ListView, các xử lý trên ListView,... Mời các bạn cùng tham khảo để nắm bắt các nội dung chi tiết.

Trường ĐH Khoa Học Tự Nhiên Tp Hồ Chí Minh TRUNG TÂM TIN HỌC Lập trình Android Bài 3: ListView Tạo Custom Layout cho ListView Ngành Mạng & Thiết bị di động www.t3h.vn 2015 Nội dung I ListView ListView gì? Các bước thực để sử dụng ListView Các xử lý ListView II Tạo Custom Layout cho ListView Lập trình Android ListView gì? ListView dạng điều khiển nâng cao có chức hỗ trợ hiển thị liệu theo dạng dịng Lập trình Android Các bước thực để sử dụng ListView  Bước 1: Đưa liệu cần hiển thị lên ListView vào mảng danh sách (ArrayList, mảng thông thường, mảng đối tượng,…)  Bước 2: Tạo ListView giao diện  Bước 3: Tạo đối tượng ArrayAdapter để liên kết giữ ListView mảng danh sách liệu ArrayAdapter gì? ArrayAdapter hiểu modem mạng Nó giúp ListView “đọc hiểu” liệu từ mảng liệu để lên giao diện Lập trình Android Các xử lý ListView  Lấy ListView thông qua Id ListView ListView lvTenLV = (ListView) findViewById(R.id.idcuaListView);  Tạo ArrayAdapter ArrayAdapter[Tên mảng adapter];  Đưa liệu từ mảng vào ArrayAdapter [Tên mảng adapter] = new ArrayAdapter (this,android.R.layout.simple_list_item_1,[tenMangDuLieu]);  Cách đổ liệu lên ListView lvTenLV.setAdapter(tenMangAdapter); Lập trình Android Nội dung I ListView II Tạo Custom Layout cho ListView Custom Layout gì? Tại phải tạo Custom Layout? Cách sử dụng ImageView Các bước thực để tạo Custom Layout cho ListView Hướng dẫn tạo lớp ArrayAdapter Lập trình Android Tại phải tạo Custom Layout?  Custom Layout layout tạo tùy biến lại cho phù hợp với yêu cầu sử dụng  Minh họa khác ListView sử dụng layout bình thường ListView có layout tùy biến: Lập trình Android – Module Cách sử dụng ImageView  Tạo giao diện sử dụng ImageView: Chọn biểu tượng ImageView Palette → Images & Media → ImageView  Lấy ImageView tạo thơng qua Id nó: ImageView tenImageView=(ImageView)findViewById(R.id.idImageView);  Đưa hình ảnh vào ImageView: tenImageView.setImageDrawable(R.drawable.tenHinh);  Chú ý: Ở đây, hình nằm thư mục res/drawable Tên hình phải viết liền chữ phải chữ thường Lập trình Android Các bước tạo sử dụng Custom Layout Sau có ListView giao diện, tạo Custom Layout cho ListView sau: Bước 1: Tạo thêm layout cho item ListView Bước 2: Tạo lớp Custom Adapter kế thừa từ lớp ArrayAdapter Bước 3: Tạo lớp dùng để quản lý liệu Bước 4: Hiển thị liệu lên ListView Lập trình Android Lớp Custom ArrayAdapter  Lớp Custom ArrayAdapter kế thừa từ lớp ArrayAdapter public class CustomAdapter extends ArrayAdapter{ public CustomAdapter(Context context, int resource, ArrayList objects) { super(context, resource, objects); } } Lập trình Android 10 Lớp Custom ArrayAdapter  Phương thức getView() @Override public View getView(int position, View convertView, ViewGroup parent){ View view = null; LayoutInflater inflater = (LayoutInflater) context.getSystemService (Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(resId, null); //Lấy điều khiển //Lấy nội dung cần hiển thị vị trí thứ position [Kiểu mảng] c = tenMang.get(position); if (c != null) { //Đưa thông tin lên TextView hình lên ImageView textView.setText(c.getThongTin()); imageView.setImageResource(c.getImg_id()); } return view; } Lập trình Android 11 Lớp Custom ArrayAdapter Với tham sớ: position: vị trí phần tư danh sách convertView: dùng để lấy control item parent: datasource truyền vào từ MainActivity Chú ý: View dạng tham chiếu đối tượng, nên thay đổi đối tượng bên View biết thay đổi trả view này, tức trả thơng sớ mà ta vừa thay đổi Lập trình Android 12 Lập trình Android – Module 13 ... bên View biết thay đổi trả view này, tức trả thông số mà ta vừa thay đổi Lập trình Android 12 Lập trình Android – Module 13 ... ListView II Tạo Custom Layout cho ListView Lập trình Android ListView gì? ListView dạng điều khiển nâng cao có chức hỗ trợ hiển thị liệu theo dạng dòng Lập trình Android Các bước thực để sử dụng... 2: Tạo lớp Custom Adapter kế thừa từ lớp ArrayAdapter Bước 3: Tạo lớp dùng để quản lý liệu Bước 4: Hiển thị liệu lên ListView Lập trình Android Lớp Custom ArrayAdapter  Lớp Custom ArrayAdapter

Ngày đăng: 09/05/2021, 18:01

Mục lục

  • Lập trình Android Bài 3: ListView. Tạo Custom Layout cho ListView

  • Nội dung

  • 1. ListView là gì?

  • 2. Các bước thực hiện để sử dụng ListView

  • 3. Các xử lý trên ListView

  • Slide 6

  • 2. Tại sao phải tạo Custom Layout?

  • 2. Cách sử dụng ImageView

  • 3. Các bước tạo và sử dụng Custom Layout

  • 4. Lớp Custom ArrayAdapter

  • Slide 11

  • Slide 12

  • PowerPoint Presentation

Tài liệu cùng người dùng

Tài liệu liên quan