-3

I try to set the text of a TextView programmatically within a Fragment. The code for the classes is as follows:

public abstract class AbstractFragment extends Fragment
{
    @Override
    protected void onActivityResult(int requestCode, int resultCode, final Intent data)
    {
       this.setFileName();
    }

    protected abstract void setFileName();
}

public class ImplementingFragment extends AbstractFragment
{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {
        View fragment = inflater.inflate(
                R.layout.fragment_layout, container, false);

        return fragment;
    }

    @Override
    protected void setFileName()
    {
        String fileName = "Test.txt";

        TextView textView = ((TextView) getActivity().findViewById(
                R.id.text_file_name));
        textView.setText(fileName);
    }
}

The Layout is as follows:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/text_pdf_filename_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/text_pdf_filename"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/text_pdf_file_name"
        android:layout_marginLeft="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

The weird thing is: within one Fragment it works, in another it does not (same parent Activity). Another fact is that after I set the text, I get it via textView.getText(). If I try to get the text in later code, I just get an empty string.

Additionally, if I debug the code, I see the text a view milliseconds, before it disappears.

Does anyone has an solution how to fix that behaviour?

der_chirurg
  • 1,475
  • 2
  • 16
  • 26

1 Answers1

-1

Inside fragment use:

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

    TextView textView = ((TextView) rootView.findViewById(
            R.id.text_file_name));
    textView.setText(fileName);

    return rootView;
}
Zoran
  • 1,484
  • 1
  • 10
  • 13