我有一个显示联系人列表的ListView。单击某个联系人时,会创建一个DialogFragment,其中会显示是否取消或确认的通知。根据他们是单击取消还是确认我想要更改ListView项目的颜色。如何获取对话框的按钮单击状态。我的对话代码:
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
String first = this.getArguments().getString("first");
String last = this.getArguments().getString("last");
String phone = this.getArguments().getString("phone");
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.add_contact_dialog, null);
TextView tv = (TextView) view.findViewById(R.id.new_first);
tv.setText(first);
TextView tv2 = (TextView) view.findViewById(R.id.new_phone);
tv2.setText(phone);
TextView tv3 = (TextView) view.findViewById(R.id.new_last);
tv3.setText(last);
builder.setView(view);
//builder.setTitle("Are you sure?").setMessage("Would you like to add " + name + " as a contact?");
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Add the contact
}
})
.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
现在我希望setPositiveButton或setNegativeButton返回一个我可以在我的主Activity中比较的值,或者在对话框的范围内获取我的ArrayAdapter,以便我可以对其进行更改。有没有办法将DialogFragment.show()
和被点击的对话框按钮联系起来?
答案 0 :(得分:2)
有没有办法将DialogFragment.show()和Dialog联系起来 点击的按钮?
使用接口将该信息传递给活动,活动又将其传递给可以进行更改的适配器/片段:
public interface OnSelectionMade {
int OK = 1000;
int CANCEL = 2000;
doChange(int result);
}
使活动实现此接口并使用doChange()
回调在适配器中进行更改,方法是直接访问适配器(如果使用简单的ListView
)或将其传递给片段持ListView
进行更改:
public class YourActivity extends Activity implements OnSelectionMade {
@Override
public void doChange(int result) {
// the result parameter will be either the OK or CANCEL constants
// you know now what button was clicked so you can do the change
...
}
}
现在您需要连接DialogFragment
以通过OnSelectionMade
传递事件,如下所示:
private OnSelectionMade mListener;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mListener = (OnSelectionMade) activity; // you'd want to implement a cast check here to be safe
}
//then in the onCreateDialog() method
builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.doChange(OnSelectionMade.OK); // do a null mListener check?
}
})
.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.doChange(OnSelectionMade.CANCEL);
}
});
您可以直接传递对DialogFragment
的引用以传递结果,但这不是一个长期非常好的解决方案。
答案 1 :(得分:0)
Activity
实例化的Fragment
或DialogFragment
中,首先声明:
public static final int MY_DIALOGFRAGMENT = 123; /* Any random int values will do. */
public static final int CONTACT_CONFIRM = 124;
public static final int CONTACT_DENY = 125;
实例化你的DialogFragment
:
FragmentManager fm = getSupportFragmentManager();
MyDialogFragment myDialogFragment = new MyDialogFragment();
myDialogFragment.setTargetFragment(this, MY_DIALOGFRAGMENT);
myDialogFragment.show(fm, "my_dialog_fragment");
使用onCreateDialog()
的{{1}}方法,
DialogFragment
最后,将以下方法添加到builder.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Add the contact
getTargetFragment().onActivityResult(getTargetRequestCode(),
MyActivity.CONTACT_CONFIRM, getActivity().getIntent());
dismiss();
}
})
.setNegativeButton("Deny", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
getTargetFragment().onActivityResult(getTargetRequestCode(),
MyActivity.CONTACT_DENY, getActivity().getIntent());
dismiss();
}
});
或Activity
:
Fragment
这应该有效。试试吧。