के बीच सूचना पारित करने के लिए सबसे आसान तरीकों में से एक गतिविधियों का उपयोग बंडल का उपयोग करना है।
आप नई गतिविधि लॉन्च करने से पहले एक्स्ट्रा जोड़ सकते हैं।
सबसे पहले:
दूसरे छोर पर (अन्य गतिविधि) आप तो बंडल से बाहर इन अतिरिक्त प्राप्त करते हैं और संभवतः नई गतिविधि में वस्तु का पुनर्निर्माण कर सकते हैं ..
यहाँ एक उदाहरण है सभी, यहाँ छात्र कक्षा है:
छात्र वर्ग - Student.java:
package com.stephendiniz.objectsharing;
public class Student
{
int id;
String name;
String profession;
Student()
{
id = 0;
name = "";
profession = "";
}
Student(int id, String name, String profession)
{
this.id = id;
this.name = name;
this.profession = profession;
}
public int getId() { return id; }
public String getName() { return name; }
public String getProfession() { return profession; }
}
मुख्य गतिविधि छात्र वर्ग में दर्शाएंगे एक वस्तु रों
मुख्य गतिविधि उत्पन्न करने के लिए - AndroidObjectSharing.java:
package com.stephendiniz.objectsharing;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class AndroidObjectSharingActivity extends Activity
{
TextView id;
TextView name;
TextView profession;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
id = (TextView)findViewById(R.id.id);
name = (TextView)findViewById(R.id.name);
profession = (TextView)findViewById(R.id.profession);
Button submitButton = (Button)findViewById(R.id.submitButton);
submitButton.setOnClickListener(new View.OnClickListener()
{
final Intent newActivity = new Intent(AndroidObjectSharingActivity.this, NewActivity.class);
public void onClick(View v)
{
Student s = new Student(Integer.parseInt(id.getText().toString()), name.getText().toString(), profession.getText().toString());
newActivity.putExtra("extraStudentId", s.getId());
newActivity.putExtra("extraStudentName", s.getName());
newActivity.putExtra("extraStudentProfession", s.getProfession());
startActivity(newActivity);
}
});
}
}
यहाँ एक्सएमएल छात्र कक्षा से जुड़ा हुआ है कि क्या है:
मुख्य एक्सएमएल लेआउट - main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student ID:" />
<EditText
android:id="@+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="9001" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Name:" />
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Steve" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Student Profession:" />
<EditText
android:id="@+id/profession"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Computer Engineer" />
<Button
android:id="@+id/submitButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="Send to New Activity" />
</LinearLayout>
नोट .. कि NewActivity एक बंडल (इस मामले में infoBundle नाम) से जानकारी तक पहुंचता है
वस्तु है "पुनर्निर्माण" प्रोग्राम के रूप में के रूप में अगर यह पारित किया गया था।
नई गतिविधि - NewActivity.java:
package com.stephendiniz.objectsharing;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
public class NewActivity extends Activity
{
LinearLayout ll;
TextView id;
TextView name;
TextView profession;
private final static String TAG = "NewActivity";
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle infoBundle = getIntent().getExtras();
Student s = new Student(infoBundle.getInt("extraStudentId"), infoBundle.getString("extraStudentName"), infoBundle.getString("extraStudentProfession"));
ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
id = new TextView(this);
name = new TextView(this);
profession = new TextView(this);
id.setText("Student Id: " + s.getId() + "\n");
name.setText("Student Name: " + s.getName() + "\n");
profession.setText("Student Profession: " + s.getProfession() + "\n");
ll.addView(id);
ll.addView(name);
ll.addView(profession);
Button closeButton = new Button(this);
closeButton.setText("Close Activity");
closeButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
finish();
}
});
ll.addView(closeButton);
setContentView(ll);
}
}
अपने प्रकट करने के लिए नई गतिविधि को जोड़ने के लिए मत भूलना!
एंड्रॉयड प्रकट - AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.stephendiniz.objectsharing"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidObjectSharingActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".NewActivity"
android:label="New Activity" />
</application>
</manifest>
एक्सएमएल और प्रोग्रामेटिक स्क्रिप्टिंग के मिश्रण के लिए क्षमा करें, मैं के बाद पहली गतिविधि है कि यह फ़ाइलों का एक बहुत कुछ हो सकता है तो प्रदर्शित करने के लिए मैं सघन महसूस किया अंतिम फ़ाइल, लेकिन आपको विचार प्राप्त करने में सक्षम होना चाहिए ..
आशा है कि इससे मदद मिलती है!
+1, यह मेरी पसंद का तरीका भी है। बहुत लचीला है क्योंकि आप अपने स्वयं के गेटर्स को कस्टम एप्लिकेशन क्लास में एक सेटर्स लिख सकते हैं। – Guillaume