0

我想将数据从一个对话框传递到另一个对话框。我有一个 onCreateContextMenu 对话框来显示列表视图上的选项

代码:

   @Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();

    switch (item.getItemId()) {

    case R.id.update_item:

        Object o1 = lv1.getItemAtPosition(info.position);
        ItemDetails obj_itemDetails1 = (ItemDetails) o1;
        /*Toast.makeText(getApplicationContext(),
                "You have chosen : " + " " + obj_itemDetails1.getId(),
                Toast.LENGTH_LONG).show();  */
        Bundle arg= new Bundle();
        arg.putString("key", obj_itemDetails1.getId());
        showDialog(DIALOG_ID, arg);
        return true;

    }
    return false;
}

我正在使用 Bundle 将此对话框中的数据发送到警报对话框。

代码:

   protected final Dialog onCreateDialog(final int id, Bundle arg) {
    Dialog dialog = null;
    switch (id) {
    case DIALOG_ID:
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        LayoutInflater inflater = this.getLayoutInflater();
              arg.getString("key");// getting same value here

        builder.setMessage(arg.getString("key"));
        builder.setPositiveButton("Cancel", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   dialog.cancel();
               }
           });
        builder.setNegativeButton("Update", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {

               }
           });    

        AlertDialog alert = builder.create();
        alert.setTitle("Update Info");
        dialog = alert;
        break;

    default:

    }
    return dialog;
}

 }

Bundle arg= new Bundle();
            arg.putString("key", obj_itemDetails1.getId());
            showDialog(DIALOG_ID, arg);

我每次都根据所选位置发送新数据

但在arg.getString("key");警报对话框中,我每次都得到相同的数据。即使我单击了新位置,数据也不会更新。

我用过arg.remove("key");arg.clear();但他们没有工作。

我怎样才能做到这一点。或任何其他在 AlertDialog 中传递数据的想法。

4

1 回答 1

0

You should not use the showDialog method as it's deprecated, also if you check the javadoc you'll see:

A call to onCreateDialog(int, Bundle) will be made with the same id the first time this is called for a given id.

So you can't update that value like that.

--

Using DialogFragment you could just search the Dialog (or create it if it doesn't exists) and pass the new parameter through a method:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
YourDialog d = YourDialog.newInstance(...);
d.show(ft, YourDialog.class.getSimpleName());

then search for it:

YourDialog d = (YourDialog) getSupportFragmentManager().findFragmentByTag(YourDialog.class.getSimpleName())
if (d == null) {
    //create a new dialog like before
}

d.setValue(yourValue)
于 2013-04-16T10:03:44.073 回答