2013-05-06 35 views
7

Tôi sẽ xem trang Nhà phát triển Android của Google trên Hộp thoại, cụ thể là phần this. Tuy nhiên, thay vì tạo thông điệp của DialogFragment lập trình, tôi đã bố trí cài đặt trước tên layout_newpayperiod.xml với các yếu tố sau:Truy xuất giá trị từ EditText trong DialogFragment

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical" > 

<Spinner 
    android:id="@+id/spinner_payperiod" 
    android:layout_width="fill_parent" 
    android:layout_height="48dp" 
    android:padding="8dp" 
    android:entries="@array/pay_periods" 
    /> 

<EditText 
    android:id="@+id/edittext_savepercent" 
    android:layout_width="fill_parent" 
    android:layout_height="48dp" 
    android:padding="8dp" 
    android:inputType="number" 
    android:hint="Percent to Save" 
    /> 

<EditText 
    android:id="@+id/edittext_payment" 
    android:layout_width="fill_parent" 
    android:layout_height="48dp" 
    android:padding="8dp" 
    android:inputType="numberDecimal" 
    android:hint="Total Payment" 
    /> 

</LinearLayout> 

Khi tôi gọi là DialogFragment nó xuất hiện như bình thường, với Spinner có các giá trị thích hợp. Tôi điền vào các mục và nhấn "OK", nhưng khi tôi cố gắng lấy các giá trị từ Spinner và hai trường EditText, các lực lượng ứng dụng đóng với một NumberFormatException: Invalid double "". Tôi có cảm giác rằng tôi không lấy lại được lượt xem. Bất cứ ai có thể giúp tôi con số này ra xin vui lòng? Cảm ơn!

public class StartPayperiodDialogFragment extends DialogFragment { 

/* The activity that creates an instance of this dialog fragment must 
* implement this interface in order to receive event callbacks. 
* Each method passees the DialogFragment in case the host needs to query it. 
*/ 
public interface StartPayperiodDialogListener{ 
    public void onDialogPositiveClick(DialogFragment dialog); 
    public void onDialogNegativeClick(DialogFragment dialog); 
} 

// Use this instance of the interface to deliver action events 
StartPayperiodDialogListener listener; 

// Override the Fragment.onAttach() method to instantiate the StartPayperiodDialogListener 
@Override 
public void onAttach(Activity activity){ 
    super.onAttach(activity); 
    // Verify that the host activity implements the callback interface 
    try{ 
     // Instantiate the NoticeDialogListener so we can send events to the host 
     listener = (StartPayperiodDialogListener) activity; 
    }catch(ClassCastException e){ 
     // The activity doesn't implement the interface, throw exception 
     throw new ClassCastException(activity.toString() + " must implement StartPayperiodDialogListener"); 
    } 
} 

@Override 
public Dialog onCreateDialog(Bundle savedInstanceState){ 
    // Use the Builder class for convenient dialog construction 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    View transactionLayout = View.inflate(getActivity(), R.layout.layout_newpayperiod, null); 
    builder.setView(transactionLayout) 
    .setPositiveButton("OK", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // Send the positive button event back to the calling activity 
      listener.onDialogPositiveClick(StartPayperiodDialogFragment.this); 
     } 
    }) 
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      // Send the negative button event back to the calling activity 
      listener.onDialogNegativeClick(StartPayperiodDialogFragment.this); 
     } 
    }); 
    return builder.create(); 
} 

}

Trong MainActivity.class, phương pháp gọi lại:

@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    // User pressed OK, so we need to grab the values from the 
    // dialog's fields and apply them to the Views in the Main 
    // Activity 

    View transactionLayout = View.inflate(this, R.layout.layout_newpayperiod, null); 

    // Start with the payment amount 
    EditText paymentEt = (EditText) transactionLayout.findViewById(R.id.edittext_payment); 
    TextView paymentTv = (TextView) findViewById(R.id.text_paycheck); 
    paymentTv.setText(moneyFormat.format(Double.parseDouble(paymentEt.getText().toString()))); 

    // Next, the percent to save 
    EditText savingsEt = (EditText) transactionLayout.findViewById(R.id.edittext_savepercent); 
    TextView savingsTv = (TextView) findViewById(R.id.text_savings); 
    savingsTv.setText(savingsEt.getText().toString() + "%"); 

    // Then, the pay period 
    Spinner periodSp = (Spinner) transactionLayout.findViewById(R.id.spinner_payperiod); 
    TextView periodTv = (TextView) findViewById(R.id.text_payperiod); 
    periodTv.setText(periodSp.getSelectedItem().toString()); 

    // Finally, let's update the daily allowance amount and clear 
    // the adapter 
    adapter.clear(); 
    adapter.notifyDataSetChanged(); 
    TextView allowanceTv = (TextView) findViewById(R.id.text_allowance); 
    Double allowanceValue; 
    switch(periodSp.getSelectedItemPosition()){ 
     case(0): // Daily 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString()); 
      break; 
     case(1): // Weekly 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString())/7; 
      break; 
     case(2): // 2 Weeks 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString())/14; 
      break; 
     case(3): // 30 Days 
      allowanceValue = Double.parseDouble(paymentTv.getText().toString())/30; 
      break; 
     default: // Debugging purposes only 
      allowanceValue = 42.0; 
      break; 
    } 
    allowanceTv.setText(Double.toString(allowanceValue)); 
} 

Trả lời

18

Hãy thử điều này:

@Override 
public void onDialogPositiveClick(DialogFragment dialog) { 
    // User pressed OK, so we need to grab the values from the 
    // dialog's fields and apply them to the Views in the Main 
    // Activity 

    // Start with the payment amount 
    Dialog dialogView = dialog.getDialog(); 
    EditText paymentEt = (EditText) dialogView.findViewById(R.id.edittext_payment); 

... vv (Lấy bất kỳ quan điểm khác từ hộp thoại bằng cách truy vấn dialogView trong cùng một cách.)

đang thổi phồng của bạn "thổi phồng" một thương hiệu mới phiên bản của chế độ xem đó. Bạn muốn truy cập cái đã được tạo trong hộp thoại.

+0

Tôi đã cho nó một shot, nhưng tôi nhận được một NullPointerException trên 'EditText paymentEt = (EditText) dialogView.findViewById (R.id.edittext_payment);' line. Tôi nghi ngờ các cuộc gọi dialog.getView() không làm những gì chúng tôi mong đợi ... – Argus9

+1

@ Argus9 bạn là chính xác. Đó sẽ là sử dụng thích hợp cho một đoạn bình thường, nhưng đối với một DialogFragment, bạn cần sử dụng getDialog(). Tôi đã chỉnh sửa câu trả lời của mình để phản ánh. –

+0

Phản ứng thiên tài tuyệt đối, cảm ơn bạn Jay! – Argus9

1

Tôi nghĩ rằng dòng này View transactionLayout = View.inflate(this, R.layout.layout_newpayperiod, null); messes tất cả mọi thứ. Có thể nó không gây rối, nhưng bạn đang nhận được địa chỉ của bố trí mới được tạo ra và gán nó vào tài liệu tham khảo transactionLayout. Sau đó, bạn sẽ nhận được lượt xem từ bố cục đó EditText paymentEt = (EditText) transactionLayout.findViewById(R.id.edittext_payment); chắc chắn không được khởi tạo. Nó có giá trị chuỗi rỗng giá trị -> "";

Tôi nghĩ bạn nên sử dụng findViewById để có được tham chiếu đến EditText của bạn như bạn làm với TextView của bạn. Nhưng khi bạn đang ở trong MainActivity bố cục của bạn có lẽ không phải là chế độ xem gốc cho số R.layout.layout_newpayperiod, bạn phải tìm cách để thực hiện điều đó một cách chính xác.

Bạn có số DialogFragment làm tham số trong phương thức gọi lại onDialogPositiveClick này. Vì vậy, bạn có thể có được nó là View và bố cục bạn đang tìm kiếm - có chứa

EditText của bạn. Xin lỗi vì đã chỉnh sửa bài đăng này rất nhiều lần.

+0

Tôi nghĩ lý do duy nhất tôi chọn không làm điều này là vì khi sử dụng findViewById như tôi đã làm với TextViews, tôi nhận được NullPointerException. Nó giống như các phần tử tồn tại trong R.id, nhưng ứng dụng không thể tìm thấy chúng trong thời gian chạy. Tôi sẽ thử lại lần nữa và xem nó có giúp ích gì không. – Argus9

+0

Ồ, cũng - các TextView có mặt trong tệp XML bố cục MainActivity, được gọi trong SetContentView() trong phương thức onCreate() của Activity. – Argus9

+0

Bạn nhận được tài liệu tham khảo Null vì các chế độ xem này không liên quan đến nhau. Kiểm tra câu trả lời của Jay Bobzin mà nên giải quyết vấn đề của bạn – Garet

Các vấn đề liên quan