2012-01-18 9 views
6

मेरे पास एक ऐसा एप्लिकेशन है जिसमें दो गतिविधियां/स्क्रीन और एक जावा क्लास शामिल है जिससे मैं ऑब्जेक्ट बना देता हूं।एंड्रॉइड प्रोग्राम में सभी गतिविधियों के लिए ऑब्जेक्ट को कैसे पहुंचाया जा सकता है?

मुझे दूसरी गतिविधि पर पहली गतिविधि (जेजा वर्ग को instanciating) पर बनाई गई वस्तु का उपयोग करने की आवश्यकता है।

ऐसा करने का सबसे आसान तरीका क्या है? मैं इसके बारे में गुमराह करता हूं और जावा वर्ग पर पार्सलबल इंटरफ़ेस को कार्यान्वित करना इस समस्या का सबसे आम उत्तर प्रतीत होता है। लेकिन वस्तु एक जटिल और पार्सलिंग है जो इसके प्रत्येक सदस्य को ब्रूट-फोर्स समाधान की तरह लगता है।

क्या इसका कोई शानदार समाधान नहीं है?

धन्यवाद

संपादित करें: एक सरल SQLite डेटाबेस पर वस्तु डेटा भंडारण के लिए एक समाधान है?

उत्तर

3

आप आवेदन संदर्भ (हालांकि यह मूल रूप से वैश्विक राज्य भंडारण है, ताकि आप आप इसे कैसे उपयोग कर रहे हैं सावधान रहना चाहिए) इस्तेमाल कर सकते हैं:

Using Application context everywhere?

+0

+1, यह मेरी पसंद का तरीका भी है। बहुत लचीला है क्योंकि आप अपने स्वयं के गेटर्स को कस्टम एप्लिकेशन क्लास में एक सेटर्स लिख सकते हैं। – Guillaume

2

के बीच सूचना पारित करने के लिए सबसे आसान तरीकों में से एक गतिविधियों का उपयोग बंडल का उपयोग करना है।

आप नई गतिविधि लॉन्च करने से पहले एक्स्ट्रा जोड़ सकते हैं।

सबसे पहले:

दूसरे छोर पर (अन्य गतिविधि) आप तो बंडल से बाहर इन अतिरिक्त प्राप्त करते हैं और संभवतः नई गतिविधि में वस्तु का पुनर्निर्माण कर सकते हैं ..

यहाँ एक उदाहरण है सभी, यहाँ छात्र कक्षा है:

छात्र वर्ग - 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> 

एक्सएमएल और प्रोग्रामेटिक स्क्रिप्टिंग के मिश्रण के लिए क्षमा करें, मैं के बाद पहली गतिविधि है कि यह फ़ाइलों का एक बहुत कुछ हो सकता है तो प्रदर्शित करने के लिए मैं सघन महसूस किया अंतिम फ़ाइल, लेकिन आपको विचार प्राप्त करने में सक्षम होना चाहिए ..

आशा है कि इससे मदद मिलती है!

0

मुझे लगता है कि सिंगलटन सभी गतिविधियों में ऑब्जेक्ट को सुलभ बनाने का सही तरीका है। यह कैसे काम करता है के बारे में अधिक जानकारी के लिए this उत्तर देखें।

संबंधित मुद्दे

 संबंधित मुद्दे