การใช้ Synchronized List สำหรับ ListView ใน Android
ตัวอย่างนี้สาธิตการใช้ Collections.synchronizedList() เพื่อทำให้ List 안전한สำหรับการเข้าถึงจากหลาย Thread (Thread-safe) พร้อมการตรวจสอบข้อมูลซ้ำ
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
Android Studio → File → New Project
ขั้นตอนที่ 2: Layout XML (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"
android:gravity="center_horizontal"
android:layout_marginTop="30dp"
tools:context=".MainActivity">
<EditText
android:id="@+id/edit_query"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buttonPanel"
android:text="Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ListView
android:id="@+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
Layout ประกอบด้วย EditText สำหรับกรอกข้อมูล Button สำหรับตรวจสอบ และ ListView สำหรับแสดงรายการ
ขั้นตอนที่ 3: โค้ด Java (src/MainActivity.java)
package com.example.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.Arrays;
import java.util.Collections;
public class MainActivity extends AppCompatActivity {
ListView list;
String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry",
"WebOS", "Ubuntu", "Windows7", "Max OS X"};
EditText edit_query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_query = findViewById(R.id.edit_query);
list = findViewById(R.id.list);
// สร้าง ArrayList จากอาร์เรย์
final ArrayList<String> arrayList = new ArrayList<>(Arrays.asList(mobileArray));
// สร้าง Synchronized List (Thread-safe)
final ArrayAdapter adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, Collections.synchronizedList(arrayList));
list.setAdapter(adapter);
findViewById(R.id.buttonPanel).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String input = edit_query.getText().toString().trim();
if (!input.isEmpty()) {
// ตรวจสอบข้อมูลซ้ำ (ต้อง synchronized ด้วย)
synchronized (arrayList) {
if (arrayList.contains(input)) {
Toast.makeText(MainActivity.this, "Already it is in list", Toast.LENGTH_LONG).show();
} else {
arrayList.add(input);
adapter.notifyDataSetChanged(); // อัปเดต ListView
}
}
}
}
});
}
}
จุดสำคัญที่ปรับปรุงจากโค้ดต้นฉบับ
- ใช้
synchronized (arrayList)เมื่อตรวจสอบและเพิ่มข้อมูล เพื่อความปลอดภัย Thread - ใช้
adapter.notifyDataSetChanged()แทนการสร้าง Adapter ใหม่ทุกครั้ง - เพิ่ม
.trim()เพื่อตัดช่องว่างหน้า/หลัง
การทดสอบ
รันแอป กรอกชื่อมือถือใหม่กด Check จะเพิ่มในรายการ กรอกซ้ำจะแจ้ง Toast ว่า "Already it is in list"

ข้อควรระวัง
Collections.synchronizedList() ทำให้เมธอดแต่ละอันเป็น Thread-safe แต่ การวนลูป (iteration) ต้อง synchronized เอง มิฉะนั้นอาจเกิด ConcurrentModificationException