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

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

问题描述

我正在尝试使用片段来构建我的第一个合适的 Android 应用程序.我有一个主要的 xml.它由两个垂直片段组成,顶部片段仅由两个 TextView 组成.其中第一个包含静态文本,第二个包含一个值,我最终将从 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 那么它会很高兴地更新我的第一个 Fragment 中 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:

FragmentMainTop 类型的 findViewById(int) 方法未定义"

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

那为什么它不知道这个方法呢?我有 ctl/shift/o 所以我知道我有所有正确的进口.我不想最终将所有代码都放在 MainActivity 中,因为如果我想在另一个活动中使用 Fragment,我将不得不重复所有代码.

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() 方法.然后,在您更新完小部件后将其退回.

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

通常,当您在 Activity 中调用 findViewById() 时,您正在调用 Activity.findViewById().在 Fragment 类中尝试相同的调用将失败,因为没有这样的 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天全站免登陆