Trong lập trình Android, sẽ sở hữu khi bạn nên hiển thị một danh sách những đối tượng nào đó, ví dụ danh sách những sinh viên, danh sách các mặt hàng…Việc dùng những View bình thường để hiển thị hầu hết danh sách là việc vô cùng mất công và thời gian. Nhưng vô cùng may trong Android đã hỗ trợ một mẫu View để làm việc này. đấy chính là ListView.
1. Hướng dẫn tạo ListView đơn giản.
Bước 1. người mua tạo một project mới mang tên là ListViewSample (Bạn mang thể xem các tạo một project tại đây)
Bước 2. Trong file xml người dùng khởi tạo một ListView như sau.
Bước 3. các bạn chuyển vào file MainActivity.java và viết code để tạo listview như sau.
Bước 4. Run project và được kết quả là một danh sách đơn thuần
2. Hướng dẫn custom ListView để hiện thị 1 danh sách phức tạp.
Trong thực tế, rất ít lúc danh sách hiển thị những đối tượng chỉ sở hữu 1 thuộc tính. Vậy đề nghị chúng ta phải custom lại ListView để hiện thì được danh sách những đối tượng có phổ biến thuộc tính hơn. người mua hãy làm theo các bước dưới đây.
Bước 1. Khởi tạo 1 project mới với tên CustomListViewSample.
Bước 2. Tạo đối tượng (Trong Android chính là một class) thể hiện đối tượng mà mình muốn hiển thị. Ví dụ mình nên hiển thị danh sách những sinh viên đăng ký học tại Devpro. Ta tạo một class như sau. Tạo một đối tượng với tên là Student mang những thuộc tính ID, Name, Phone sau đó tạo get/set cho đối tượng.
Xem thêm:Nội dung khoa hoc lap trinh game unity
Bước 3. Custom giao diện một item cho ListView
chúng ta sẽ hiển thị ảnh, tên và số điện thoại của học viên.
<?xml version="1.0″ encoding="utf-8″?>
<LinearLayout xmlns:android="http:// schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#fff"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/avatar" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="vertical">
<TextView
android:id="@+id/txtName"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:gravity="center_vertical"
android:text="Toan"
android:textColor="#000″
android:textStyle="bold" />
<TextView
android:id="@+id/txtPhone"
android:layout_width="fill_parent"
android:layout_height="25dp"
android:gravity="center_vertical"
android:text="0123″ />
</LinearLayout>
</LinearLayout>
Bước 4. Tạo một adapter mang tên StudentAdapter và cấu hình cho nó như sau.
public class StudentAdapter extends BaseAdapter
ArrayList<Student> listData;
LayoutInflater inflater;
// Hàm tạo của custom
public StudentAdapter(Context context, ArrayList<Student> listData)
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.listData = listData;
// Trả về số lượng phần tử được hiển thị trong listview
@Override
public int getCount()
return listData.size();
// Trả về đối tượng được lấy theo vị trí
@Override
public Object getItem(int position)
return listData.get(position);
@Override
public long getItemId(int position)
return 0;
// Hàm quan trọng nhất, hiển thị giao diện của listview
@Override
public View getView(int position, View convertView, ViewGroup parent)
// Lấy ra đối tượng bắt buộc hiển thị ở vị trí vật dụng position
Student item = listData.get(position);
// Khai báo các component
TextView txtName, txtPhone;
// Khởi tạo view.
if (convertView == null)
convertView = inflater.inflate(R.layout.item_listview, parent, false);
txtName = (TextView) convertView.findViewById(R.id.txtName);
txtPhone = (TextView) convertView.findViewById(R.id.txtPhone);
// Set dữ liệu vào item của list view
txtName.setText(item.getName());
txtPhone.setText(item.getPhone());
return convertView;
Bước 5. sau lúc đã có Adapter chúng ta vào trong file MainActivity.java và viết code như sau.
Bước 6. Sau lúc đã hoàn thành các bước trên, bạn tiến hành Run Project và xem thành quả của người mua.
Trên đây mình đã giới thiệu cho khách hàng biết về listview, từ listview cơ bản tới custom 1 listview theo ý muốn. giả dụ gặp khó khắn trong công đoạn code, các bạn với thể tham khảo khóa học Android tại đây để với thể phát triển thành một lập trình Android giỏi. Chúc quý khách thành công!
Gợi ý xem thêm:
EmoticonEmoticon