如何使用数据库中的初始值填充Alert Box

时间:2014-06-08 09:18:05

标签: java android android-alertdialog

我需要在弹出框后用数据库中的数据填充警告框。以下是代码

@Override
public boolean onCreateOptionsMenu(Menu m) {
    /*      // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;*/

    super.onCreateOptionsMenu(m);
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main , m);
    menu = m;

    MenuItem item = menu.findItem(R.id.action_settings);
    item.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override
        public boolean onMenuItemClick(MenuItem item) {

            // get prompts.xml view
            LayoutInflater li = LayoutInflater.from(context);
            View promptsView = li.inflate(R.layout.register, null);

            if(databaseHelper.getAllDetails().getCount() > 0){
            EditText name = (EditText) findViewById(R.id.reg_fullname);
            EditText contact = (EditText) findViewById(R.id.reg_contact);
            EditText emergencyNo1 = (EditText) findViewById(R.id.reg_emergency_number1);
            EditText emergencyNo2 = (EditText) findViewById(R.id.reg_emergency_number2);
            EditText emergencyNo3 = (EditText) findViewById(R.id.reg_emergency_number3);

            Cursor cursor = databaseHelper.getAllDetails();
            if(cursor.moveToFirst()){
                do {
                    name.setText("");
                    //name.setText(cursor.getString(cursor.getColumnIndex("name")));
                    contact.setText(cursor.getString(cursor.getColumnIndex("phone")));
                    emergencyNo1.setText(cursor.getString(cursor.getColumnIndex("number1")));
                    emergencyNo2.setText(cursor.getString(cursor.getColumnIndex("number2")));
                    emergencyNo3.setText(cursor.getString(cursor.getColumnIndex("number3")));
                } while (cursor.moveToNext());
            }
        }
            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set prompts.xml to alertdialog builder
            alertDialogBuilder.setView(promptsView);

            databaseHelper = new UserDatabaseHelper(context);



            // set dialog message
            alertDialogBuilder
            .setCancelable(false)
            .setPositiveButton("Save",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog,
                        int id) {

                    saveOrUpdateDetails();
                    dialog.dismiss();

                }

                private void saveOrUpdateDetails() {
                    EditText name = (EditText) findViewById(R.id.reg_fullname);
                    EditText contact = (EditText) findViewById(R.id.reg_contact);
                    EditText emergencyNo1 = (EditText) findViewById(R.id.reg_emergency_number1);
                    EditText emergencyNo2 = (EditText) findViewById(R.id.reg_emergency_number2);
                    EditText emergencyNo3 = (EditText) findViewById(R.id.reg_emergency_number3);

                    if(databaseHelper.isDetailsExists()){
                        if(getEmergencyNo(getIntent()).isEmpty() && emergencyNo1 == null && emergencyNo2 == null && emergencyNo3 == null ){
                            databaseHelper.updateRecord(name, contact, null, null, null, null,"1");
                        }
                        else {
                            databaseHelper.updateRecord(name, contact, emergencyNo1, emergencyNo2, emergencyNo3, "123","0");
                        }
                    }
                    else if(emergencyNo1 == null && emergencyNo2 == null && emergencyNo3 == null ){
                        databaseHelper.saveRecord(name, contact, null, null, null, null,"1");
                    }
                    else {
                        databaseHelper.saveRecord(name, contact, emergencyNo1, emergencyNo2, emergencyNo3, "123","0");
                    }

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

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

            return true;
        }
    });
    return true;
}

正在发生的事情就在发生之后

View promptsView = li.inflate(R.layout.register, null);

为以下情况获取空值,因此应用程序因空指针而失败。

            EditText name = (EditText) findViewById(R.id.reg_fullname);
            EditText contact = (EditText) findViewById(R.id.reg_contact);
            EditText emergencyNo1 = (EditText) findViewById(R.id.reg_emergency_number1);
            EditText emergencyNo2 = (EditText) findViewById(R.id.reg_emergency_number2);
            EditText emergencyNo3 = (EditText) findViewById(R.id.reg_emergency_number3);

请告诉我我能做些什么。剩下的代码工作正常。感谢。

1 个答案:

答案 0 :(得分:0)

由于您的窗口小部件存在于活动布局的另一个布局中,您需要使用以下代码初始化窗口小部件:

EditText name = (EditText) promptsView.findViewById(R.id.reg_fullname);
EditText contact = (EditText) promptsView.findViewById(R.id.reg_contact);
EditText emergencyNo1 = (EditText) promptsView.findViewById(R.id.reg_emergency_number1);
EditText emergencyNo2 = (EditText) promptsView.findViewById(R.id.reg_emergency_number2);
EditText emergencyNo3 = (EditText) promptsView.findViewById(R.id.reg_emergency_number3);

您需要使用视图为YourView.findViewById

promptsView