0

I have one activity that holds multiple fragments. Each fragment implements my UpdateRequest interface, where each of these fragments do some asynctask and downloads data from web service. My problem is that I don't really understand how to update existing fragments. I read this

Android Refreshing Fragment View after AsyncTask

but I still can't figure out how to locate views of each fragment and then update them. I'll give an example:

There's an UserFragment, which should represents user profile:

public class UserFragment extends SherlockFragment implements
        UpdateRequest {

    View rootView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_user_info,
                container, false);
        return rootView;
    }
    ...
    public void update(final OnFragmentUpdatedListener context) {
    ...
    asyntask
     ...
    }

}

And fragment_user_info looks like this:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >



    <ImageView
        android:id="@+id/fragment_user_picture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/image_of_user" />

    <TextView
        android:id="@+id/fragment_user_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/user_info" />

</RelativeLayout>

What I want is to locate instance of UserFragment and then in its AsyncTask.onPostExecute() method change text in fragment_user_name textView to something that asynctask returns.

Community
  • 1
  • 1
vanomart
  • 1,739
  • 1
  • 18
  • 39

1 Answers1

0

The easiest way to do this is to create a final variable in the fragment to hold a reference to the TextView. Then the AsyncTask, being an anonymous inner class of the fragment, will have direct access to it. So, something like this:

public class UserFragment extends SherlockFragment implements UpdateRequest {

private View rootView;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    rootView = inflater.inflate(R.layout.fragment_user_info, container, false);
    return rootView;
}
...
public void update(final OnFragmentUpdatedListener context) {
    final TextView userNameView = (TextView) rootView.findViewById(R.id.fragment_user_name);
    ...
    asyntask
    ...
    onPostExecute() {
        userNameView.setText(....);
    }

}
GreyBeardedGeek
  • 29,460
  • 2
  • 47
  • 67
  • findByID() should be findViewById(int) which will return a reference to the TextView you want to update. OP should be able to call findViewById() from onPostExecute(). @GreyBeardedGeek, if you fix your answer, I'll upvote it. – mttdbrd Nov 24 '13 at 02:17
  • I was thinking about this too solution , but how can I be sure that onCreateView is called before update? Then rootView will be null a we all know how this will end. – vanomart Nov 24 '13 at 10:39
  • Ok, I finally found suitable solution. I did it as you suggested, but this returned NullPointerException becouse that update() was called before onCreateView as I expected. So I moved the "final TextView userNameView = (TextView) rootView.findViewById(R.id.fragment_user_name);" code to onPostCreate and now it's working just fine. – vanomart Nov 24 '13 at 17:51