Android में LinkedList और ListView का उपयोग करके आइटम हटाना
यह ट्यूटोरियल दिखाता है कि LinkedList डेटा स्ट्रक्चर का उपयोग करके ListView से विशिष्ट एलिमेंट्स कैसे हटाए जाते हैं। LinkedList में इन्सर्शन/डिलीशन ऑपरेशन ArrayList से तेज होते हैं।
चरण 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" />
</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.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<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);
// रिफ्रेश बटन
findViewById(R.id.refresh).setOnClickListener(v -> {
if (arrayAdapter != null) {
arrayAdapter.notifyDataSetChanged();
listView.invalidateViews();
listView.refreshDrawableState();
}
});
// डिलीट बटन - नाम के आधार पर एलिमेंट हटाता है
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 = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, link_list);
listView.setAdapter(arrayAdapter);
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();
}
});
// सेव बटन - LinkedList में जोड़ता है
findViewById(R.id.save).setOnClickListener(v -> {
String inputName = name.getText().toString().trim();
if (!inputName.isEmpty()) {
link_list.add(inputName);
arrayAdapter = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_list_item_1, link_list);
listView.setAdapter(arrayAdapter);
Toast.makeText(MainActivity.this, "इन्सर्ट किया गया", Toast.LENGTH_LONG).show();
name.setText("");
} else {
name.setError("नाम दर्ज करें");
}
});
}
}
मुख्य बिंदु:
- LinkedList बनाम ArrayList: LinkedList में
remove(Object)O(n) है लेकिन बार-बार इन्सर्शन/डिलीशन के लिए बेहतर है। - रिटर्न वैल्यू चेक:
remove()बूलियन रिटर्न करता है - सफल डिलीशन कन्फर्म करने के लिए इसका उपयोग करें। - एडैप्टर रीसेट: डेटा बदलने के बाद नया एडैप्टर सेट करना पड़ता है (बेहतर:
notifyDataSetChanged()के साथ सिंगल एडैप्टर इंस्टेंस)।
ऐप चलाएं
डिवाइस कनेक्ट करें, Run दबाएं। नाम जोड़ने के लिए "सेव करें", सूची अपडेट करने के लिए "रिफ्रेश", और विशिष्ट नाम हटाने के लिए नाम दर्ज करके "डिलीट" दबाएं।


नतीजा: उपयोगकर्ता नाम जोड़ सकते हैं और EditText में नाम दर्ज करके "डिलीट" बटन से विशिष्ट एंट्री हटा सकते हैं।