我希望在删除对话框片段后刷新/重绘活动..我怎样才能实现这一点?

时间:2016-06-14 04:30:29

标签: android android-dialogfragment redraw

这是它首先看的方式:

The image description of the first image

这是"编辑"时弹出的对话框片段。按下,我想在对话框片段被解除后在活动中看到更改。 The image description of the second image

public Dialog onCreateDialog(Bundle savedInstanceState) {
        final View view = getActivity().getLayoutInflater().inflate(R.layout.edit_profile_dialog, new LinearLayout(getActivity()), false);
        editProfile = (EditText) view.findViewById(R.id.changeProfile);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        setupProgressDialog();
/*get value from Bundle*/
        String editValue = getArguments().getString("value", "");
        editProfile.setText(editValue);
        String title = getArguments().getString("title", "");
        builder.setTitle(title);
        builder.setView(view);
        builder.setCancelable(false);
        builder.setPositiveButton("Ok!", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
/*edit the value in shared preference*/
                sharedPref = getActivity().getSharedPreferences(getString(R.string.sharedPref), 0);
                editor = sharedPref.edit();
                editor.putString(getArguments().getString("saved", ""), editProfile.getText().toString());
                editor.apply();
               ID= sharedPref.getString("id", null);
                access_token=sharedPref.getString("token",null);
//Start of AsyncTask after this

3 个答案:

答案 0 :(得分:0)

在对话框的onClickListener中,您应该能够使布局无效并强制重绘/刷新

检查一下:How to force an entire layout View refresh?

答案 1 :(得分:0)

如果您只需要更新用户从对话框输入的数据,则无需重新绘制整个布局。

您只能将用户名设置为相关的textview并关闭对话框片段。

TextView yourNameTextView = (TextView)findViewById(R.id.your_textview);

public void setNameToTextView(String name){
    yourNameTextView.setText(name);
}

当用户点击“确定”按钮时,您可以拨打:

((YourActivity)getActivity).setText(input);
祝你好运。

答案 2 :(得分:0)

感谢你们所有人试图帮助我。我想我通过这样做得到了答案: 在我的DialogFragment

public class DialogFragmentEditProfile extends DialogFragment {
...
/*Initialize Parent Activity*/
private ChangeProfileActivity cp;
/*Override onAttachMethod */
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
        cp = (ChangeProfileActivity) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement FeedbackListener");
        }
    }
/*create a method to recreate the parent activity*/
public void onButtonPushed(View view) {
cp.recreate();
    }

然后onPostExecute()DialogFragment中的AsyncTask方法

  protected void onPostExecute(String result) {
            super.onPostExecute(result);
...
/*Recreate activity after successful update by calling the onButtonPushed() method*/
 onButtonPushed(getView());
}

}