Trong lập trình Android, Adapter là một lớp trung gian có vai trò kết nối giữa dữ liệu và giao diện người dùng. Adapter lấy dữ liệu từ một nguồn dữ liệu (như mảng, danh sách, cơ sở dữ liệu, v.v.) và hiển thị chúng trên các thành phần giao diện người dùng như ListView, GridView, RecyclerView, v.v. Bằng cách sử dụng Adapter, bạn có thể làm cho dữ liệu động hiển thị trên các thành phần UI có cấu trúc danh sách hoặc lưới.
Adapter đóng vai trò như một “cầu nối” giữa View (giao diện người dùng) và Model (dữ liệu), giúp bạn hiển thị dữ liệu một cách hiệu quả và linh hoạt.
Adapter có ba nhiệm vụ chính:
View
(thành phần giao diện) tương ứng với từng mục dữ liệu, sau đó gán các giá trị dữ liệu vào các View
này trước khi hiển thị chúng lên giao diện.View
thông qua cơ chế View recycling. Điều này đặc biệt hữu ích khi làm việc với danh sách lớn.Android cung cấp một số loại Adapter phổ biến mà bạn có thể sử dụng tùy theo nhu cầu của mình. Dưới đây là các loại adapter thường gặp:
ArrayAdapter
ArrayAdapter là loại adapter cơ bản nhất, thường được sử dụng khi dữ liệu được lưu trữ dưới dạng một mảng hoặc danh sách đơn giản. Adapter này lấy dữ liệu từ mảng và gán chúng vào các TextView
bên trong danh sách.
Khi nào sử dụng?
Ví dụ:
// Dữ liệu là một mảng chuỗi String[] fruits = {"Apple", "Banana", "Cherry", "Date"}; // Tạo ArrayAdapter và gán nó cho ListView ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, fruits); ListView listView = findViewById(R.id.listView); listView.setAdapter(adapter);
BaseAdapter
BaseAdapter
là lớp cha của tất cả các adapter trong Android. Nó cung cấp các phương thức trừu tượng mà bạn cần phải ghi đè để định nghĩa cách adapter của bạn hoạt động. Bạn thường sử dụng BaseAdapter
khi cần tạo adapter tùy chỉnh để hiển thị dữ liệu phức tạp hơn, chẳng hạn như một danh sách các đối tượng tùy chỉnh.
Khi nào sử dụng?
Ví dụ:
public class CustomAdapter extends BaseAdapter { private Context context; private List<Item> itemList; public CustomAdapter(Context context, List<Item> itemList) { this.context = context; this.itemList = itemList; } @Override public int getCount() { return itemList.size(); } @Override public Object getItem(int position) { return itemList.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { LayoutInflater inflater = LayoutInflater.from(context); convertView = inflater.inflate(R.layout.list_item, parent, false); } // Lấy item hiện tại Item currentItem = itemList.get(position); // Thiết lập các giá trị cho view TextView textView = convertView.findViewById(R.id.textView); textView.setText(currentItem.getName()); return convertView; } }
RecyclerView.Adapter
RecyclerView.Adapter
là adapter mạnh mẽ và linh hoạt nhất, được sử dụng với RecyclerView – một widget hiện đại thay thế cho ListView
và GridView
. RecyclerView.Adapter
yêu cầu bạn cài đặt một lớp con và tùy chỉnh cách hiển thị và xử lý các mục dữ liệu.
Khi nào sử dụng?
Ví dụ:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<MyRecyclerViewAdapter.ViewHolder> { private List<String> data; public MyRecyclerViewAdapter(List<String> data) { this.data = data; } @NonNull @Override public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false); return new ViewHolder(view); } @Override public void onBindViewHolder(@NonNull ViewHolder holder, int position) { holder.textView.setText(data.get(position)); } @Override public int getItemCount() { return data.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { TextView textView; public ViewHolder(View itemView) { super(itemView); textView = itemView.findViewById(R.id.textView); } } }
SimpleCursorAdapter
SimpleCursorAdapter
được sử dụng khi dữ liệu của bạn nằm trong một Cursor, tức là khi bạn đang lấy dữ liệu từ một cơ sở dữ liệu SQL hoặc một nguồn dữ liệu khác sử dụng Cursor
.
Khi nào sử dụng?
Ví dụ:
Cursor cursor = database.query("my_table", null, null, null, null, null, null); String[] fromColumns = {"column_name"}; int[] toViews = {R.id.textView}; // Tạo SimpleCursorAdapter SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, fromColumns, toViews, 0); ListView listView = findViewById(R.id.listView); listView.setAdapter(adapter);
Adapter là một phần quan trọng của Android trong việc kết nối dữ liệu với giao diện người dùng, giúp bạn hiển thị dữ liệu động dưới dạng danh sách, lưới hoặc các cấu trúc phức tạp hơn.
TextView
.RecyclerView
cho các danh sách lớn, hiệu suất cao, cần tái sử dụng và tùy chỉnh sâu hơn.Cursor
và dữ liệu từ cơ sở dữ liệu.Việc lựa chọn đúng loại Adapter sẽ giúp ứng dụng của bạn chạy mượt mà, hiệu quả hơn, đồng thời tối ưu hóa hiệu suất khi hiển thị dữ liệu lớn.