通过普通 java 类更新 ViewPager 中的 Textview

发布于 2025-01-07 02:23:08 字数 843 浏览 5 评论 0原文

我使用兼容包中的 ViewPager 来显示页面片段。 我想要一个后台服务来为片段提供数据。因此,我使用“普通java类”来更新(例如)这样一个片段中的TextView:

public class Updater {

 private Activity mActivity=null;
 private TextView tv = null;
 private Context mContext; 
 private View mInflatedMenu =null;

 public Updater (Activity _activity, Context _context, View inflatedMenu) { 
    mActivity = _activity;
    mContext = _context;
    mInflatedMenu = inflatedMenu;
 }

 public void updateTextView (String _text) {
    tv = (TextView) mInflatedMenu.findViewById(R.id.tvFragment1Updater);
    tv.setText(_text);
    Toast.makeText(mContext, tv.getText(), Toast.LENGTH_SHORT).show();
 }      

}

Toast说,TextView具有参数_text的值。没关系!
问题:在屏幕上,TextView 的原始值尚未更改为 _text

我想我已经尝试过各种invalidate()。我做错了什么?您将如何更新 ViewPager 中的片段“视图”?

I use the ViewPager from the compatibility pack to page fragments.
I want to have a background service which serves the fragments with data. Therefore I use a "normal java class" to update (for example) a TextView in such a fragment:

public class Updater {

 private Activity mActivity=null;
 private TextView tv = null;
 private Context mContext; 
 private View mInflatedMenu =null;

 public Updater (Activity _activity, Context _context, View inflatedMenu) { 
    mActivity = _activity;
    mContext = _context;
    mInflatedMenu = inflatedMenu;
 }

 public void updateTextView (String _text) {
    tv = (TextView) mInflatedMenu.findViewById(R.id.tvFragment1Updater);
    tv.setText(_text);
    Toast.makeText(mContext, tv.getText(), Toast.LENGTH_SHORT).show();
 }      

}

The Toast says, that the TextView has the value of the parameter _text. That's okay!
The problem: On the screen the original value of the TextView hasn't changed to _text.

I think I've tried all kinds of invalidate(). What I'm doing wrong? How would you update your fragment "views" in a ViewPager?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

韬韬不绝 2025-01-14 02:23:08

您需要保存toast实例,当您更改_text的vlaue时,您需要隐藏旧的toast并创建一个新的toast。

You need to save the toast instance, when you are changing the vlaue of _text, you neen to hide the old toast and create a new one.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文