我有一个ViewPager活动,viewPager上的每个Fragment打开一个DailogFragment,我可以在其中插入一个对象,当我点击Accept按钮时,DialogFragmnent解除,而父Fragment必须将信息添加到Layout。我使用接口与片段进行通信,但我无法更新片段上的布局。我尝试了一些没有好结果的解决方案。 代码如下:
public class DialogFragmentProducts extends DialogFragment {
View view;
Picking picking;
PackOperation pack, packOp;
Holder holder;
byte index;
private static final int ZBAR_SCANNER_REQUEST = 0;
private Fragment fragment;
public DialogFragmentProducts(){
}
public static DialogFragmentProducts newInstance(String title, Long id, Long idProd) {
DialogFragmentProducts frag = new DialogFragmentProducts();
Bundle args = new Bundle();
args.putString("title", title);
args.putLong("picking", id);
if (idProd!= null)
args.putLong("producto", idProd);
else
args.putLong("producto", -1);
frag.setArguments(args);
return frag;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
super.onCreateDialog(savedInstanceState);
fragment = getTargetFragment();
String title = getArguments().getString("title");
picking = Model.load(Picking.class, getArguments().getLong("picking"));
Long test = getArguments().getLong("producto");
if (getArguments().getLong("producto")!=-1) {
packOp = ListService.getPackOperationbyProduct(String.valueOf(getArguments().getLong("producto"))).get(0);
}
LayoutInflater inflater = getActivity().getLayoutInflater();
view = inflater.inflate(R.layout.dialog_fragment_product, null);
Inicialize();
fragment = getTargetFragment();
return new AlertDialog.Builder(getActivity())
.setTitle(title)
.setView(view)
.setPositiveButton(this.getString(R.string.accept_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
try{
pack.setProductQty(Double.parseDouble((String) qty.getText().toString()));
pack.setPackage(holder.getPackFuente());
pack.setSync(false);
sendBackResult();
((TransferPickingActivityProduct)getTargetFragment()).onResume();
}
catch (Exception e){
Toast.makeText(getContext(), getResources().getString(R.string.data_entry), Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton(this.getString(R.string.cancel_button),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
((TransferPickingActivityProduct)getTargetFragment())
.doNegativeClick();
dismiss();
}
} ).create();
}
public interface DFragmentListener {
void onFinishEditDialog(PackOperation packOperation);
}
public void sendBackResult() {
DFragmentListener listener = (DFragmentListener) getTargetFragment();
listener.onFinishEditDialog(pack);
dismiss();
}
public void launchScanner(View view) {
try {
if (isCameraAvailable()) {
Intent intent = new Intent(DialogFragmentProducts.this.getContext(), ZBarScannerActivity.class);
startActivityForResult(intent, ZBAR_SCANNER_REQUEST);
(intent.getStringExtra(ZBarConstants.SCAN_RESULT).equals(null));
} else {
Toast.makeText(this.getContext(), this.getString(R.string.available_camera), Toast.LENGTH_SHORT).show();
}
} catch (Exception e){
Toast.makeText(this.getContext(), this.getString(R.string.read_barcode_error), Toast.LENGTH_SHORT).show();
}
}
public boolean isCameraAvailable() {
PackageManager pm = DialogFragmentProducts.this.getActivity().getPackageManager();
return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case ZBAR_SCANNER_REQUEST:
if (resultCode == getActivity().RESULT_OK) {
String ean13 = data.getStringExtra(ZBarConstants.SCAN_RESULT);
Fragment f = getTargetFragment();
}
break;
}
}
@Override
public void onSaveInstanceState(final Bundle outState){
this.setTargetFragment(this, -1);
}
@Override
public void onDismiss(final DialogInterface dialog) {
super.onDismiss(dialog);
setTargetFragment(getTargetFragment(), 1);
Fragment parentFragment = getTargetFragment();
if (parentFragment instanceof DialogInterface.OnDismissListener) {
((DialogInterface.OnDismissListener) parentFragment).onDismiss(dialog);
}
}
}
和DialogFragmentProduct:
{{1}}
答案 0 :(得分:0)
首先,你应该检查你是否真的从父片段的对话框中获取数据 简单方法Log.d(“我的调试”,......)
如果没有看到下面的例子
从片段
调用对话框 DialogFragment fragment = new DialogFragment ;
fragment.setArguments(args);
fragment.setTargetFragment(this, REQ_FLAG);
fragment.show(getFragmentManager(), fragment.getClass().getName());
在关闭对话框片段
后获取回调和数据 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
.....
} else if (resultCode == Activity.RESULT_CANCELED){
.....
String string = data.getStringExtra("string");
}
在对话框片段
中builder.setView(view).setNegativeButton(R.string.btnDel,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.putExtra("string", " Hi ");
getTargetFragment().onActivityResult(getTargetRequestCode(),
Activity.RESULT_CANCELED, intent);
}
});
之后您应该记住ViewPager在开始时创建所有片段,您应该手动刷新片段元素