Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Android

วิธีใช้ LinkedHashSet ใน Android

ภาพรวม

ตัวอย่างนี้สาธิตวิธีการใช้ LinkedHashSet ร่วมกับ ArrayList และ ListView ใน Android พร้อมฟังก์ชัน เพิ่ม และรีเฟรช ข้อมูล (ไม่มีฟังก์ชันลบ) โดย LinkedHashSet จะรักษา ลำดับการเพิ่มข้อมูล (Insertion Order) และ ไม่ซ้ำกัน (Unique)

ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่

เปิด Android Studio ไปที่ File → New Project กรอกข้อมูลที่จำเป็นทั้งหมดเพื่อสร้างโปรเจกต์ใหม่

ขั้นตอนที่ 2: จัดทำ Layout (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:layout_height="wrap_content"
        android:hint="Enter Name" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <Button
            android:id="@+id/save"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Save" />

        <Button
            android:id="@+id/refresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Refresh" />

    </LinearLayout>

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

</LinearLayout>

หมายเหตุ: โค้ด XML ต้นฉบับมี Syntax Error (xmlns:android ซ้ำ, layout_width ผิดที่) - ได้แก้ไขแล้วในเวอร์ชันนี้

ขั้นตอนที่ 3: เขียนโค้ดหลัก (src/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.LinkedHashSet;
import java.util.Set;

public class MainActivity extends AppCompatActivity {

    EditText name;
    ArrayAdapter<String> arrayAdapter;
    ArrayList<String> arrayList;
    Set<String> set = new LinkedHashSet<>();
    private ListView listView;

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

        arrayList = new ArrayList<>();
        name = findViewById(R.id.name);
        listView = findViewById(R.id.listView);

        // ปุ่ม Refresh - อัปเดตการแสดงผล
        findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (arrayAdapter != null) {
                    arrayAdapter.notifyDataSetChanged();
                    listView.invalidateViews();
                    listView.refreshDrawableState();
                }
            }
        });

        // ปุ่ม Save - เพิ่มข้อมูล
        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String nameText = name.getText().toString().trim();
                if (!nameText.isEmpty()) {
                    // เพิ่มใน LinkedHashSet (ไม่ซ้ำ รักษาลำดับการเพิ่ม)
                    set.add(nameText);
                    
                    // อัปเดต ListView
                    updateListView();
                    
                    Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
                } else {
                    name.setError("Enter NAME");
                }
            }
        });
    }

    // เมธอดช่วยอัปเดต ListView
    private void updateListView() {
        arrayList.clear();
        arrayList.addAll(set);
        
        arrayAdapter = new ArrayAdapter<>(
            MainActivity.this,
            android.R.layout.simple_list_item_1,
            arrayList
        );
        listView.setAdapter(arrayAdapter);
    }
}

คำอธิบายสำคัญ:

  • LinkedHashSet - ชุดข้อมูลที่ ไม่ซ้ำกัน (Unique) และ รักษาลำดับการเพิ่ม (Insertion Order)
  • ต่างจาก HashSet (ไม่รักษาลำดับ) และ TreeSet (จัดเรียงอัตโนมัติ)
  • เหมาะสำหรับ: รายการที่ต้องการไม่ซ้ำ แต่ต้องการลำดับตามที่ผู้ใช้เพิ่ม (เช่น Recently Used, History)
  • ไม่มีปุ่ม Delete ในตัวอย่างนี้
💡 กรณีใช้งานจริงของ LinkedHashSet:
• Recent Searches - ค้นหาล่าสุด (ไม่ซ้ำ, เรียงตามเวลา)
• Play History - ประวัติเล่นเพลง/วิดีโอ
• Visited Pages - หน้าที่เคยเข้า (ไม่ซ้ำ, เรียงล่าสุด)
• Tag/Category Input - ป้อนแท็กไม่ซ้ำ รักษาลำดับที่เลือก
การปรับปรุงจากโค้ดต้นฉบับ:
• แก้ไข XML Layout ให้ถูกต้อง (xmlns:android ซ้ำ, layout_width ผิด)
• เพิ่ม Generic Types: Set<String>, LinkedHashSet<>, ArrayAdapter<String>
• สร้างเมธอด updateListView() แยกต่างหาก
• ใช้ trim() และตรวจสอบ empty อย่างถูกต้อง
• แก้ไขคำอธิบาย Layout (ต้นฉบับกล่าวถึง record number แต่ไม่มีใน Layout)

การทดสอบแอปพลิเคชัน

เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ

วิธีใช้ LinkedHashSet ใน Android

ภาพ: เพิ่มชื่อจาก LinkedHashSet ไปยัง ArrayList แสดงใน ListView (เรียงตามลำดับที่เพิ่ม)