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()) - सभी डिलीट करने के लिए नाम की जरूरत नहीं होती।
ऐप चलाएं
कई नाम जोड़ें, फिर "सभी डिलीट करें" दबाकर पूरी सूची खाली करें।


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