使用Bundle将数据从活动发送到片段

时间:2018-06-20 21:05:06

标签: java android android-studio android-fragments dialog

我正在尝试使用Bundle将数据从活动发送到片段。当用户单击操作栏上的添加图标时,活动正在从对话框接收输入。该按钮还打开对话框,但它将数据直接发送到片段(我正在尝试了解活动和片段之间的区别,并与对话框片段进行交互)。互联网上没有任何解决方案对我有用,我希望有人可以提供帮助

我提供了可视化帮助您对此问题进行解释。因此,最初,我单击动作添加图标以打开对话框(第二张图片),当我输入一个输入时,它不会更改片段上的数据。仅当我第二次按下操作添加图标时,第一个输入才会更新(第3张图片)。您还可能会注意到它说“捆绑{[对话框输入=第一输入]}”,其中“第一输入”是用户输入。如何将其更改为“仅第一输入”。我尝试在设置值之前清除textview,但这不起作用。现在最后,当我按下按钮时,它将打开对话框,当我输入数据时,来自动作添加图标的数据(在活动中进行处理,然后将数据发送到片段)与按钮中的数据(直接发送到片段的数据)重叠。任何帮助,将不胜感激。预先感谢。

enter image description here

MainActivity:

public class MainActivity extends AppCompatActivity implements 
MyCustomDialog.OnInputSelected{

public String dialogInput;
FragmentManager fragmentManager;

@Override
public void sendInput(String input) {
    dialogInput = input;
}

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

    fragmentManager = getSupportFragmentManager();
}

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

    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    //Handle action bar clicks here. The action bar will automatically handle clicks on the home/up button
    //so long as you specify a parent activity in AndroidManifest.xml

    switch(item.getItemId()){

        case R.id.action_add:
            MyCustomDialog dialog = new MyCustomDialog();
            dialog.show(getSupportFragmentManager(), "MyCustomDialog");

            //Trying Bundle to pass data, dialog input between activity and fragment
            Bundle bundle = new Bundle();
            bundle.putString("Dialog Input", dialogInput);
            //Set Fragment class arguments
            MainFragment fragment = new MainFragment();
            fragment.setArguments(bundle); //set argument bundle to fragment

            fragmentManager.beginTransaction().replace(R.id.MainFragment,fragment).commit(); //now replace Mainfragment


            Toast.makeText(this, "Action_Add Clicked Successfully", Toast.LENGTH_SHORT).show();
    }

    return super.onOptionsItemSelected(item);
}
}

MainFragment:

public class MainFragment extends Fragment implements MyCustomDialog.OnInputSelected{

TextView InputDisplay;
Button OpenDialog;

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_main, container, false);

    InputDisplay = view.findViewById(R.id.InputDisplay);
    OpenDialog = view.findViewById(R.id.Open_Dialog);

    //Getting Main Activity dialog information with Bundle, that was received from toolbar add
    Bundle bundle = getArguments();
    if(bundle != null){
        String dialogInput = bundle.toString();
        //Clearing since Fragment call and activity call overlap each other.
        InputDisplay.setText("");
        InputDisplay.clearComposingText();
        InputDisplay.setText(dialogInput);
    }
    //String dialogInput = this.getArguments().getString("Dialog Input");

    OpenDialog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("MainFragment", "onClick: opening dialog");

            MyCustomDialog customDialog = new MyCustomDialog();
            customDialog.setTargetFragment(MainFragment.this, 1);
            customDialog.show(getFragmentManager(), "MyCustomDialog");
        }
    });

    return view;
}

@Override
public void sendInput(String input) {
    InputDisplay.setText("");
    InputDisplay.setText(input);
}
}

我的自定义对话框:

public class MyCustomDialog extends DialogFragment {

private EditText Input;
private TextView ActionOK, ActionCANCEL;

private OnInputSelected onInputSelected;

public interface OnInputSelected{
    void sendInput(String input);
}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        Fragment onInputSelected_fragment = getTargetFragment();
        Activity onInputSelected_activity = getActivity();
        if(onInputSelected_fragment != null){
            onInputSelected = (OnInputSelected) onInputSelected_fragment;
        }else{
            onInputSelected = (OnInputSelected) onInputSelected_activity;
        }
        //throw new RuntimeException("Custom Dialog onAttach Listener was NULL");
    }catch(ClassCastException e){
        Log.e("Custom Dialog", "onAttach: ClassCastException: " + e.getMessage());
    }
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.dialog_my_custom, container, false);

    Input = view.findViewById(R.id.Input);
    ActionOK = view.findViewById(R.id.Action_OK);
    ActionCANCEL = view.findViewById(R.id.Action_CANCEL);

    ActionCANCEL.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getDialog().dismiss();
        }
    });

    ActionOK.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            onInputSelected.sendInput(Input.getText().toString());

            getDialog().dismiss();
        }
    });

    return view;
}
}

1 个答案:

答案 0 :(得分:0)

  

如何将其更改为“仅第一输入”。

您的输出被打印为“捆绑包{[Dialog Input = First Input]}”,因为您直接进行bundle.toString(); ,而不是获取存储在捆绑包中的值。
将以上内容更改为此

String dialogInput = bundle.getString("Dialog Input")
InputDisplay.setText(dialogInput);
  

操作添加图标中的数据与按钮上的数据

重叠

在设置新值之前清除文本视图中的现有文本

String dialogInput = bundle.getString("Dialog Input")
InputDisplay.setText(");
InputDisplay.setText(dialogInput);

此外,我注意到您使用的所有变量名均不遵循驼峰大小写。我建议您也对此进行更正。