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

วิธีใช้คลาส Vector ใน ListView บน Android

ตัวอย่าง: การใช้คลาส Vector ใน ListView บน Android

ตัวอย่างนี้สาธิตวิธีใช้คลาส Vector แทน ArrayList สำหรับเก็บข้อมูลใน ListView

ขั้นตอนที่ 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 เพื่อเก็บข้อมูลใน Vector กด Refresh เพื่อแสดงการเปลี่ยนแปลงของ 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.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.Vector;

public class MainActivity extends AppCompatActivity {
    EditText name;
    ArrayAdapter vectorAdapter;
    Vector<String> vector_list;
    private ListView listView;

    @Override
    protected void onCreate(Bundle readdInstanceState) {
        super.onCreate(readdInstanceState);
        setContentView(R.layout.activity_main);
        vector_list = new Vector<String>();
        name = findViewById(R.id.name);
        listView = findViewById(R.id.listView);

        findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                vectorAdapter.notifyDataSetChanged();
                listView.invalidateViews();
                listView.refreshDrawableState();
            }
        });

        findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!name.getText().toString().isEmpty()) {
                    vector_list.add(name.getText().toString());
                    vectorAdapter = new ArrayAdapter(MainActivity.this,
                            android.R.layout.simple_list_item_1, vector_list);
                    listView.setAdapter(vectorAdapter);
                    Toast.makeText(MainActivity.this, "Inserted", Toast.LENGTH_LONG).show();
                } else {
                    name.setError("Enter NAME");
                }
            }
        });
    }
}

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

รันแอปพลิเคชันจาก Android Studio เลือกอุปกรณ์มือถือ แล้วตรวจสอบหน้าจอเริ่มต้น

วิธีใช้คลาส Vector ใน ListView บน Android

ในผลลัพธ์ข้างต้น เราเพิ่มชื่อของสมาชิก Vector เข้า Adapter

สรุป

Vector เป็นคลาสเก่าใน Java ที่คล้าย ArrayList แต่มีความแตกต่างคือ Thread-safe (ปลอดภัยต่อเธรด) เนื่องจากเมธอดต่างๆ ถูก synchronized ทำให้เหมาะสำหรับการใช้งานในสภาพแวดล้อมหลายเธรด (Multi-threaded) อย่างไรก็ตาม ใน Android แอปส่วนใหญ่ทำงานบน Main Thread เดียว การใช้ ArrayList จะมีประสิทธิภาพดีกว่าและเป็นมาตรฐานในปัจจุบัน Vector เหมาะสำหรับกรณีที่มีการเข้าถึงข้อมูลพร้อมกันจากหลายเธรด เช่น การดาวน์โหลดข้อมูลพร้อมแสดงผลใน ListView