2012-12-04 23 views
11

मैं इरादे के माध्यम से ऑब्जेक्ट्स के ऐरेलिस्ट को पास करने की कोशिश कर रहा हूं लेकिन इसे काम नहीं कर सकता। यहाँ मैं क्या है:इरादे के माध्यम से ऑब्जेक्ट्स के ऑरेलिस्ट को पास करना - जावा (एंड्रॉइड)

public class QuestionView extends Activity { 

    //variables and other methods... 

    public void endQuiz() { 
     Intent intent = new Intent(QuestionView.this, Results.class); 
     intent.putExtra("correctAnswers", correctAnswers); 
     intent.putExtra("wrongAnswers", wrongAnswers); 
     intent.putParcelableArrayListExtra("queries", queries); 
     startActivity(intent); 
    } 
} 

उद्देश्य यहाँ प्राप्त किया जा रहा है:

public class Results extends Activity { 

    int cAnswers; 
    int wAnswers; 
    ArrayList<Question> qs; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.resultsmain); 

     cAnswers = getIntent().getIntExtra("correctAnswers", -1); 
     wAnswers = getIntent().getIntExtra("wrongAnswers", -1); 

     qs = getIntent().getParcelableArrayListExtra("queries"); 

        //more code... 
    } 
} 

दो ints, correctAnswer और wrongAnswers, प्राप्त किया जा रहा है और मैं उन्हें इस्तेमाल कर सकते हैं। ArrrayList के माध्यम से नहीं आ रहा है। EndQuiz() विधि में कोई त्रुटि नहीं है लेकिन 'qs = getIntent()। GetParcelableArrayListExtra ("query");' एक त्रुटि फेंक रहा है और कह रहा है "बाउंड मिस्चैच।"

इस पर कोई मदद की सराहना की जाती है!

प्रश्न वर्ग:

public class Question { 
    String a1; 
    String a2; 
    String a3; 
    String a4; 
    int correctAnswer; 
    String query; 
    int selectedAnswer; 
    boolean correctness; 

    public Question() { 
    } 

    public Question(String a1, String a2, String a3, String a4, int correctAnswer, String query, int selectedAnswer, boolean correctness) { 
     this.a1 = a1; 
     this.a2 = a2; 
     this.a3 = a3; 
     this.a4 = a4; 
     this.correctAnswer = correctAnswer; 
     this.query = query; 
     this.selectedAnswer = selectedAnswer; 
     this.correctness = correctness; 
    } 
    } 
+1

क्या आपका 'प्रश्न' वर्ग लागू [पार्ससेलबल] (http://developer.android.com/reference/android/os/Parcelable.html) है? – wsanville

+3

नहीं ... यही कारण है कि वह इसे कास्टिंग कर रहा है ... '(ArrayList > प्रश्नों को बढ़ाता है);' :) जो निश्चित रूप से – Selvin

+0

की सहायता नहीं करेगा क्या हम देख सकते हैं कि आपके प्रश्नों का प्रकार वास्तव में कैरे जैसा दिखता है? मेरा मतलब वर्ग है ... – LuxuryMode

उत्तर

25

आप वास्तव में Parcelable लागू करने के लिए अपने Question वर्ग को बदलना होगा। पार्ससेल इंटरफेस पहले भ्रमित हो सकता है ... लेकिन वहां लटका।

वहाँ दो Parcelable तरीकों कि आप पर ध्यान केंद्रित करना चाहिए:

  • writeToParcel() जो एक पार्सल वस्तु में अपनी कक्षा बदल देता है।
  • Question(Parcel in) जो एक पार्सल ऑब्जेक्ट को आपकी कक्षा के प्रयोग योग्य उदाहरण में वापस परिवर्तित करता है।

आप सुरक्षित रूप से & को पार कर सकते हैं जो मैंने चिह्नित अन्य पार्सल योग्य जानकारी पेस्ट किया है।

सरलता के मैं सिर्फ अपने प्रश्न वर्ग का हिस्सा उपयोग करेगा के लिए:

public class Question implements Parcelable { 
    String a1; 
    String a2; 
    ... 

    public Question(String a1, String a1) { 
     this.a1 = a1; 
     this.a2 = a2; 
    } 

    // Your existing methods go here. (There is no need for me to re-write them.) 

    // The following methods that are required for using Parcelable 
    private Question(Parcel in) { 
     // This order must match the order in writeToParcel() 
     a1 = in.readString(); 
     a2 = in.readString(); 
     // Continue doing this for the rest of your member data 
    } 

    public void writeToParcel(Parcel out, int flags) { 
     // Again this order must match the Question(Parcel) constructor 
     out.writeString(a1); 
     out.writeString(a2); 
     // Again continue doing this for the rest of your member data 
    } 

    // Just cut and paste this for now 
    public int describeContents() { 
     return 0; 
    } 

    // Just cut and paste this for now 
    public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() { 
     public Question createFromParcel(Parcel in) { 
      return new Question(in); 
     } 

     public Question[] newArray(int size) { 
      return new Question[size]; 
     } 
    }; 
} 

अब बदल कैसे आप अपने आशय एक्स्ट्रा कलाकार में queries डाल दिया।

intent.putParcelableArrayListExtra("queries", queries); 

जिस तरह से आप पार्ससेल योग्य सरणी पढ़ते हैं वह बिल्कुल सही है।

+0

उस विस्तृत उत्तर सैम के लिए धन्यवाद! मैंने आपको अपना पूरा कोड नहीं दिया है इसलिए मैंने आपको निश्चित रूप से फेंक दिया होगा। मैंने अपनी सभी कक्षाओं के साथ अपनी शुरुआती पोस्ट संपादित की। अब इसके साथ, क्या मुझे पार्सेलबल लागू करना चाहिए और उन सभी अन्य तरीकों को QuestionView या प्रश्न में जोड़ना चाहिए? – Matt

+0

आप केवल प्रश्नों के एक ऐरेलिस्ट को पास करना चाहते हैं, इसलिए केवल प्रश्न वर्ग को पार्सलबल – Sam

+0

लागू करना चाहिए ओह मैन यह काम करता है! यह सबसे दूर है कि मैं इस पर काम कर रहे 2 महीनों में रहा हूं। आज विशाल मील का पत्थर! फिर से धन्यवाद सैम। :) – Matt

7

आदेश में एक आशय के हिस्से के रूप एक ArrayList वितरित करने के लिए सक्षम होने के लिए, अपने प्रकार Parcelable को लागू करना चाहिए। इस तथ्य के आधार पर आपको अपनी सूची (ArrayList<? extends Parcelable>) पर डालना पड़ा, आपने ऐसा नहीं किया। यदि आप के लिए किया था, तो आप बस हो सकता है:

class Foo implements Parcelable { 
//implementation here 
} 
ArrayList<Foo> foos = new ArrayList<Foo>(); 
new Intent().putParcelableArrayListExtra("foos", foos); //no need to cast 
+0

पार्सेलबल को लागू करना आवश्यक नहीं है, इसके लिए सीरियलज़ेबल का उपयोग करना एक प्रदर्शन जुर्माना शामिल है, इसलिए आपको वास्तव में पार्सेलबल का उपयोग करना चाहिए। –