2011-09-25 30 views
8

tôi viết một hộp thoại tùy chỉnh và cố gắng để có được một số dữ liệu từ hoạt động mẹ của nó, nhưng tôi luôn có được rỗng khi tôi gọi getOwnerActivity, bất cứ ai có thể cho tôi biết tại sao điều này xảy ra? Tại sao tôi có thể hiển thị dữ liệu trong DemoDialog trong khi không hiển thị dữ liệu từ TestDialogActivity?getOwnerActivity trả về null trong hộp thoại tùy chỉnh

Rất cám ơn trước.

DialogTestActivity

public class DialogTestActivity extends Activity { 
    List<String> data = new ArrayList<String>(); 
    Button button; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     button = (Button)findViewById(R.id.button); 
     button.setOnClickListener(new OnClickListener(){ 
      public void onClick(View v) { 
       showDialog(0); 
      } 
     }); 
    } 

    public List<String> getData(){ 
     data.add("one"); 
     data.add("two"); 
     data.add("three"); 
     return data; 
    } 

    public Dialog onCreateDialog(int id){ 
     return new DemoDialog(this); 
    } 
} 

DemoDialog

public class DemoDialog extends Dialog { 
    Context context; 

    public DemoDialog(Context context) { 
     super(context); 
     setContentView(R.layout.dialog); 
     this.context = context; 
     setTitle("Delete City"); 
     ListView list = (ListView)findViewById(R.id.list); 
     ArrayAdapter<String> aa = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, ((DialogTestActivity)getOwnerActivity()).getData()); 
//  ArrayAdapter<String> aa = new ArrayAdapter<String>(context, android.R.layout.simple_list_item_multiple_choice, getData()); 
     list.setAdapter(aa); 
     list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); 
    } 

    private List<String> getData(){ 
     List<String> data = new ArrayList<String>(); 
     data.add("1"); 
     data.add("2"); 
     return data; 
    } 
} 

Trả lời

5

Nếu bạn nghĩ về tình hình, bạn sẽ hiểu tại sao. Khi bạn gọi new DemoDialog(this), bạn thực thi tất cả mã trong hàm tạo. Sau đó, bạn trả lại từ onCreateDialog và Android thực hiện phép thuật của nó. Nếu bạn cố gắng để có được chủ sở hữu từ các nhà xây dựng, Android đã không nối nó được nêu ra, vì vậy bạn không có chủ sở hữu nào được nêu ra.

Vì vậy, bạn có thể làm một trong những:

public class DemoDialog extends Dialog { 
    public DemoDialog(Context context) { 
     super(context); 

     // chances of context not being an activity is very low, but better to check. 
     Activity owner = (context instanceof Activity) ? (Activity)context : null; 
     if (owner != null) { 
      // owner activity is defined here 
     } 
    } 

    public void onAttachedToWindow() { 
     super.onAttachedToWindow(); 
     // getOwnerActivity() should be defined here if called via showDialog(), so do the related init here 
     Activity owner = getOwnerActivity(); 
     if (owner != null) { 
      // owner activity defined here 
     } 
    } 
} 

Lưu ý rằng phương pháp thứ hai được ưa thích vì

+0

Lưu ý: Tôi có nghĩa là các cuộc gọi onCreate() của Dialog. Tuy nhiên, bạn có thể cần phải đặt nó trong onAttachedToWindow() nếu điều đó không thành công, bởi vì Android có thể thực sự trì hoãn phép thuật tùy ý. Điều đó nói rằng, nếu bạn cần phải truy cập vào chủ sở hữu từ các nhà xây dựng, chủ sở hữu là "bối cảnh", vì vậy chỉ cần sử dụng "bối cảnh" :) – Sajid

+0

Cảm ơn bạn đã trả lời của bạn, Sajid. Tôi đã cố gắng làm theo đề nghị của bạn nhưng sitll có null, bạn có thể vui lòng chia sẻ mã của bạn? – eric2323223

+0

Đã thêm làm chỉnh sửa ở trên. Bạn phải kiểm tra trênAttachToWindow() sau khi tất cả. – Sajid

16

Tôi cố gắng để sử dụng getOwnerActivity() phương pháp trong tất cả các phương pháp có thể có của custom Dialog tôi. Nó luôn trả về null (Android 2.3). Sau đó, tôi đã kiểm tra mã nguồn của nó và hoạt động mà nó trả về chỉ được đặt trong setOwnerActivity(Activity activity) không được gọi ở bất kỳ đâu. Vì vậy, nếu bạn muốn getOwnerActivity() trở về giá trị khác với null, bạn phải làm điều này:

public MyCustomDialog(Context context) { 
    super(context); 
    if (context instanceof Activity) { 
     setOwnerActivity((Activity) context); 
    } 
} 
+0

true !!!!!!!!!!! –

0

này, dưới đây, làm việc cho tôi.

private Activity activity; 

public MyCustomDialog(Activity activity) { 
    super(activity); 
    this.activity = activity; 
} 

Sau đó, tôi sử dụng hoạt động thay vì getOwnerActivity().

1

Bạn có thể mở rộng hộp thoại tùy chỉnh của bạn từ AppCompatDialog và tiếp cận các hoạt động của mã này:

public static Activity scanForActivity(Context context) { 
    if (context == null) 
     return null; 
    else if (context instanceof Activity) 
     return (Activity) context; 
    else if (context instanceof ContextWrapper) 
     return scanForActivity(((ContextWrapper) context).getBaseContext()); 
    return null; 
} 
Các vấn đề liên quan