Thứ Tư, 22 tháng 6, 2016

Hướng dẫn tạo ListView trong Android

Trong lập trình Android, sẽ sở hữu khi bạn phải hiển thị 1 danh sách các đối tượng nào đấy, ví dụ danh sách những sinh viên, danh sách những mặt hàng…Việc sử dụng các View bình thường để hiển thị đa số danh sách là việc cực kỳ mất công và thời gian. Nhưng vô cùng may trong Android đã hỗ trợ 1 cái View để khiến việc này. đó chính là ListView.

1. Hướng dẫn tạo ListView đơn thuần.

Bước 1. khách hàng tạo một project mới mang tên là ListViewSample (Bạn với thể xem những tạo 1 project tại đây)

Bước 2. Trong file xml các bạn khởi tạo 1 ListView như sau.

39

Bước 3. quý khách 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 giản

40

2. Hướng dẫn custom ListView để hiện thị 1 danh sách phức tạp.

Trong thực tế, siêu ít khi danh sách hiển thị các đối tượng chỉ mang một thuộc tính. Vậy đề nghị chúng ta bắt buộc custom lại ListView để hiện thì được danh sách các đối tượng có phổ biến thuộc tính hơn. người dùng hãy làm theo các bước dưới đây.

Bước 1. Khởi tạo một 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 sở hữu tên là Student sở hữu những thuộc tính ID, Name, Phone sau ấy 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 1 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 1 adapter sở hữu 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 phải hiển thị ở vị trí thiết bị position
Student item = listData.get(position);
// Khai báo những 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 đã mang Adapter chúng ta vào trong file MainActivity.java và viết code như sau.

43

Bước 6. Sau khi đã 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.

42

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 một listview theo ý muốn. giả dụ gặp khó khắn trong giai đoạn code, khách hàng với thể tham khảo khóa học Android tại đây để mang thể trở nên 1 lập trình Android giỏi. Chúc quý khách thành công!

Gợi ý xem thêm:


EmoticonEmoticon