使用Intent数据并在android中完成

时间:2011-07-19 17:48:05

标签: android onclick android-intent

我正在创建一个应用程序,以便用户在Activity中,并打开AlertDialog。

alertDialog内部是一个按钮,用于打开第二个活动,等待结果并填写alertDialog中的文本字段。

好吧,至少它应该是。

为了更好地说明问题,请注意一下:

private String mFillAddrField = "";
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if(data == null) {
            ...
        } else {
            Bundle b = data.getExtras(); //DATA retrieved from second activity successfully
            mFillAddrField = b.getString("address");
        }
    }
}

...
public void displayAddScreen() {
    AlertDialog.Builder alert = new AlertDialog.Builder(this);

    alert.setTitle("Create a new Grocery Store");
    alert.setMessage("Create a new store by filling out the form below.");

    LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.newstore_dialog, (ViewGroup) findViewById(R.id.nsd_root));

    final EditText nameField = (EditText) layout.findViewById(R.id.nsd_et_name);
    final EditText addrField = (EditText) layout.findViewById(R.id.nsd_et_address);
    final Button addrSelect = (Button) layout.findViewById(R.id.nsd_b_address);

    alert.setView(layout);

    addrSelect.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            //Open MAPS API return me an address
            //Save address to text field
            Intent i = new Intent(getApplicationContext(), GroceryMapActivity.class);
            startActivityForResult(i,0);
            addrField.setText(mFillAddrField); //OFFENDING CODE HERE
        }
    });

我的问题是对addrField.setText的调用是在活动完成之前(可能在它启动之前!),我怎么能在这里等到活动返回结果,然后继续其余的代码(设置文本)?

2 个答案:

答案 0 :(得分:2)

将addrField.setText()移动到onActivityResult()方法

(你不能以你想要的方式在UI线程中停下来等待)

我已经更改了提供的代码,使addrField成为一个类成员,并将其设置为与您想要的相匹配:

private String mFillAddrField = "";
EditText addrField;
...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        if(data == null) {
            ...
        } else {
            Bundle b = data.getExtras(); //DATA retrieved from second activity successfully
            mFillAddrField = b.getString("address");
            addrField.setText(mFillAddrField);
        }
    }
}

...
public void displayAddScreen() {
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Create a new Grocery Store");
alert.setMessage("Create a new store by filling out the form below.");

LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newstore_dialog, (ViewGroup) findViewById(R.id.nsd_root));

final EditText nameField = (EditText) layout.findViewById(R.id.nsd_et_name);
addrField = (EditText) layout.findViewById(R.id.nsd_et_address);

final Button addrSelect = (Button) layout.findViewById(R.id.nsd_b_address);

alert.setView(layout);

addrSelect.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        //Open MAPS API return me an address
        //Save address to text field
        Intent i = new Intent(getApplicationContext(), GroceryMapActivity.class);
        startActivityForResult(i,0);
    }
});

答案 1 :(得分:1)

构建AlertDialog时,请在初始化后立即致电addrField.setText(mFillAddrField)。第一次构建对话框时,addrField将为空。 onActivityResult()完成后,地址将显示在mFillAddrField中,当您重建对话框时,您可以将addrField设置为该地址。