ตัวอย่างนี้สาธิตวิธีการเพิ่มข้อมูล (insert element) ใน LinkedList เพื่อใช้กับ ListView ใน Android
ขั้นตอนที่ 1: สร้างโปรเจกต์ใหม่
เปิด Android Studio ไปที่ File → New Project แล้วกรอกรายละเอียดที่จำเป็นเพื่อสร้างโปรเจกต์ใหม่
ขั้นตอนที่ 2: ออกแบบหน้าจอ (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"
tools:context=".MainActivity"
android:orientation="vertical">
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:hint="Enter Name"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="@+id/save"
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/refresh"
android:text="Refresh"
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>
ในโค้ดด้านบน เรามี EditText สำหรับชื่อ เมื่อผู้ใช้กดปุ่ม Save ข้อมูลจะถูกเก็บใน LinkedList กดปุ่ม Refresh เพื่อดูการเปลี่ยนแปลงของ ListView
ขั้นตอนที่ 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.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.LinkedList;
public class MainActivity extends AppCompatActivity {
Button save, refresh;
EditText name;
ArrayAdapter arrayAdapter;
LinkedList linkedList;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linkedList = new LinkedList<>();
name = findViewById(R.id.name);
listView = findViewById(R.id.listView);
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
listView.refreshDrawableState();
}
});
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!name.getText().toString().isEmpty()) {
linkedList.add(name.getText().toString());
arrayAdapter = new ArrayAdapter(MainActivity.this,
android.R.layout.simple_list_item_1, linkedList);
listView.setAdapter(arrayAdapter);
Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
} else {
name.setError("Enter NAME");
}
}
});
}
}
การทดสอบแอปพลิเคชัน
เชื่อมต่ออุปกรณ์ Android จริงกับคอมพิวเตอร์ ใน Android Studio เปิดไฟล์ Activity และคลิกไอคอน Run จากแถบเครื่องมือ เลือกอุปกรณ์มือถือของคุณ แล้วตรวจสอบหน้าจอเริ่มต้น

ผลลัพธ์: เพิ่มชื่อเข้า LinkedList และแสดงชื่อใน ListView
ข้อดีของ LinkedList
- เพิ่ม/ลบข้อมูลที่หัวและท้ายได้เร็ว O(1)
- เหมาะสำหรับ Queue, Stack, Deque
- ใช้หน่วยความจำน้อยกว่า ArrayList เมื่อเพิ่ม/ลบบ่อย
- ไม่ต้องย้ายข้อมูล (shift elements) เมื่อเพิ่ม/ลบตรงกลาง
ข้อเสีย
- เข้าถึงแบบสุ่ม (random access) ช้ากว่า ArrayList O(n)
- ใช้หน่วยความจ้อมากกว่าเล็กน้อยต่อ element (เก็บ pointer)