Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Android

Cách xóa tất cả phần tử ArrayList cho ListView trong Android

Giới thiệu

Ví dụ minh họa thao tác CRUD cơ bản với ArrayList và ListView: thêm, xóa một phần tử, xóa tất cả.

Bước 1: Tạo project mới

Android Studio → File → New Project.

Bước 2: Giao diện (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/name" android:layout_width="match_parent"
        android:hint="Nhập tên" android:layout_height="wrap_content" />

    <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
        <Button android:id="@+id/save" android:text="Lưu"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <Button android:id="@+id/refresh" android:text="Làm mới"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <Button android:id="@+id/delete" android:text="Xóa"
            android:layout_width="wrap_content" android:layout_height="wrap_content" />
        <Button android:id="@+id/deleteall" android:text="Xóa tất cả"
            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>

Bước 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.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    EditText name;
    ListView listView;
    ArrayList array_list = new ArrayList<>();
    ArrayAdapter arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        name = findViewById(R.id.name);
        listView = findViewById(R.id.listView);
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, array_list);
        listView.setAdapter(arrayAdapter);

        // Làm mới
        findViewById(R.id.refresh).setOnClickListener(v -> {
            arrayAdapter.notifyDataSetChanged();
            listView.invalidateViews();
        });

        // Xóa tất cả
        findViewById(R.id.deleteall).setOnClickListener(v -> {
            if (array_list.isEmpty()) {
                Toast.makeText(this, "Không có phần tử để xóa", Toast.LENGTH_LONG).show();
            } else {
                array_list.clear();
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(this, "Đã xóa tất cả", Toast.LENGTH_LONG).show();
            }
        });

        // Xóa một phần tử theo tên
        findViewById(R.id.delete).setOnClickListener(v -> {
            String input = name.getText().toString();
            if (array_list.isEmpty()) {
                Toast.makeText(this, "Không có phần tử để xóa", Toast.LENGTH_LONG).show();
            } else if (!input.isEmpty() && array_list.remove(input)) {
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(this, "Đã xóa: " + input, Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(this, "Không tìm thấy: " + input, Toast.LENGTH_LONG).show();
            }
        });

        // Thêm
        findViewById(R.id.save).setOnClickListener(v -> {
            String input = name.getText().toString();
            if (!input.isEmpty()) {
                array_list.add(input);
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(this, "Đã thêm", Toast.LENGTH_LONG).show();
            } else {
                name.setError("Nhập tên");
            }
        });
    }
}

Chạy ứng dụng

Nhập tên, nhấn "Lưu" để thêm vào ListView. Nhấn "Xóa tất cả" để clear ArrayList.

Cách xóa tất cả phần tử ArrayList cho ListView trong Android

Cách xóa tất cả phần tử ArrayList cho ListView trong Android