2011-05-25 24 views
9

पर डेटा पास करें मैं एक गतिविधि से डेटा को एक संवाद बॉक्स पर पास करने का एक तरीका ढूंढ रहा हूं। मैं showDialog(int); पर कॉल करने की कोशिश कर रहा हूं, हालांकि मुझे संवाद बॉक्स में किसी भी डेटा को पास करने का कोई तरीका नहीं दिख रहा है। मैं एक स्ट्रिंग के लिए एक पुष्टि प्रदर्शित करने के लिए :)गतिविधि से डायलॉग

चीयर्स

उत्तर

13

यदि आप Android 2.2 (एपीआई स्तर 8 या उन्नत) लक्षित कर रहे हैं आप

public final boolean showDialog (int id, Bundle args) 

का उपयोग और Bundle में अपने तर्क दे सकते हैं। documentation देखें।

यदि आप पुराने एंड्रॉइड संस्करणों का समर्थन करना चाहते हैं, तो आपको अपने तर्क Activity कक्षा के सदस्यों में सहेजना चाहिए और फिर उन्हें अपने onPrepareDialog फ़ंक्शन से एक्सेस करना चाहिए। ध्यान दें कि onCreateDialog आपकी आवश्यकताओं के अनुरूप नहीं होगा क्योंकि इसे केवल संवाद निर्माण के लिए बुलाया जाता है।

class MyActivity { 

    private static int MY_DLG = 1; 
    private String m_dlgMsg; 

    private showMyDialog(String msg){ 
     m_dlgMsg = msg; 
     showDialog(MY_DLG); 
    } 

    private doSomething() { 
     ... 
     showMyDlg("some text"); 
    } 

    protected void onCreateDialog(int id){ 
     if(id == MY_DLG){ 
      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      .... 
      return builder.create(); 
     } 
     return super.onCreateDialog(id); 
    }   

    @Override 
    protected void onPrepareDialog (int id, Dialog dialog){ 
     if(id == MY_DLG){ 
      AlertDialog adlg = (AlertDialog)dialog; 
      adlg.setMessage(m_dlgMsg); 
     } else { 
      super.onPrepareDialog(id, dialog); 
     }    
    } 
} 
+0

बहुत बढ़िया, अच्छा लगता है :) केवल समस्या यह है कि जहां आप DoSomething(), मैं, एक इनलाइन onClick ईवेंट के भीतर से showDialog बोल रहा हूँ तो यह माध्यम से मान पास होने के लिए नहीं लगता है, i ' मुझे यकीन नहीं है कि क्यों ... – jsw

+0

'showDialog (int id, बंडल args)' एपीआई 13 में बहिष्कृत –

1

में आप कर रहे हैं जब showDialog (int) संवाद बॉक्स को पारित करने के लिए गतिविधि के onCreateDialog विधि कहा जाता है की जरूरत है। वहां आपको एक संवाद उदाहरण बनाना होगा और इसे वापस करना होगा, और यह दिखाया जाएगा।

फिर आप एक संवाद बना रहे हैं, आपके पास अपने वर्ग के फ़ील्ड तक पूर्ण पहुंच है और बनाए गए संवाद के पैरामीटर और सामग्री को समायोजित करने के लिए उनके मूल्यों का उपयोग कर सकते हैं।

1
//`enter code here`I used shared preferences and it worked. 
**in your activity:** 

//your field: public static final String GAME_PREFERENCES = null; 

         String template = selectedItem.getProduct().getName(); 
      String num = selectedItem.getNumber(); 
      String id = selectedItem.getId(); 
      String location = selectedItem.getLocationName(); 


     SharedPreferences settings = getSharedPreferences(GAME_PREFERENCES, 
        MODE_PRIVATE); 
      SharedPreferences.Editor prefEditor = settings.edit(); 
      prefEditor.putString("template", template); 
      prefEditor.putString("num", num); 
      prefEditor.putString("id", id); 
      prefEditor.putString("location", location); 
      prefEditor.commit(); 

**in your dialog class:** 
//In my case I needed to add activity because how i created dialog: 

public MyDialog(Context context, int theme) { 
     super(context, theme); 
     init(context); 
    } 

    private void init(Context context) { 
     setContentView(R.layout.dialog); 
     this.context = context; 
     final Activity activity = (Activity) context; 

       // If you dont call activity you can try context.getpreferences(......) 
     SharedPreferences prefs = ((Activity) context) 
       .getPreferences(Context.MODE_PRIVATE); 

     String template = prefs.getString("template", ""); 
     String num = prefs.getString("num", ""); 
     String id = prefs.getString("id", ""); 
     String location = prefs.getString("location", ""); 

}`enter code here` 
0

मैं जानता हूँ कि इस सवाल का वर्ष है, लेकिन अगर आप एक कस्टम संवाद का उपयोग कर रहे आप setArguments विधि का उपयोग कर सकते हैं।

String myString = "How to do it" 
DialogFragment newFragment = new AddUserDialog(); 
       Bundle args = new Bundle(); 
       args.putString("number", myString); //The first parameter is the key that will be used to retrieve the value, which is the second parameter. 
       newFragment.setArguments(args); 
       newFragment.show(getSupportFragmentManager(), "add_a_member"); 
संबंधित मुद्दे