从dialogfragment更新片段

时间:2016-04-11 17:46:02

标签: android sqlite android-fragments

我在片段中使用来自SQLite数据库的textview文本。现在我想更新SQLite数据库时更新textview。我正在使用alertdialog来更新SQLite数据库。当我单击保存按钮然后更新数据库,但是在重新加载片段之前textview不会更新。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    db = new DatabaseHandler(getActivity());
    user = db.getUserDetails();
    setHasOptionsMenu(true);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflating view layout
    View v = inflater.inflate(R.layout.fragment_profile, container, false);

    proName = (TextView)v.findViewById(R.id.proName);
    proChangePassword = (Button)v.findViewById(R.id.proChangePassword);

    proName.setText(user.get("fname") + " " + user.get("lname"));

    return v;
}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.profile_edit:
            profileEdit();
            return false;
        default:
            break;
    }

    return false;
}


private void profileEdit() {
    final AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();
    final View dialogView = inflater.inflate(R.layout.profile_edit, null);
    dialogBuilder.setView(dialogView);

    eFirstName = (TextInputLayout) dialogView.findViewById(R.id.pEditFirst);
    eLastName = (TextInputLayout) dialogView.findViewById(R.id.pEditLast);
    final Button pEditSave = (Button) dialogView.findViewById(R.id.pEditSave);

    eFirstName.getEditText().setText(user.get("fname"));
    eLastName.getEditText().setText(user.get("lname"));

    dialogBuilder.setTitle("Profile Edit");
    final AlertDialog dialog = dialogBuilder.create();

    pEditSave.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String fname = eFirstName.getEditText().getText().toString();
            String lname = eLastName.getEditText().getText().toString();

            db.updateProfile(fname,lname);
            Toast.makeText(getActivity(), "Your Profile is successfully updated.", Toast.LENGTH_SHORT).show();

            dialog.dismiss();
        }
    });
    dialog.show();
}

如何在点击对话框的保存按钮时自动刷新片段?

2 个答案:

答案 0 :(得分:1)

使用接口概念可以在Dismiss of Custom Dialog Fragment

中重新加载活动中的片段
  

1.定义包含以下方法的接口

public interface IdialogDismissFragmentReload {
  public void onDismisClick();
  }
  

2. Fragment A extends Fragment implements IdialogDismissFragmentReload这是您要更新片段

的地方
 Fragment A extends Fragment implements IdialogDismissFragmentReload  
  interface 
   {
  ......
  @Override
  public void onClick(View v) {
  FragmentB mFragmentB = new FragmentB ();
  mFragmentB .setListener(this);
  mFragmentB.show(getActivity().getSupportFragmentManager(),PreviewDialog");
}

 @Override
 public void onDismisClick() {
  //  write code you want to
}
}
  

3.在解雇时,它会调用Fragment A onDismisClick()方法,您可以编写更新后的代码,也可以将所需数据从B传递给A

3.FragmentB extends DialogFragment
{
 private IdialogDismissFragmentReload mIReminderAdded;

public void setListener(IdialogDismissFragmentReload listener) {
    mIReminderAdded = listener;
  }

........

 @Override
 public void onClick(View v) {
 mIReminderAdded.onDismisClick();
 getDialog().dismiss();
}

}

答案 1 :(得分:0)

您可以使用GetInvoice onResume来读取数据库并更新UI。因此,将数据库查询和用户更新从Fragment移至onCreate

onResume

每当您关闭@Override public void onResume() { super.onResume(); db = new DatabaseHandler(getActivity()); user = db.getUserDetails(); } 时,系统都会调用Alert Dialog并更新数据。