即使我设置了文本,Edittext的文本也会返回空字符串

时间:2016-09-17 11:11:33

标签: java android

我想在警告对话框中设置基本信息,所以我创建了一个对话框,并使用EditText.getText()。toString()。trim()从编辑文本中获取值。但是,即使我输入警告对话框中的字符串,单击“正”按钮后,日志显示该字符串为空。这真的很奇怪,我自己找不到问题。

@Override
    public void onBindParentViewHolder(TodoListParentViewHolder todoListParentViewHolder, int i, Object o) {
        ToDoParent toDoParent = (ToDoParent) o;
        //todoListParentViewHolder.mTextView.setText(toDoParent.getTitle());
        todoListParentViewHolder.mAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(mContext);
                dialogBuilder.setView(R.layout.item_alert_dialog_to_do);
                dialogBuilder.setTitle("添加ToDo");
                dialogBuilder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
                        View dialogView = layoutInflater.inflate(R.layout.item_alert_dialog_to_do,null);
                        EditText toDoTitle = (EditText) dialogView.findViewById(R.id.alert_dialog_edit_text_todo_name);
                        CalendarView calendarView = (CalendarView) dialogView.findViewById(R.id.alert_dialog_calendar_todo_deadline);
                        final Calendar calendar = Calendar.getInstance();
                        calendarView.setOnDateChangeListener(new CalendarView.OnDateChangeListener() {
                            @Override
                            public void onSelectedDayChange(CalendarView view, int year, int month, int dayOfMonth) {
                                calendar.set(year,month,dayOfMonth);
                                Log.d("Date", CalendarStringConverter.fromCalenderToString(calendar));
                            }
                        });

                        EditText estimatedTime = (EditText) dialogView.findViewById(R.id.alert_dialog_estimated_time);
                        estimatedTime.setText("100");

                            ToDo toDo = new ToDo();
                            toDo.setToDoName(toDoTitle.getText().toString().trim());
                            toDo.setDeadline(calendar);
                            toDo.setDone(false);
                            toDo.setEstimatedTime(Integer.parseInt(estimatedTime.getText().toString().trim()));
                            toDo.setPlanName("haha"); 
                            Log.d("Todo Name",toDo.getToDoName());
                            //call database to store the data
                            MyDatabase db = MyDatabase.getInstance(mContext);
                            db.addTodo(toDo);
                            notifyDataSetChanged();

                        dialog.dismiss();
                    }
                });
                AlertDialog alertDialog = dialogBuilder.create();
                alertDialog.show();

            }
        });

    }

0 个答案:

没有答案