Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách thêm phần tử vào LinkedList cho ListView Android

Giới thiệu

LinkedList triển khai List và Deque, thao tác thêm/xóa đầu/cuối O(1). Ví dụ này dùng LinkedList làm nguồn dữ liệu cho ListView.

Bước 1: Tạo project mới

Android Studio → File → New Project.

Bước 2: Giao diện (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText android:id="@+id/name" android:layout_width="match_parent"
        android:hint="Nhập tên" android:layout_height="wrap_content" />

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
        <Button android:id="@+id/save" android:text="Lưu"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <Button android:id="@+id/refresh" android:text="Làm mới"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
    </LinearLayout>

    <ListView android:id="@+id/listView" android:layout_width="match_parent"
        android:layout_height="wrap_content">
    </ListView>
</LinearLayout>

Bước 3: MainActivity.java

package com.example.andy.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.LinkedList;

public class MainActivity extends AppCompatActivity {
    EditText name;
    ListView listView;
    LinkedList link_list = new LinkedList<>();
    ArrayAdapter arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        listView = findViewById(R.id.listView);
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, link_list);
        listView.setAdapter(arrayAdapter);

        findViewById(R.id.refresh).setOnClickListener(v -> arrayAdapter.notifyDataSetChanged());

        findViewById(R.id.save).setOnClickListener(v -> {
            String input = name.getText().toString();
            if (!input.isEmpty()) {
                link_list.add(input); // Thêm vào cuối
                // Hoặc: link_list.addFirst(input) / addLast(input)
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(this, "Đã thêm vào LinkedList", Toast.LENGTH_LONG).show();
            } else {
                name.setError("Nhập tên");
            }
        });
    }
}

Đặc điểm LinkedList

  • Thêm/xóa đầu/cuối: O(1) - nhanh hơn ArrayList.
  • Truy cập theo index: O(n) - chậm hơn ArrayList.
  • Implements Deque: hỗ trợ queue/stack (push, pop, poll...).

Chạy ứng dụng

Nhập tên, nhấn "Lưu" -> hiển thị lên ListView theo thứ tự thêm.

Cách thêm phần tử vào LinkedList cho ListView Android