Android DialogFragment Update Views Dynamically setText

2.3k views Asked by At

I know there are numerous posts with similar titles, but I've been searching and experimenting for hours now, and I haven't found anything that helps me, so I'm asking for help before my head explodes with frustration.

I have a dialog fragment that inflates from an xml file, but I need to update the TextView text values dynamically from NFC tags. I cannot, for the life of me, find out how to get a TextView object so I can update it. In the onCreate method, it's easy, but anywhere else I try it, I get a null pointer exception, because apparently the ID doesn't exist in that layout.

Here is the applicable fragment code:

View rootView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.foo_bar, container,
            false);

    //Eliminate the title frame
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    return rootView;

Inside the oncreate method I can easily and freely use findViewById to manipulate my values. However, even though rootView is a class-wide variable, in other methods in the class, I am unable to use the exact same method (??) to accomplish this.

I have tried many other methods, all to no avail. All I need to accomplish is either in the Activity class, or in the fragment class, update the TextView text after it's been created.

Here is my main activity class, method for creating this fragment:

public void showLogDialog( String title ){
        LogWorkOut updateDialog = new LogWorkOut();
        updateDialog.show(fm, "Update2Metrics");
        updateDialog.setHeader(title);
    }

I would like to be able to use a setter, like shown here (setHeader) to set it. Alternatively, if I can actually get to the view that contains the id that was inflated, I could do it directly. By all rights, updateDialog itself should be that view, since rootView was returned. It makes no sense.

Can you sense my hatred for Java, yet?

Any hints or help would be a life saver. I might send you flowers. If you like flowers.

Edit: Since it's been asked twice now, this is how I'm trying to update my TextView (but I don't really care, as long as it's not within onCreateView, because I need to do it after it's been created.)

TextView headerView = (TextView) rootView.findViewById(R.id.header_view);

I've tried various different forms in desperation, including trying various forms of getView, in both the fragment and in the main activity. Nothing works, and it makes no sense why using rootView only works in the onCreate method.

Edit2: Since two people have offered this as a solution, setting a TextView to an class object doesn't work. I still get a NullPointerException if I try to use setText() outside of onCreateView. Examples below.

public class LogWorkOut extends DialogFragment {

//Class variables and objects
FragmentManager fm = getFragmentManager();

TextView headerView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.log_workout_2, container,
            false);

    //Eliminate the title frame
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);

    headerView = (TextView) rootView.findViewById(R.id.workout_title);
    headerView.setText("Test");
    return rootView;
}

Above, inside the onCreateView method, using setText works just fine. If I then try to update it in another method, as below, I get a null pointer exception:

    public void setHeader( String header) {
    headerView.setText(header);
}
1

There are 1 answers

2
jmateo On

Get a reference to the textview in onCreateView:

TextView mTextView;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.foo_bar, container,
            false);

    //Eliminate the title frame
    getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
    mTextView = (TextView) rootView.findViewById(R.id.header_view);
    return rootView;
}

Then you can use it in any method called after onCreateView

private void setTextViewContent(String text)
{
    mTextView.setText(text);
}

Edit: If your fragment is not attached to any activity, your view will be null. If there's any possibility of your code running when the fragment is not attached to an activity, you should call isAdded() before you try to manipulate any of the views.