将字符串值从自定义对话框传递到另一个活动

时间:2015-01-10 14:27:17

标签: android csv android-alertdialog gettext getter

正在使用自定义对话框接收用户输入,然后使用getter方法将这些值传递给另一个活动。

但是当我将值传递给一个将字符串值输出到CSV文件shipNameanalystName等的方法时,这些值在文件中显示为空,如下所示{ {1}}虽然我在对话框中输入了值。

我通过在调试菜单的表达式窗口" "shipName中观察字符串值来调试问题,但值在表达式窗口中永远不会更新。

我从中收集到输入传递的方法i不正确。

有谁知道为什么输出的值是空的?

这是使用的对话框类:

analystName

当我选择设置按钮时,这就是我在SearchResult类中检索值的方式:

package ie.gmi.computing;



import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.renderscript.Sampler;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MyMessageDialog {

    private Context context;
    private EditText shipText, scientistNameText , scientistEmailText , volumeText , colourText ;

    private String shipString, scientistNameString , scientistEmailString , volumeString , colourString ;



    public AlertDialog displayMessage(Context context, String title, String message){
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(title);
        builder.setMessage(message);
        LayoutInflater inflater = LayoutInflater.from(context);
        final View v = inflater.inflate(R.layout.custom_view, null);
        builder.setView(v);
        shipText = (EditText)v.findViewById(R.id.shipNameEditText);
        scientistNameText = (EditText)v.findViewById(R.id.scientistEditText);
        scientistEmailText = (EditText)v.findViewById(R.id.emailEditText);
        volumeText = (EditText)v.findViewById(R.id.volumeEditText);
        colourText  = (EditText)v.findViewById(R.id.colourEditText);
        builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {



            }
        });
        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });
        AlertDialog dialog= builder.create();
        dialog.show();
        Button tb = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
        tb.setOnClickListener(new CustomListener(dialog));

        return dialog;
    }


    //getter/setters to allow access to string values
    //in SearchResult class
    public EditText getShipText() {
        return shipText;
    }



    public void setShipText(EditText shipText) {
        this.shipText = shipText;
    }



    public EditText getScientistNameText() {
        return scientistNameText;
    }



    public void setScientistNameText(EditText scientistNameText) {
        this.scientistNameText = scientistNameText;
    }



    public EditText getScientistEmailText() {
        return scientistEmailText;
    }


    public void setScientistEmailText(EditText scientistEmailText) {
        this.scientistEmailText = scientistEmailText;
    }



    public String getShipString() {
        return shipString;
    }


    public void setShipString(String shipString) {
        this.shipString = shipString;
    }


    public String getScientistNameString() {
        return scientistNameString;
    }


    public void setScientistNameString(String scientistNameString) {
        this.scientistNameString = scientistNameString;
    }


    public String getScientistEmailString() {
        return scientistEmailString;
    }


    public void setScientistEmailString(String scientistEmailString) {
        this.scientistEmailString = scientistEmailString;
    }


    public String getVolumeString() {
        return volumeString;
    }


    public void setVolumeString(String volumeString) {
        this.volumeString = volumeString;
    }


    public String getColourString() {
        return colourString;
    }


    public void setColourString(String colourString) {
        this.colourString = colourString;
    }


    public EditText getVolumeText() {
        return volumeText;
    }



    public void setVolumeText(EditText volumeText) {
        this.volumeText = volumeText;
    }



    public EditText getColourText() {
        return colourText;
    }



    public void setColourText(EditText colourText) {
        this.colourText = colourText;
    }



    @SuppressLint("NewApi")
    class CustomListener implements View.OnClickListener {
        private final Dialog dialog;
        public CustomListener(Dialog dialog) {
            this.dialog = dialog;
        }
        @SuppressLint("NewApi")
        @Override
        public void onClick(View v) {


            if(shipText.getText().toString().isEmpty() && !shipText.getText().toString().equals(null)){
                shipText.setError("The Field is required");

            }else if(scientistNameText.getText().toString().isEmpty() && !scientistNameText.getText().toString().equals(null)){
                scientistNameText.setError("The Field is required");
            }else if(scientistEmailText.getText().toString().isEmpty() && !scientistEmailText.getText().toString().equals(null)){
                scientistEmailText.setError("The Field is required");
            }else if(volumeText.getText().toString().isEmpty() && !volumeText.getText().toString().equals(null)){
                volumeText.setError("The Field is required");
            }else if(colourText.getText().toString().isEmpty() && !colourText.getText().toString().equals(null)){
                colourText.setError("The Field is required");
            }else{
                shipText.setError(null);
                scientistNameText.setError(null);
                scientistEmailText.setError(null);
                volumeText.setError(null);
                colourText.setError(null);


                shipString = shipText.getText().toString();
                scientistNameString = scientistNameText.getText().toString();
                scientistEmailString = scientistEmailText.getText().toString();
                volumeString = volumeText.getText().toString();
                colourString = colourText.getText().toString();


                Toast.makeText(dialog.getContext(), "The Values you get from : " +
                        "\n Ship name value: " + shipText.getText().toString() +
                        "\n Scientist name value: " + scientistNameText.getText().toString() +
                        "\n email value: " + scientistEmailText.getText().toString() +
                        "\n sample volume value: " + volumeText.getText().toString() +
                        "\n sample colour value: " + colourText.getText().toString() , Toast.LENGTH_SHORT).show();

                dialog.dismiss();
            }
        }
    }
}

3 个答案:

答案 0 :(得分:6)

您显示的对话框是异步的;也就是说,onOptionsItemSelected()中的代码执行在调用dialog.displayMessage()后不会暂停,因此getter返回这些字段的初始值,在所有情况下都为null。在onClick()的{​​{1}}方法中设置这些字段之后,您应该创建一个Activity实现的接口以接收回调,然后更新Activity的变量。

在对话框类中,我们创建一个接口。例如:

CustomListener

请务必保存对上下文的引用:

public class MyMessageDialog {
    public interface DialogCallback {
        public void onValuesSet();
    }
    ...
}

public AlertDialog displayMessage(Context context, String title, String message){ this.context = context; ... } 方法结束时,在设置字段值之后:

onClick()

Activity需要实现我们创建的接口,对话框应该是一个类成员,并且字段将在接口的回调方法中设置:

((DialogCallback) context).onValuesSet();

答案 1 :(得分:2)

  

有谁知道为什么输出的值是空的?

因为从对话框类获取数据的所有语句都在菜单选项部分dialog.displayMessage之后执行。

  

如何在MyMessageDialog按钮上点击Ok获取数据?

1。而不是从MyMessageDialog获取EditText,而是将所有getter / setter方法的返回类型更改为String ..

2. 使用interface创建一个事件监听器,以便在活动中点击Ok按钮时关闭警报事件。您可以创建事件监听器:

Android Custom Event Listener

3. Ok按钮上点击“警报”调用所有setter方法。在调用所有setter方法调用事件监听器方法以在Alert完成后在Activity中执行事件。:

      @Override
        public void onClick(DialogInterface dialog, int which) {
             setShipText(shipText.getText().toString());
             setScientistNameText(shipText.getText().toString());
             ....
             //..close alert and call event listener method
        }

答案 2 :(得分:1)

您的getters似乎实际上并未在每个EditText视图上调用getText()方法。修改它们(例如,scientistNameText.getText())。

PS而且,是的,Mike M可能确实写过有关问题的异步性。另一种解决方法是在每个EditText视图的代码中添加类似的内容:

myEditTextView.addTextChangedListener(new TextWatcher() {

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub

            //Set the variable here that you call in your getter. Use `getText()` to get the string (e.g., myGetterVariable = myEditTextView.getText().


        }
    }