ภาพรวม
ตัวอย่างนี้สาธิตวิธีการจัดการข้อมูลด้วย HashMap และแสดงผลใน ListView ผ่าน ArrayList พร้อมฟังก์ชัน เพิ่ม แก้ไข ลบ และรีเฟรช ข้อมูล
ขั้นตอนที่ 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/record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="record number"
android:inputType="number" />
<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" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
ในโค้ดข้างต้น เรามี:
EditTextสำหรับกรอก "record number" (เป็นคีย์) และ "Name" (เป็นค่า)- ปุ่ม Save - บันทึกข้อมูลลง HashMap
- ปุ่ม Refresh - รีเฟรชการแสดงผล ListView
- ปุ่ม Delete - ลบข้อมูลตาม record number
ListView- แสดงรายการข้อมูล
ขั้นตอนที่ 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.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
Button save, refresh;
EditText name, record;
ArrayAdapter<String> arrayAdapter;
ArrayList<String> arrayList;
Map<String, String> map = new HashMap<>();
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);
record = findViewById(R.id.record);
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();
}
}
});
// ปุ่ม Delete - ลบข้อมูลตาม record number
findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String recordKey = record.getText().toString().trim();
if (!recordKey.isEmpty()) {
// ลบจาก HashMap
map.remove(recordKey);
// อัปเดต ArrayList และ Adapter
updateListView();
Toast.makeText(MainActivity.this, "Deleted", Toast.LENGTH_LONG).show();
} else {
record.setError("Enter Record Number");
}
}
});
// ปุ่ม Save - เพิ่ม/แก้ไขข้อมูล
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String nameText = name.getText().toString().trim();
String recordText = record.getText().toString().trim();
if (!nameText.isEmpty() && !recordText.isEmpty()) {
// เพิ่ม/อัปเดตใน HashMap
map.put(recordText, nameText);
// อัปเดต ListView
updateListView();
Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
} else {
if (nameText.isEmpty()) name.setError("Enter NAME");
if (recordText.isEmpty()) record.setError("Enter Record");
}
}
});
}
// เมธอดช่วยสำหรับอัปเดต ListView
private void updateListView() {
arrayList.clear();
arrayList.addAll(map.values());
arrayAdapter = new ArrayAdapter<>(
MainActivity.this,
android.R.layout.simple_list_item_1,
arrayList
);
listView.setAdapter(arrayAdapter);
}
}
คำอธิบายสำคัญ:
- HashMap - เก็บข้อมูลแบบ Key-Value (record number → name) ไม่อนุรักษ์ลำดับ
- ArrayList - ใช้เก็บค่า (values) จาก HashMap เพื่อส่งให้ ArrayAdapter
- ArrayAdapter - เชื่อมต่อข้อมูลกับ ListView
- เมธอด
updateListView()- ลดโค้ดซ้ำโดยรวม邏輯การอัปเดต Adapter
การปรับปรุงจากโค้ดต้นฉบับ:
• ลบ import ที่ไม่ใช้งาน (
• เพิ่ม Generic Types ให้ Map และ ArrayAdapter เพื่อความปลอดภัยของประเภทข้อมูล
• สร้างเมธอด
• เพิ่มการตรวจสอบ null และ trim() สำหรับ input
• ลบ import ที่ไม่ใช้งาน (
ArrayDeque, Deque, PriorityQueue, Collection)• เพิ่ม Generic Types ให้ Map และ ArrayAdapter เพื่อความปลอดภัยของประเภทข้อมูล
• สร้างเมธอด
updateListView() แยกต่างหากเพื่อลดการซ้ำของโค้ด• เพิ่มการตรวจสอบ null และ trim() สำหรับ input
การทดสอบแอปพลิเคชัน
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ

ภาพ: เพิ่มข้อมูล 3 รายการแล้วแสดงใน ListView

ภาพ: ลบรายการที่ record number = 3 แล้ว ListView อัปเดตอัตโนมัติ