Android的:如何更新我的TextView的片段 [英] Android : How do I update my textView in a Fragment

查看:135
本文介绍了Android的:如何更新我的TextView的片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用片段来建立我的第一个适当的Andr​​oid应用程序。 我有一个主XML。它由两个垂直的碎片,上面的片段包括只有两个TextViews的。其中的第一个包含静态文本和第二包含一个值,我将最终被动态地从SQL获得

I am trying to use fragments to build my first proper Android App. I have a main xml. which consists of two vertical fragments, the top fragments consists of just two TextViews. The first of these contains static text and the second contains a value which I will eventually be getting dynamically from SQL.

如果我把这是我MainActivity.java然后兴高采烈地更新我的第一个片段的TextView的值:

If i put this is my MainActivity.java then it happily updates the value of the TextView in my first Fragment:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    // Set the Text to try this out
    TextView t = (TextView)findViewById(R.id.viewItem);
    t.setText("Text to Display");
}

我有两个片段,XML,并一个java相互支持,所以我希望把这个字段的设置到支持的片段,而不是Java可以为MainActivity了Java。

I have two fragment XMLs and a java to support each so I want to put the setting of this field into the Java that supports the fragment rather than the java that is for the MainActivity.

当我把它的片段,它看起来是这样的: -

When I put it in the fragment it looks like this:-

    public class FragmentMainTop extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState){
    // -- inflate the layout for this fragment
    return inflater.inflate(R.layout.fragmentmt, container,false);

    // Set the Text to try this out
    TextView t = (TextView)findViewById(R.id.viewItem);
    t.setText("Text to Display");
}

但是,如果我这样做,我得到的TextView的行错误:

But if I do this I get an error on the TextView line:

的方法findViewById(INT)是未定义的类型FragmentMainTop

"The method findViewById(int) is undefined for the type FragmentMainTop"

那么,为什么它不知道这个方法?我有CTL /移位/ O,所以我知道我拥有所有正确的进口。 我不希望最终把我所有的code在MainActivity,因为如果我想要再使用片段中的另一个活动,我将不得不重复所有code。

So why does it not know this method? I have ctl/shift/o so I know I have all the right imports. I don't want to end up putting all my code in the MainActivity because if I want to then use the Fragment in another activity I will have to repeat all the code.

推荐答案

您需要充气段布局分配给一个变​​量,这样就可以访问它 findViewById()方法。然后你回吧,一旦你完成更新你的widget。

You need to assign the inflated fragment layout to a variable so that you can access its findViewById() method. Then you return it once you're done updating your widgets.

@Override
public View onCreateView(LayoutInflater inflater,
        ViewGroup container, Bundle savedInstanceState){
    // -- inflate the layout for this fragment
    View myInflatedView = inflater.inflate(R.layout.fragmentmt, container,false);

    // Set the Text to try this out
    TextView t = (TextView) myInflatedView.findViewById(R.id.viewItem);
    t.setText("Text to Display");

    return myInflatedView;
}

通常情况下,当你调用 findViewById()活动,你调用在 Activity.findViewById()。尝试在你的片段类相同的呼叫会失败,因为没有这样的 Fragment.findViewById()方法。所以,你必须使用 View.findViewById()您的膨胀,以获得引用您的小工具。

Normally, when you call findViewById() within an Activity, you're invoking Activity.findViewById(). Trying the same call within your Fragment class will fail because there is no such Fragment.findViewById() method. So, you must use View.findViewById() with your inflated view to obtain references to your widgets.

这篇关于Android的:如何更新我的TextView的片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆