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

Cách thêm dữ liệu từ PriorityQueue vào ArrayList cho ListView Android

Giới thiệu

PriorityQueue tự động sắp xếp phần tử theo thứ tự tự nhiên. Ví dụ này chuyển dữ liệu từ PriorityQueue sang ArrayList để hiển thị lên ListView đã sắp xếp.

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.ArrayList;
import java.util.PriorityQueue;

public class MainActivity extends AppCompatActivity {
    EditText name;
    ListView listView;
    ArrayList array_list = new ArrayList<>();
    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, array_list);
        listView.setAdapter(arrayAdapter);

        findViewById(R.id.refresh).setOnClickListener(v -> {
            arrayAdapter.notifyDataSetChanged();
            listView.invalidateViews();
        });

        findViewById(R.id.save).setOnClickListener(v -> {
            String input = name.getText().toString();
            if (!input.isEmpty()) {
                array_list.add(input);

                // Chuyển sang PriorityQueue để sắp xếp, rồi đưa lại ArrayList
                PriorityQueue pq = new PriorityQueue<>();
                pq.addAll(array_list);
                array_list.clear();
                array_list.addAll(pq);

                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(this, "Đã thêm & sắp xếp", Toast.LENGTH_LONG).show();
            } else {
                name.setError("Nhập tên");
            }
        });
    }
}

Chạy ứng dụng

Nhập các tên, mỗi lần lưu danh sách tự động sắp xếp theo thứ tự alphabet.

Cách thêm dữ liệu từ PriorityQueue vào ArrayList cho ListView Android