2012-06-03 2 views
7

मेरे पास दो टुकड़ों के साथ एक गतिविधि है: एक ग्रिडव्यू में उत्पादों को दिखाने के लिए और दूसरे उत्पादों को दिखाने के लिए जो उपयोगकर्ता ऑर्डर (सूचीफ्रेगमेंट) में जोड़ता है। जब उपयोगकर्ता ग्रिड व्यू में किसी उत्पाद पर क्लिक करता है, तो मुझे एक संवाद (डायलॉगफ्रेगमेंट) प्रदर्शित करने की आवश्यकता होती है जिसमें मैं उत्पाद की मात्रा पूछता हूं। फिर, जब उपयोगकर्ता संवाद में स्वीकार करता है, तो मैं चाहता हूं कि उत्पाद ListFragment में दिखाई दे।एंड्रॉइड में टुकड़े/संवाद के बीच संचार

एक तरफ, मुझे ऑब्जेक्ट उत्पाद को डायलॉग के शीर्षक (उदाहरण के लिए) के रूप में दिखाने के लिए संवाद में पास करना होगा। तो मैंने इसे इस तरह से पारित किया था:

public static class ProductDialog extends DialogFragment { 

     static ProductDialog newInstance(ProductVO product) { 
      ProductDialog f = new ProductDialog(); 

      Bundle args = new Bundle(); 
      args.putSerializable("product", product); 
      f.setArguments(args); 

      return f; 
     } 

     @Override 
     public Dialog onCreateDialog(Bundle savedInstanceState) { 
      ProductVO product = (ProductVO) getArguments().getSerializable("product"); 

      return new AlertDialog.Builder(getActivity()) 
        .setIcon(R.drawable.ic_dialog_add) 
        .setTitle(R.string.add_product) 

        ... 

        .setPositiveButton(R.string.accept, 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 

          } 
         } 
        ) 
        .setNegativeButton(R.string.cancel, 
         new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int whichButton) { 
          } 
         } 
        ) 
        .create(); 
     } 
    } 

मुझे लगता है कि यह ओकी है, अगर मैं गलत हूं तो मुझे सही करें। लेकिन फिर, सकारात्मक बटन की ऑनक्लिक घटना में मुझे संवाद में प्रस्तुत मात्रा को पुनर्प्राप्त करना होगा, और उसके बाद इसे दूसरे खंड (सूचीफ्रेगमेंट) में पास करना होगा, और फिर इसे तुरंत सूची में प्रदर्शित किया जाना चाहिए।

मैं यह कैसे कर सकता हूं?

अग्रिम

उत्तर

10

recommended approach धन्यवाद टुकड़ा करने की गतिविधि से एक अंतरफलक का उपयोग कर गतिविधि DialogFragment से संवाद करने के लिए है, और फिर।

अपनी गतिविधि में:

public class Main extends FragmentActivity implements OnQuantitySelectedListener { 

    public interface OnQuantitySelectedListener { 
     void onFinishEditDialog(String inputText); 
    } 


    @Override 
    public void onFinishEditDialog(String inputText) { 
     Toast.makeText(this, "Quantity: " + inputText, Toast.LENGTH_SHORT).show(); 
    } 
} 

फिर DialogFragment भीतरी वर्ग

public static class ProductDialog extends DialogFragment { 

    static ProductDialog newInstance(ProductVO product) { 
     ProductDialog f = new ProductDialog(); 

     Bundle args = new Bundle(); 
     args.putSerializable("product", product); 
     f.setArguments(args); 

     return f; 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     ProductVO product = (ProductVO) getArguments().getSerializable("product"); 

     LayoutInflater factory = LayoutInflater.from(getActivity()); 
     final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null); 
     mEditText = (EditText) textEntryView.findViewById(R.id.txt_your_name); 

     return new AlertDialog.Builder(getActivity()) 
       .setIcon(R.drawable.ic_dialog_add) 
       .setTitle(R.string.add_product) 
       .setView(textEntryView) 
       .setPositiveButton(R.string.accept, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
          OnQuantitySelectedListener listener = (OnQuantitySelectedListener) getActivity(); 
          listener.onFinishEditDialog(mEditText.getText().toString()); 
         } 
        } 
       ) 
       .setNegativeButton(R.string.cancel, 
        new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int whichButton) { 
         } 
        } 
       ) 
       .create(); 
    } 
} 

R.layout.alert_dialog_text_entry के लिए एक्सएमएल API Demos से है। यह उपयोगकर्ता से मात्रा प्राप्त करने के आपके उपयोग के मामले में फिट नहीं है, लेकिन यह उपयोगकर्ता से मूल्य प्राप्त करने के लिए कस्टम लेआउट का उपयोग करके दिखाता है।

<?xml version="1.0" encoding="utf-8"?> 
<!-- Copyright (C) 2008 The Android Open Source Project 

    Licensed under the Apache License, Version 2.0 (the "License"); 
    you may not use this file except in compliance with the License. 
    You may obtain a copy of the License at 

      http://www.apache.org/licenses/LICENSE-2.0 

    Unless required by applicable law or agreed to in writing, software 
    distributed under the License is distributed on an "AS IS" BASIS, 
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
    See the License for the specific language governing permissions and 
    limitations under the License. 
--> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/username_view" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:text="@string/alert_dialog_username" 
     android:gravity="left" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/username_edit" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:scrollHorizontally="true" 
     android:autoText="false" 
     android:capitalize="none" 
     android:gravity="fill_horizontal" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <TextView 
     android:id="@+id/password_view" 
     android:layout_height="wrap_content" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:text="@string/alert_dialog_password" 
     android:gravity="left" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

    <EditText 
     android:id="@+id/password_edit" 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_marginLeft="20dip" 
     android:layout_marginRight="20dip" 
     android:scrollHorizontally="true" 
     android:autoText="false" 
     android:capitalize="none" 
     android:gravity="fill_horizontal" 
     android:password="true" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</LinearLayout> 
+1

चक्रीय विरासत का कारण बनता है? –

+0

मुझे दृढ़ता से लगता है कि यह उत्तर एक टुकड़ा '(संवादप्रवाह)' और एक गतिविधि '(मुख्य)' के बीच संचार के बारे में और बताता है कृपया इस धागे को एक समान प्रश्न के साथ जांचें: https://stackoverflow.com/q/18561119/3987745 –