TreeMap से डेटा हटाकर ListView में दिखाना (सॉर्टेड क्रम)
यह उदाहरण दर्शाता है कि TreeMap (की के आधार पर सॉर्टेड) से रिकॉर्ड नंबर द्वारा डेटा कैसे हटाएं और 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.Map;
import java.util.TreeMap;
public class MainActivity extends AppCompatActivity {
private Button saveBtn, refreshBtn, deleteBtn;
private EditText nameEditText, recordEditText;
private ListView listView;
private ArrayAdapter<String> arrayAdapter;
private ArrayList<String> arrayList;
private Map<String, String> treeMap = new TreeMap<>(); // रिकॉर्ड नंबर (की) के आधार पर सॉर्टेड
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
saveBtn = findViewById(R.id.save);
refreshBtn = findViewById(R.id.refresh);
deleteBtn = findViewById(R.id.delete);
nameEditText = findViewById(R.id.name);
recordEditText = findViewById(R.id.record);
listView = findViewById(R.id.listView);
arrayList = new ArrayList<>();
// रिफ्रेश बटन
refreshBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (arrayAdapter != null) {
arrayAdapter.notifyDataSetChanged();
}
}
});
// हटाएं बटन - रिकॉर्ड नंबर (की) द्वारा
deleteBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String recordKey = recordEditText.getText().toString().trim();
if (!recordKey.isEmpty()) {
String removedValue = treeMap.remove(recordKey);
updateListView();
if (removedValue != null) {
Toast.makeText(MainActivity.this, "रिकॉर्ड " + recordKey + " हटा दिया गया", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(MainActivity.this, "रिकॉर्ड नहीं मिला: " + recordKey, Toast.LENGTH_SHORT).show();
}
} else {
recordEditText.setError("रिकॉर्ड नंबर दर्ज करें");
}
}
});
// सेव बटन
saveBtn.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()) {
treeMap.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();
// TreeMap values() की के सॉर्टेड क्रम में लौटाता है
arrayList.addAll(treeMap.values());
if (arrayAdapter == null) {
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
} else {
arrayAdapter.notifyDataSetChanged();
}
}
}
TreeMap में डिलीट कैसे काम करता है
remove(key)- की के आधार पर एंट्री हटाता है- की स्ट्रिंग है, लेकिन "1", "2", "10" का क्रम होगा: "1", "10", "2" (लेक्सिकोग्राफिक)
- न्यूमेरिक क्रम के लिए Integer की उपयोग करें:
TreeMap<Integer, String>
बेहतर: Integer की के साथ TreeMap
// Integer की के लिए - न्यूमेरिक सॉर्टिंग Map<Integer, String> treeMap = new TreeMap<>(); // सेव करते समय: int recordNum = Integer.parseInt(recordEditText.getText().toString().trim()); treeMap.put(recordNum, name); // हटाते समय: int recordNum = Integer.parseInt(recordEditText.getText().toString().trim()); treeMap.remove(recordNum);
ऐप चलाएं
रिकॉर्ड नंबर और नाम डालकर सेव करें - ListView में रिकॉर्ड नंबर के क्रम में दिखेंगे। रिकॉर्ड नंबर डालकर हटाएं।

