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.
public class MainActivity extends AppCompatActivity {
TestView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
}
private class ASyncCall extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
//What happens BEFORE everything in the background.
}
@Override
protected Void doInBackground(Void... arg0) {
textView.setText("set the text");
return null;
}
@Override
protected void onPostExecute(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:
public interface IProfiler {
void ShowProgressbar();
void HideProgressbar();
void MakeToast();
}
My activity class implements this interface, as follow:
public class ProfileActivity extends MenuActivity implements IProfiler {
private ProgressBar mProgressar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityProfileBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_profile);
binding.setUser(new User());
binding.setViewActions(new ProfileViewModel(this));
//get the toolbar
Toolbar tb = (Toolbar)findViewById(R.id.toolbarMain);
setSupportActionBar(tb);
//set the Progressbar
mProgressar = (ProgressBar)findViewById(R.id.progressPostUser);
}
@Override
public void ShowProgressbar() {
mProgressar.setVisibility(View.VISIBLE);
}
@Override
public void HideProgressbar() {
mProgressar.setVisibility(View.GONE);
}
@Override
public void MakeToast() {
Toast.makeText(this, "Some toast", Toast.LENGTH_SHORT);
}
}
My ProfileViewModel, which excepts the interface as parameter:
public class ProfileViewModel {
private User mUser;
private IProfiler mProfiler;
public ProfileViewModel(IProfiler profiler){
mProfiler = profiler;
}
public void onSaveClicked(User user) {
try {
String nameTest = user.get_name();
String surnameTest = user.get_surname();
new AsyncTaskPost(mProfiler).execute(new URL("http://www.Trackme.com"));
}
catch (Exception ex) {
}
}
}
And then finally, my AsyncTaskPost.
public class AsyncTaskPost extends AsyncTask<URL, Void, Void> {
private IProfiler mProfilerActions;
public AsyncTaskPost(IProfiler profilerActions){
mProfilerActions = profilerActions;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
mProfilerActions.ShowProgressbar();
}
@Override
protected Void doInBackground(URL... urls) {
try{
Thread.sleep(5000);
return null;
}
catch (Exception ex) {
return null;
}
}
@Override
protected void onPostExecute(Void aVoid) {
mProfilerActions.HideProgressbar();
mProfilerActions.MakeToast();
}
@Override
protected void onCancelled() {
super.onCancelled();
mProfilerActions.HideProgressbar();
}
}
Post a Comment for "Passing View To AsyncTask To Access FindViewById"