简体   繁体   English

从片段更新UI

[英]Update UI from fragment

I have a fragment that gets data from a DialogFragment via intent with onActivityResult(...); 我有一个片段,通过onActivityResult(...)的意图从DialogFragment获取数据;

I am trying to update the fragment's UI but runOnUiThread does not work. 我正在尝试更新片段的UI,但runOnUiThread不起作用。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ArrayList<Farm> farms = data.getParcelableArrayListExtra("farms_list");
    float totalArea = 0;
    for(Farm farm : farms) {
        if (farm.isSelected()) {
            Log.d("farm", farm.getName());
            totalArea += farm.getArea();
        }
    }
    final float finalTotalArea = totalArea;
    activity.runOnUiThread(new Runnable() {
        @Override
        public void run() {
            area.setText(String.valueOf(finalTotalArea));
        }
    });
}

The area TextView does not update. TextView区域不会更新。

I get the reference to the activity with onAttach() method. 我使用onAttach()方法获取对活动的引用。

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (!(context instanceof PageFragmentCallbacks)) {
        throw new ClassCastException(
                "Activity must implement PageFragmentCallbacks");
    }

    mCallbacks = (PageFragmentCallbacks) context;
    this.activity = (Activity) context;
}

onActivityResult is being called on main thread. 在主线程上调用onActivityResult。 More here . 更多这里 For some reason views can't be edited from this method (probably not created or drawn). 由于某种原因,无法从此方法编辑视图(可能未创建或绘制)。 One trick that you can do is set var boolean updateTextView = false; 你可以做的一个技巧是设置var boolean updateTextView = false; On onActivityResult set it on true, save data that you got, and than onResume check updateTextView and if true set data to your TextView. on onActivityResult将其设置为true,保存您获得的数据,并且onResume检查updateTextView,如果为true,则将数据设置为TextView。

What about trying to wrap it in a proper thread that you launch afterwards? 尝试将其包装在之后启动的正确线程中怎么样?

new Thread() {
            public void run() {
                try {
                   activity.runOnUiThread(new Runnable() {
                       @Override
                       public void run() {
                          area.setText(String.valueOf(finalTotalArea));
                       }
                   });
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }.start();

Seems like your Activity is loosing your Fragment somewhere, what I did in this case was to add a Method to my Callbacks , maybe reattachDialog(DialogFragment dialogFragment) and calling it in onResume of the DialogFragment with reattachDialog(this) . 好像你的Activity在某个地方丢失你的Fragment ,我在这种情况下做的是为我的Callbacks添加一个方法,可能是reattachDialog(DialogFragment dialogFragment)并使用reattachDialog reattachDialog(this)在DialogFragment的onResume中调用它。

And in your Activity you do this: 在您的活动中,您执行此操作:

@Override
public void reattachFragment(DialogFragment dialogFragment) {
 this.dialogFragment = dialogFragment;
}

Or in Fragment: 或者在片段中:

@Override
public void reattachFragment(DialogFragment dialogFragment) {
 getActivity().setDialog(dialogFragment);
}

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

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