Jumpa
Jumpa

Reputation: 4409

Remove Child View From Fragment

I'm on Android 2.3.3 and I'm trying to remove programmatically a child view node from its container. I'm inside a fragment. The method removeView doesn't seem to work... The method getCategories is an async http task that return objects result (I don't think is useful here, but in case I can post the code):

Fragment

public class CategoriesFragment extends ListFragment {

    private CategoriesAdapter adapter;
    private CategorySelectedListener listener;
    private DataHelper dataHelper;
    private List<Category> categories;
    private List<Category> subCategories;
    private Parcelable listState;
    private LayoutInflater inflater;
    private ViewGroup container;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        this.inflater = inflater;
        this.container = container;

        View view = inflater.inflate(R.layout.fragment_list_categories,
                container, false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {

        super.onActivityCreated(savedInstanceState);
        dataHelper = new DataHelper(new LoadDataTaskCompletedListener());

        inflater.inflate(R.layout.progress_bar, container, true); // ADD CIRCLE PROGRESS BAR INSIDE CONTAINER
        dataHelper.getCategories();
    }

    private void setCategories(List<Category> categories) {
        if (isAdded()) {
            container.removeView(getActivity().findViewById(R.id.progressBar)); // NOT WORKING
            if (categories != null) {
                adapter = new CategoriesAdapter(getActivity(),
                        R.layout.row_category, categories);

                setListAdapter(adapter);
            }
        }
    }

    public class LoadDataTaskCompletedListener implements
            TaskCompletedListener<Object> {

        @Override
        public void onTaskCompleted(Object result) {
            if (result != null) {
                Bundle bundle = (Bundle) result;

                if (bundle.containsKey("categories")) {

                    categories = Arrays.asList((Category[]) bundle
                            .getParcelableArray("categories"));

                    setCategories(categories);
                }
            } else {
                setCategories(null);
            }
        }
    }
}

I'm inflating this progress bar inside root container:

Progress bar

<?xml version="1.0" encoding="utf-8"?>
<ProgressBar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/progressBar"
    style="Widget.ProgressBar.Large.Inverse"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" >

</ProgressBar>

I'd like to avoid show/hide xml node. Where am I wrong? My call is inside UI thread? Should I call invalidate or postInvalidate? Many thanks.

EDIT: something strange: if I add the following line:

container.removeView(container.findViewById(R.id.progressBar));

inside onTaskCompleted AND inside setCategories() (so twice), the progress bar is being correctly removed.

Upvotes: 0

Views: 2487

Answers (2)

Nathua
Nathua

Reputation: 8826

instead of

getActivity().findViewById

you should use

container.findViewById

Upvotes: 1

slhddn
slhddn

Reputation: 1997

Try this:

getActivity().findViewById(R.id.progressBar).setVisibility(View.GONE);

Upvotes: 0

Related Questions