Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Android में ListView के लिए LinkedList से सभी एलिमेंट्स कैसे डिलीट करें?

Android में LinkedList से सभी आइटम एक साथ क्लियर करना

यह ट्यूटोरियल दिखाता है कि LinkedList का उपयोग करके ListView के सभी एलिमेंट्स को एक क्लिक में कैसे डिलीट किया जाए clear() मेथड से।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio: File → New Project → विवरण भरें।

चरण 2: लेआउट 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"
    tools:context=".MainActivity"
    android:orientation="vertical">

    <EditText
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:hint="नाम दर्ज करें"
        android:layout_height="wrap_content" />

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/save"
            android:text="सेव करें"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/refresh"
            android:text="रिफ्रेश"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/delete"
            android:text="डिलीट"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/deleteall"
            android:text="सभी डिलीट करें"
            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>

चरण 3: MainActivity.java पूरा कोड

package com.example.andy.myapplication;

import android.os.Bundle;
import androidx.appcompat.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.LinkedList;

public class MainActivity extends AppCompatActivity {

    EditText name;
    ArrayAdapter<String> arrayAdapter;
    LinkedList<String> link_list;
    private ListView listView;

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

        link_list = new LinkedList<>();
        name = findViewById(R.id.name);
        listView = findViewById(R.id.listView);

        // एडैप्टर एक बार बनाएं
        arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, link_list);
        listView.setAdapter(arrayAdapter);

        // रिफ्रेश बटन
        findViewById(R.id.refresh).setOnClickListener(v -> 
            arrayAdapter.notifyDataSetChanged()
        );

        // सभी डिलीट करें बटन
        findViewById(R.id.deleteall).setOnClickListener(v -> {
            if (link_list.size() > 0) {
                link_list.clear();
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "सभी डिलीट किए गए", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(MainActivity.this, "डिलीट करने के लिए कोई एलिमेंट नहीं", Toast.LENGTH_LONG).show();
            }
        });

        // सिंगल डिलीट बटन
        findViewById(R.id.delete).setOnClickListener(v -> {
            String inputName = name.getText().toString().trim();
            if (link_list.size() > 0) {
                if (!inputName.isEmpty()) {
                    boolean removed = link_list.remove(inputName);
                    arrayAdapter.notifyDataSetChanged();
                    if (removed) {
                        Toast.makeText(MainActivity.this, "डिलीट किया गया", Toast.LENGTH_LONG).show();
                    } else {
                        Toast.makeText(MainActivity.this, "नाम नहीं मिला", Toast.LENGTH_LONG).show();
                    }
                } else {
                    name.setError("नाम दर्ज करें");
                }
            } else {
                Toast.makeText(MainActivity.this, "कोई एलिमेंट नहीं", Toast.LENGTH_LONG).show();
            }
        });

        // सेव बटन
        findViewById(R.id.save).setOnClickListener(v -> {
            String inputName = name.getText().toString().trim();
            if (!inputName.isEmpty()) {
                link_list.add(inputName);
                arrayAdapter.notifyDataSetChanged();
                Toast.makeText(MainActivity.this, "इन्सर्ट किया गया", Toast.LENGTH_LONG).show();
                name.setText("");
            } else {
                name.setError("नाम दर्ज करें");
            }
        });
    }
}

मुख्य सुधार (मूल कोड से):

  • सिंगल एडैप्टर इंस्टेंस: onCreate में एक बार बनाएं, हर क्लिक पर नया न बनाएं।
  • notifyDataSetChanged(): डेटा बदलने पर सिर्फ नोटिफाई करें - परफॉर्मेंस बेहतर।
  • clear() मेथड: LinkedList को खाली करने का सही तरीका।
  • लॉजिक फिक्स: मूल कोड में deleteall में गलत कंडीशन थी (!name.getText().toString().isEmpty()) - सभी डिलीट करने के लिए नाम की जरूरत नहीं होती।

ऐप चलाएं

कई नाम जोड़ें, फिर "सभी डिलीट करें" दबाकर पूरी सूची खाली करें।

Android में ListView के लिए LinkedList से सभी एलिमेंट्स कैसे डिलीट करें?

Android में ListView के लिए LinkedList से सभी एलिमेंट्स कैसे डिलीट करें?

नतीजा: "सभी डिलीट करें" बटन एक क्लिक में पूरी LinkedList और ListView खाली कर देता है।

  1. Android में LinkedList में एलिमेंट जोड़कर ListView में कैसे दिखाएं? Android में LinkedList में एलिमेंट जोड़कर ListView में कैसे दिखाएं?

    Android में LinkedList का उपयोग करके ListView में डेटा प्रदर्शित करना यह उदाहरण दर्शाता है कि Android में LinkedList में एलिमेंट जोड़कर ListView में कैसे दिखाएं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में एक नया प्रोजेक्ट बनाएं: File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक

  1. Android में ListView के लिए LinkedList से एलिमेंट कैसे डिलीट करें? Android में ListView के लिए LinkedList से एलिमेंट कैसे डिलीट करें?

    Android में LinkedList और ListView का उपयोग करके आइटम हटाना यह ट्यूटोरियल दिखाता है कि LinkedList डेटा स्ट्रक्चर का उपयोग करके ListView से विशिष्ट एलिमेंट्स कैसे हटाए जाते हैं। LinkedList में इन्सर्शन/डिलीशन ऑपरेशन ArrayList से तेज होते हैं। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New

  1. Android में ListView के लिए ArrayList से एलिमेंट कैसे डिलीट करें? Android में ListView के लिए ArrayList से एलिमेंट कैसे डिलीट करें?

    Android में ArrayList और ListView से आइटम हटाने का पूरा गाइड यह उदाहरण दर्शाता है कि ArrayList का उपयोग करके ListView में से विशिष्ट एलिमेंट्स कैसे हटाए जाते हैं। ArrayList रैंडम एक्सेस के लिए ऑप्टिमाइज्ड है। चरण 1: प्रोजेक्ट सेटअप Android Studio में नया प्रोजेक्ट बनाएं: File → New Project। चरण 2: