如何从Alert Dialog保存用户输入?

时间:2017-08-21 12:56:15

标签: android nullpointerexception android-recyclerview alertdialog

我正在构建一个应用程序,该应用程序应该从用户输入文本并将它们放入自定义列表项中的textviews中。用户将文本输入AlertDialog,但一旦AlertDialog关闭(因此被销毁),输入的所有文本都会随之消失并给出NullPointerException错误。

这是处理对话框创建和数据检索的代码:

    public class AssignmentActivity extends AppCompatActivity {
    ...

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_assignment);

        ...

        FloatingActionButton assignmentAddButton = (FloatingActionButton) findViewById(R.id.assignment_add_button);

        assignmentAddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                editCourseInfoDialog();
            }
        });

    }

    private void editCourseInfoDialog() {
        LayoutInflater inflater = LayoutInflater.from(AssignmentActivity.this);
        View dialogLayout = inflater.inflate(R.layout.assignment_edit_dialog, null);
        AlertDialog.Builder dialog = new AlertDialog.Builder(AssignmentActivity.this);
        dialog.setView(dialogLayout);

        final TextView name = (TextView) dialogLayout.findViewById(R.id.assignmentNameView);
        final TextView mark = (TextView) dialogLayout.findViewById(R.id.assignmentMarkView);
        final TextView overallMark = (TextView) dialogLayout.findViewById(R.id.assignmentOverallMarkView);
        final TextView weight = (TextView) dialogLayout.findViewById(R.id.assignmentWeightView);

        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                assignment.setName(String.valueOf(name.getText()));
                assignment.setMark(Double.valueOf(mark.getText() + ""));
                assignment.setMarkOutOf(Double.valueOf(overallMark.getText() + ""));
                assignment.setPercentage(Double.valueOf(weight.getText() + ""));
                assignmentAdapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });

        dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        dialog.show();
    }
}

1 个答案:

答案 0 :(得分:1)

初始化的问题。

LayoutInflater inflater = LayoutInflater.from(AssignmentActivity.this);
View dialogLayout = inflater.inflate(R.layout.assignment_edit_dialog, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(AssignmentActivity.this);
dialog.setView(dialogLayout);

final TextView name = (TextView) dialogLayout .findViewById(R.id.assignmentNameView);
final TextView mark = (TextView) dialogLayout .findViewById(R.id.assignmentMarkView);
final TextView overallMark = (TextView) dialogLayout .findViewById(R.id.assignmentOverallMarkView);
final TextView weight = (TextView) dialogLayout .findViewById(R.id.assignmentWeightView);

正如您所说,TextViews与AlertDialog自定义视图相连,那么它们的上下文应该是Dialog视图。

在概念方面你需要学习很多东西。您在初始化列表之前设置了适配器。

assignmentAdapter = new AssignmentListAdapter(assignmentList, this); // at this line **assignmentList** is null as you initiliaze after this line
assignment = new Assignment(this);

您必须先按照这样初始化

assignment = new Assignment(this);
assignmentAdapter = new AssignmentListAdapter(assignmentList, this);