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

Android में Intent का उपयोग करके ArrayList को दूसरी एक्टिविटी में कैसे पास करें?

Android में Intent के माध्यम से ArrayList पास करने का गाइड

यह ट्यूटोरियल दिखाता है कि कैसे एक ArrayList को Intent के extra के रूप में दूसरी एक्टिविटी में पास किया जाए।

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

Android Studio में File → New Project चुनकर नया प्रोजेक्ट बनाएं।

चरण 2: पहली एक्टिविटी का लेआउट

res/layout/activity_main.xml में:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/relativeLayout"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Pass array to 2nd Activity"
        android:layout_centerInParent="true" />
</RelativeLayout>

चरण 3: MainActivity.java में कोड जोड़ें

src/MainActivity.java में:

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

public class MainActivity extends AppCompatActivity {
    Button button;
    ArrayList<String> numbers = new ArrayList<>();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                numbers.add("ONE");
                numbers.add("TWO");
                numbers.add("THREE");
                numbers.add("FOUR");
                numbers.add("FIVE");
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                intent.putExtra("key", numbers);
                startActivity(intent);
            }
        });
    }
}

चरण 4: दूसरी एक्टिविटी बनाएं (SecondActivity.java)

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

public class SecondActivity extends AppCompatActivity {
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        textView = findViewById(R.id.textView);
        ArrayList<String> numbersList = (ArrayList<String>) getIntent().getSerializableExtra("key");
        textView.setText(String.valueOf(numbersList));
    }
}

चरण 5: दूसरी एक्टिविटी का लेआउट (activity_second.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".SecondActivity">
    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text=""
        android:textSize="24sp"
        android:textStyle="bold"/>
</RelativeLayout>

चरण 6: AndroidManifest.xml में दोनों एक्टिविटी घोषित करें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="app.com.sample">
    <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=".SecondActivity"></activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

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

ArrayList को Intent में पास करने के लिए यह Serializable इंटरफेस लागू करता है। कस्टम ऑब्जेक्ट्स के लिए, आपकी मॉडल क्लास को Serializable या Parcelable (अधिक कुशल) लागू करना चाहिए।

ऐप चलाएं

डिवाइस कनेक्ट करें, Run आइकन पर क्लिक करें, बटन दबाएं और दूसरी एक्टिविटी में ArrayList डेटा देखें।

  1. एंड्रॉइड में इंटेंट का उपयोग करके एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट कैसे भेजें? एंड्रॉइड में इंटेंट का उपयोग करके एक एक्टिविटी से दूसरी एक्टिविटी में ऑब्जेक्ट कैसे भेजें?

    Serializable के साथ एक्टिविटीज के बीच ऑब्जेक्ट पास करना यह उदाहरण दर्शाता है कि Serializable इंटरफेस को लागू करके कस्टम ऑब्जेक्ट को इंटेंट के माध्यम से एक एक्टिविटी से दूसरी एक्टिविटी में कैसे भेजें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मेन लेआउट में EditT

  1. एंड्रॉइड में एक्टिविटी से फ्रैगमेंट में वेरिएबल कैसे पास करें? एंड्रॉइड में एक्टिविटी से फ्रैगमेंट में वेरिएबल कैसे पास करें?

    Bundle और setArguments के साथ फ्रैगमेंट को डेटा पास करें यह उदाहरण दर्शाता है कि Bundle का उपयोग करके एक्टिविटी से फ्रैगमेंट में स्ट्रिंग डेटा कैसे पास करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: लेआउट में कंटेनर और इनपुट res/layout/activity_main.xml: <?

  1. एंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी में इमेज कैसे पास करें एंड्रॉइड में एक एक्टिविटी से दूसरी एक्टिविटी में इमेज कैसे पास करें

    Intent के माध्यम से इमेज रिसोर्स ID या बिटमैप पास करना एंड्रॉइड में एक एक्टिविटी से दूसरी में इमेज पास करने के दो मुख्य तरीके हैं: रिसोर्स ID (इंट/ड्रॉएबल रेफरेंस) पास करना (कुशल, केवल ऐप-इंटरनल इमेज के लिए) या बिटमैप/बाइट ऐरे पास करना (किसी भी इमेज के लिए, लेकिन मेमोरी-इंटेंसिव)। चरण 1: नया प्रोजे