HashMap से डेटा जोड़कर ListView में प्रदर्शित करना (Append)
यह उदाहरण दर्शाता है कि HashMap में डेटा जोड़कर (append) 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="रिकॉर्ड नंबर" />
<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="रिफ्रेश" />
</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.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();
}
}
});
// सेव बटन - डेटा जोड़ता है (append)
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); // HashMap में जोड़ें/अपडेट करें
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()); // HashMap के values को ArrayList में कॉपी करें
if (arrayAdapter == null) {
arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
} else {
arrayAdapter.notifyDataSetChanged();
}
}
}
मुख्य अंतर
| विशेषता | पिछला उदाहरण (1047) | यह उदाहरण (1049) |
|---|---|---|
| डिलीट बटन | हाँ | नहीं |
| ऑपरेशन | जोड़ें + हटाएं | केवल जोड़ें (append) |
| उपयोग | पूर्ण CRUD | केवल डेटा जोड़ना |
ऐप चलाएं
नाम और रिकॉर्ड नंबर डालकर सेव करें, रिफ्रेश दबाकर ListView अपडेट देखें।
