Birger
Birger

Reputation: 359

Update textview in fragment

This might be a very simple problem, but I can't seem to get it to work.

I'm trying to update a textview in a Fragment on "OnCreateView" My code looks like this

public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) {
     View v = inflater.inflate(R.layout.fragment_display_product, container, false);
    txtAge = (TextView)v.findViewById(R.id.txtAge);
    txtCountry = (TextView)v.findViewById(R.id.txtCountry);
    txtName = (TextView)v.findViewById(R.id.txtName); 
    txtAge.setText("Hallo world");
    return inflater.inflate(R.layout.fragment_display_product, container,false);
}

The text in textview txtAge never get set and I can't seem to find a reason. I'm new to android so sorry if this is a stupid question.

/Birger

Upvotes: 0

Views: 209

Answers (1)

amit singh
amit singh

Reputation: 1422

Do it like this-

public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{
    View v = inflater.inflate(R.layout.fragment_display_product, null);
    txtAge = (TextView)v.findViewById(R.id.txtAge);
    txtCountry = (TextView)v.findViewById(R.id.txtCountry);
    txtName = (TextView)v.findViewById(R.id.txtName); 
    txtAge.setText("Hallo world");
    return v;
}

Return the same view which you have inflated already.

Upvotes: 1

Related Questions