HashMap से डेटा हटाकर ListView में प्रदर्शित करना
यह उदाहरण दर्शाता है कि HashMap में स्टोर डेटा को ArrayList में बदलकर ListView में कैसे दिखाएं, और रिकॉर्ड नंबर द्वारा डेटा कैसे हटाएं।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में नया प्रोजेक्ट बनाएं।
चरण 2: 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/record"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="रिकॉर्ड नंबर"
android:inputType="number" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="नाम दर्ज करें" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="सेव करें" />
<Button
android:id="@+id/refresh"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="रिफ्रेश" />
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="हटाएं" />
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity.java
package com.example.myapplication;
import android.os.Bundle;
import android.support.v7.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.ArrayList;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
private EditText nameEditText, recordEditText;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> arrayList;
private Map<String, String> dataMap = new HashMap<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEditText = findViewById(R.id.name);
recordEditText = findViewById(R.id.record);
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
// रिफ्रेश बटन
findViewById(R.id.refresh).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (arrayAdapter != null) {
arrayAdapter.notifyDataSetChanged();
}
}
});
// हटाएं बटन
findViewById(R.id.delete).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String recordKey = recordEditText.getText().toString().trim();
if (!recordKey.isEmpty()) {
if (dataMap.containsKey(recordKey)) {
dataMap.remove(recordKey);
updateListView();
Toast.makeText(MainActivity.this, "हटा दिया गया", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "रिकॉर्ड नहीं मिला", Toast.LENGTH_SHORT).show();
}
} else {
recordEditText.setError("रिकॉर्ड नंबर दर्ज करें");
}
}
});
// सेव बटन
findViewById(R.id.save).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEditText.getText().toString().trim();
String record = recordEditText.getText().toString().trim();
if (!name.isEmpty() && !record.isEmpty()) {
dataMap.put(record, name);
updateListView();
Toast.makeText(MainActivity.this, "सेव हो गया", Toast.LENGTH_SHORT).show();
} else {
if (name.isEmpty()) nameEditText.setError("नाम दर्ज करें");
if (record.isEmpty()) recordEditText.setError("रिकॉर्ड नंबर दर्ज करें");
}
}
});
}
private void updateListView() {
arrayList.clear();
arrayList.addAll(dataMap.values());
if (arrayAdapter == null) {
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
} else {
arrayAdapter.notifyDataSetChanged();
}
}
}
कैसे काम करता है
- सेव करें: नाम और रिकॉर्ड नंबर डालकर सेव करें - HashMap में स्टोर होता है
- रिफ्रेश: ListView को अपडेट करता है
- हटाएं: रिकॉर्ड नंबर डालकर हटाएं बटन दबाएं - HashMap से हट जाता है
ऐप चलाएं
कई रिकॉर्ड सेव करें, फिर रिकॉर्ड नंबर डालकर हटाएं बटन दबाएं।

