Skip to content Skip to sidebar Skip to footer

Passing View To Asynctask To Access Findviewbyid

I am new to Android programming, and have one question. I am trying to access findViewById in my AsyncTask, but obviously, by default this will not be available, because I am not p

Solution 1:

You can do something like this. You can change information in the onPre, doInBackground, onPost --> This wouldnt matter much.

publicclassMainActivityextendsAppCompatActivity {

    TestView textView;

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.textView);

    }


    privateclassASyncCallextendsAsyncTask<Void, Void, Void> {
        @OverrideprotectedvoidonPreExecute() {
            super.onPreExecute();
            //What happens BEFORE everything in the background.
        }

        @OverrideprotectedVoiddoInBackground(Void... arg0) {
            textView.setText("set the text");
            returnnull;
        }
        @OverrideprotectedvoidonPostExecute(Void result) {
            super.onPostExecute(result);

            //After you get everything you need from the JSON.

        }
    }
}

Solution 2:

I really did not like the idea of passing context, and the View around to different objects, and like to keep the view specific functionality within the activity class itself, so I implemented an interface, that I passed around, as follow:

Here is my interface:

publicinterfaceIProfiler {
    voidShowProgressbar();
    voidHideProgressbar();
    voidMakeToast();
}

My activity class implements this interface, as follow:

publicclassProfileActivityextendsMenuActivityimplementsIProfiler {

private ProgressBar mProgressar;

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActivityProfileBindingbinding= DataBindingUtil.setContentView(this, R.layout.activity_profile);
    binding.setUser(newUser());
    binding.setViewActions(newProfileViewModel(this));


    //get the toolbarToolbartb= (Toolbar)findViewById(R.id.toolbarMain);
    setSupportActionBar(tb);

    //set the Progressbar
    mProgressar = (ProgressBar)findViewById(R.id.progressPostUser);
}

@OverridepublicvoidShowProgressbar() {
    mProgressar.setVisibility(View.VISIBLE);
}

@OverridepublicvoidHideProgressbar() {
    mProgressar.setVisibility(View.GONE);
}

@OverridepublicvoidMakeToast() {
    Toast.makeText(this, "Some toast", Toast.LENGTH_SHORT);
}
}

My ProfileViewModel, which excepts the interface as parameter:

publicclassProfileViewModel {

    private User mUser;
    private IProfiler mProfiler;

    publicProfileViewModel(IProfiler profiler){
        mProfiler = profiler;
    }

    publicvoidonSaveClicked(User user) {
        try {
            StringnameTest= user.get_name();
            StringsurnameTest= user.get_surname();

            newAsyncTaskPost(mProfiler).execute(newURL("http://www.Trackme.com"));
        }
        catch (Exception ex) {

        }
    }
}

And then finally, my AsyncTaskPost.

publicclassAsyncTaskPostextendsAsyncTask<URL, Void, Void> {

private IProfiler mProfilerActions;

publicAsyncTaskPost(IProfiler profilerActions){
    mProfilerActions = profilerActions;
}

@OverrideprotectedvoidonPreExecute() {
    super.onPreExecute();
    mProfilerActions.ShowProgressbar();
}

@OverrideprotectedVoiddoInBackground(URL... urls) {
    try{
        Thread.sleep(5000);
        returnnull;
    }
    catch (Exception ex) {
        returnnull;
    }
}

@OverrideprotectedvoidonPostExecute(Void aVoid) {
    mProfilerActions.HideProgressbar();
    mProfilerActions.MakeToast();
}

@OverrideprotectedvoidonCancelled() {

    super.onCancelled();
    mProfilerActions.HideProgressbar();
}
}

Post a Comment for "Passing View To Asynctask To Access Findviewbyid"