Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

एंड्रॉइड में एक्टिविटीज के बीच बड़ा डेटा कैसे पास करें?

Serializable का उपयोग करके बड़ी डेटा सूची पास करना

जब बड़ी मात्रा में डेटा (जैसे ऑब्जेक्ट्स की सूची) एक एक्टिविटी से दूसरी में पास करनी हो, तो Serializable इंटरफेस का उपयोग करके Bundle के माध्यम से भेजा जा सकता है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio में File → New Project चुनें।

चरण 2: लेआउट (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"
    android:layout_margin="16dp"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/etName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="नाम"
        android:inputType="textCapWords" />

    <EditText
        android:id="@+id/etAge"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="उम्र"
        android:inputType="number" />

    <EditText
        android:id="@+id/etCity"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:hint="शहर"
        android:inputType="textCapWords" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginTop="16dp"
        android:text="अगला" />
</LinearLayout>

चरण 3: मॉडल क्लास (src/User.java)

package com.example.myapplication;

import java.io.Serializable;

public class User implements Serializable {
    private String city;
    private String name;
    private String age;

    public User(String name, String age, String city) {
        this.name = name;
        this.age = age;
        this.city = city;
    }

    // Getters और Setters
    public String getCity() { return city; }
    public void setCity(String city) { this.city = city; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public String getAge() { return age; }
    public void setAge(String age) { this.age = age; }
}

चरण 4: MainActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.ArrayList;

public class MainActivity extends AppCompatActivity {
    EditText etName, etAge, etCity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        etName = findViewById(R.id.etName);
        etAge = findViewById(R.id.etAge);
        etCity = findViewById(R.id.etCity);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(v -> {
            String name = etName.getText().toString().trim();
            String age = etAge.getText().toString().trim();
            String city = etCity.getText().toString().trim();

            ArrayList<User> userList = new ArrayList<>();
            userList.add(new User(name, age, city));

            Bundle bundle = new Bundle();
            bundle.putSerializable("myUser", userList);

            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            intent.putExtras(bundle);
            startActivity(intent);
        });
    }
}

चरण 5: सेकंड एक्टिविटी लेआउट (res/layout/activity_second.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:layout_margin="16dp"
    android:orientation="vertical"
    tools:context=".SecondActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

चरण 6: SecondActivity.java

package com.example.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
import java.util.ArrayList;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            ArrayList<User> userList = (ArrayList<User>) bundle.getSerializable("myUser");
            if (userList != null && !userList.isEmpty()) {
                User user = userList.get(0);
                String display = "नाम: " + user.getName() + "\nउम्र: " + user.getAge() + "\nशहर: " + user.getCity();
                TextView tvName = findViewById(R.id.textView);
                tvName.setText(display);
            }
        }
    }
}

चरण 7: AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.myapplication">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".SecondActivity"></activity>
    </application>
</manifest>

महत्वपूर्ण नोट्स

  • Serializable इंटरफेस लागू करना आवश्यक है।
  • बड़े डेटा के लिए Parcelable अधिक कुशल है (परफॉर्मेंस के लिए बेहतर)।
  • Bundle की साइज लिमिट (~1MB) ध्यान में रखें। बहुत बड़े डेटा के लिए ViewModel, Room डेटाबेस, या SharedPreferences का उपयोग करें।
  1. Android में एक्टिविटी के बीच स्विच कैसे करें? Android में एक्टिविटी के बीच स्विच कैसे करें?

    एक्टिविटी के बीच नेविगेशन यह उदाहरण दर्शाता है कि Intent का उपयोग करके दो एक्टिविटी के बीच आगे-पीछे कैसे नेविगेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:an

  1. Android में मोबाइल डेटा कैसे डिसेबल करें? Android में मोबाइल डेटा कैसे डिसेबल करें?

    प्रोग्रामेटिक रूप से मोबाइल डेटा चालू/बंद करने का प्रयास - लेकिन गैर-रूटेड डिवाइस पर प्रतिबंधों के कारण यह काम नहीं करता। ⚠️ महत्वपूर्ण सीमाएं Android 5.0+ (API 21+): setMobileDataEnabled() रिफ्लेक्शन के माध्यम से भी कॉल नहीं किया जा सकता Android 2.1-4.4: रिफ्लेक्शन के माध्यम से काम करता था (रूटेड

  1. Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण

    डेटा बाइंडिंग एक तकनीक है जिसका उपयोग तब किया जाता है जब आप सूचना के टुकड़ों (आपके डेटा) को कुछ विज़ुअल उपयोगकर्ता इनपुट तत्वों से जोड़ना चाहते हैं। इस प्रक्रिया में, जब भी इनपुट अपडेट होता है, तो उसके पीछे का डेटा भी अपडेट हो जाता है। यह कोई नई अवधारणा नहीं है, और ऐसे ढेर सारे फ्रेमवर्क हैं जिन्हें