繁体   English   中英

如何从另一个片段更新DialogFragment的Textview

[英]How to update Textview of an DialogFragment from another Fragment

第一个片段RequestDialog

当我通过同一类调用函数request_services_change时工作正常,但是当FragmentB调用为空指针并且在调试器上request_services为空时

 public void RequestDialog(Context context) {

        this.listener = listener;
    }


 public RequestDialog()
    {

    }

  public void request_services_change()
    {
                    Log.d("Text","Request Dailog First Fragment ");// this is working fine from called by same class or Fragment B
        request_services.setText("hello from test 2"); // is called by same class is working fine but when call by FragmentB is null pointer and request_services is empty on debuger 
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        this.view = inflater.inflate(R.layout.add_request_dailoge, container, true);
         request_services = (TextView) view.findViewById(R.id.services_request);

        return this.view;
    }

片段片段B

private RequestDialog listener;
public FragmentB () {
    this.listener=new RequestDialog();
}

public function Test()
{
   listener.View_request_services();
}

尽管还有很多其他方法可以执行此操作,但是特定的库对我来说效果很好: Event bus

事件总线促进并简化了活动,片段,线程,服务等之间的通信。在您的情况下,您可以使用此库来更新DialogFragment的TextView(甚至来自另一个片段)。

它还具有额外的功能,例如传送线,而且速度也相对较轻。 希望对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM