ภาพรวม
ตัวอย่างนี้สาธิตวิธีการ เพิ่ม (append) ข้อมูลจาก HashMap ไปยัง ArrayList เพื่อแสดงใน ListView พร้อมฟังก์ชันบันทึกและรีเฟรชข้อมูล
ขั้นตอนที่ 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" />
<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>
ในโค้ดข้างต้น เรามีช่องกรอก "record number" และ "Name" พร้อมปุ่ม Save และ Refresh (ไม่มีปุ่ม Delete ต่างจากตัวอย่างก่อนหน้า)
ขั้นตอนที่ 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();
}
}
});
// ปุ่ม Save - เพิ่มข้อมูลลง HashMap
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 (key = record, value = name)
map.put(recordText, nameText);
// อัปเดต ArrayList และ Adapter
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 โดย Key คือ record number, Value คือ name
- map.put(key, value) - เพิ่มข้อมูลใหม่ หรืออัปเดตค่าเดิมถ้า Key ซ้ำ
- map.values() - ดึงเฉพาะค่า (values) ทั้งหมดมาเป็น Collection
- ArrayList.addAll() - เพิ่มข้อมูลทั้งหมดจาก Collection เข้า ArrayList
- ไม่มีฟังก์ชันลบข้อมูลในตัวอย่างนี้ (มีแค่เพิ่มและรีเฟรช)
• ลบ import ที่ไม่ใช้งาน (
ArrayDeque, Deque, PriorityQueue, Collection)• เพิ่ม Generic Types ให้ Map และ ArrayAdapter
• สร้างเมธอด
updateListView() แยกต่างหาก• ใช้ trim() และตรวจสอบ empty อย่างถูกต้อง
การทดสอบแอปพลิเคชัน
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity แล้วคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ


ภาพ: เพิ่มชื่อจาก HashMap ไปยัง ArrayList แล้วแสดงใน ListView