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

时间:2018-02-25 08:58:50

标签: java android fragment

First 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;
    }

Fragment FragmentB

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

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

1 个答案:

答案 0 :(得分:0)

虽然有很多其他方法可以做到这一点但是特定的图书馆对我来说很合适: Event bus

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

它还具有交付线程等额外功能,而且速度快且相对较轻。我希望这有帮助..快乐编码!