Zombie
Zombie

Reputation: 1963

Dynamically adding and removing Views in AsyncTask

I am using AsyncTask to create a TableLayout and to remove. But I am not able to remove it. the table is adding successfully as needed. But I need to remove all the views when it is called next.

Where am I going wrong?

my code:

 private class asyncBrandName extends AsyncTask<String, Void, String> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if(count==1)
        {
            //((TableLayout)table1.getParent()).removeView(table1);
                container.removeView(table1);
        }
    }
    @Override
    protected String doInBackground(String... params) {
                    //mycode to create table
        return null;
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        container=(LinearLayout)findViewById(R.id.container);
        container.addView(table1);
    }
}

Upvotes: 0

Views: 793

Answers (1)

goofyz
goofyz

Reputation: 243

If there is nothing other than table1 in container, you can just called container.removeAllViews() to clear all child views. If not, You need to check the following:

  1. In onPreExecute(), make sure count == 1 will be true. Maybe count is 0, so that container.removeView() does not get called.
  2. Is the contianer point to the correct layout when you the asyncTask is executed? I notice you have set the container reference in onPostExecute().
  3. Is the table1 reference changed (Re-created or point to other view) before asyncTask is executed?

Upvotes: 1

Related Questions