質問

I have created a Custom AlertDialog and I'm trying to set text for EditTexts. I have tried following

public class CheckListDialog extends DialogFragment {
    private View view;

    public CheckListDialog(View view) {
        this.view = view;
    }

    @Override
    public AlertDialog onCreateDialog(Bundle savedInstanceState) {         
        LayoutInflater inflater = getActivity().getLayoutInflater();    

        View dialogLayout = inflater.inflate(R.layout.dialog_checklist, null);

        EditText etCost = (EditText) dialogLayout.findViewById(R.id.etCost);
        EditText etOdoReading = (EditText) dialogLayout.findViewById(R.id.etOdometer);
        etOdoReading.setText("bla");
        etCost.setText("tada");

        AlertDialog.Builder builder = new AlertDialog.Builder(new ContextThemeWrapper(Reminders.this, android.R.color.transparent));
         builder.setView(inflater.inflate(R.layout.dialog_checklist, null))
               .setTitle("jaja")
               .setPositiveButton("Add", new DialogInterface.OnClickListener() {
                   @Override
                   public void onClick(DialogInterface dialog, int id) {

                   }
               })
               .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {


                   }
               });      
        return builder.create();
        }
    }

The EditTexts are empty and does not contain the values. How can i fix this?

役に立ちましたか?

解決

Replace

builder.setView(inflater.inflate(R.layout.dialog_checklist, null))

with

builder.setView(dialogLayout)

You're modifying the edittexts in one layout and then inflating a new layout for the dialog.

他のヒント

For such operations you have to use a custom layout to inflate into the CheckListDialog class of yours. DialogFragment will provide you complete freedom to customize your dialog as per your requirement.

You can refer to this tutorial

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top