Android : How do I update my textView in a Fragment

38,537

Solution 1

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

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.

Solution 2

I am not an expert but I would say that return statement within a method returns something and the next lines after this statement are unreachable :O

So try to change the order! :)

Share:
38,537
RobT
Author by

RobT

I currently work in a large Multinational designing changes to a large BackEnd System running over Oracle. I've taken up Android programming out of interest and to add another string to my bow.

Updated on July 21, 2022

Comments

  • RobT
    RobT almost 2 years

    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.

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

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

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

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

    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.