I am trying to load images into my FlipView using AsyncTask but Im not sure how to continue.
I need to use AsyncTask because the application is force closing since I have 20 images to load from the drawables.
private class asyncImage extends AsyncTask<Void, Void, Void>{
int i;
#Override
protected Void doInBackground(Void... params) {
for (i=0;i<imageID.length;i++){
image = new ImageView(getBaseContext());
image.setId(i);
image.setImageResource(imageID[i]);
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
image.setScaleType(ImageView.ScaleType.FIT_XY);
FlipV.addView(image);
}
return null;
}
protected void onPostExecute(Void result){
image.setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
image.setScaleType(ImageView.ScaleType.FIT_XY);
for(int x=0;x<imageID.length;x++){
FlipV.addView(???); //How to add images into FlipView?
}
}
}
Im stuck in adding the images to the FlipView. Any help?
As I have mentioned from my comment I've already solved my problem by using an imageview array instead ang loading the images in the doInBackground and adding the imageviews in the viewflipper in the postExecute. I've also added a progress dialog to show a loading time instead of a blank screen in my app. Thanks to everyone that replied.
private class asyncImage extends AsyncTask<Void, Void, Void>{
int i;
#Override
protected Void doInBackground(Void... params) {
for (i=0;i<imageID.length;i++){
image[i] = new ImageView(getBaseContext());
image[i].setImageResource(imageID[i]);
image[i].setBackgroundDrawable(getResources().getDrawable(R.drawable.border));
image[i].setScaleType(ImageView.ScaleType.FIT_XY);
}
return null;
}
protected void onPostExecute(Void result){
for (int x=0;x<imageID.length;x++){
FlipV.addView(image[x]);
}
count = 1;
progDialog.dismiss();
txItem.setText(Integer.toString(count) + "/" + (imageID.length));
}
}
You cant add instructions to doInBackground method which affect event/UI thread. Instead you should use, publishProgress() method to notify event thread, and in onProgressUpdate() add view to flipview.
Related
i have designed custom gallery but problem is that every time when i open gallery asyctask is call and all image getting load again. i want to load asyctask if new images arrived into device otherwise directly gallery will be shown without load
i have no more idea that how to do this kind of programming stuff so if there is anyway to do this kind of stuff then please suggest me
is there any way to do this ... ??
private class AsyncTaskForListview extends
AsyncTask<String, String, String> {
private String responce;
ProgressDialog progressDialog;
#Override
protected String doInBackground(String... params) {
getAllGalleryFolderName(); // process for get image
return responce;
}
#Override
protected void onPostExecute(String result) {
lvShowAllImageFolder.setAdapter(effectImageAdapter); // set adapeter in listview
effectImageAdapter.notifyDataSetChanged();
progressDialog.dismiss();
}
#Override
protected void onPreExecute() {
progressDialog = ProgressDialog
.show(DisplayGalleryActivity.this,
getResources().getString(
R.string.progress_dialog_heading),
getResources().getString(
R.string.progress_waiting_message));
}
#Override
protected void onProgressUpdate(String... text) {
}
}
You can use the FileObserver class to monitor a file or fold. It will notify if a change by any process in the device occurs. More details here .
I want to run 2 method simultaneously. However it show only the output of second method. Can you help me?
ImageView imgView;
ImageView imgView2;
public void loadImageToImageView(){ imgView.setImageBitmap(currentBitmap);}
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public void DrawLetter() {
new AsyncTask<Void, Void, Bitmap>() {
#Override
protected void onPreExecute() {
super.onPreExecute();
}
#Override
protected Bitmap doInBackground(Void... params) {
Draw();
Draw2();
return currentBitmap;
}
#Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if(bitmap!=null) {
loadImageToImageView();
}
}
}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
I'm assuming you're running this task twice. But the two times are going to load the result into the same image view. That means whichever finishes first will be overwritten by the one that finishes second. If you want to see both, you need to use two separate image views.
I'm making an application, which only pick some datas on a website. But the problem is that the layout does not show up until the datas are available, the application seem to be blocked while it is looking for the datas. I tried to put the content view in the oncreate and then change the text in the onstart, when I have the datas, but the application still blocks.
Is this possible to print a default text, and then change it when the application have the datas?
Try this, it uses AsyncTask:
public class LoadData extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
//declare other objects as per your need
#Override
protected void onPreExecute()
{
//show loading dialog
progressDialog= ProgressDialog.show(YourActivity.this, "Progress Dialog Title Text","Process Description Text", true);
//do initialization of required objects objects here
};
#Override
protected Void doInBackground(Void... params)
{
//do loading operation here
return null;
}
#Override
protected void onPostExecute(Void result)
{
super.onPostExecute(result);
progressDialog.dismiss();
};
}
You can call this using from your onCreate():
LoadData task = new LoadData();
task.execute();
Have you tried AsyncTask to download data from website (API)? It will show you progressdialog until data is not downloaded.
I seem to be going round in circles.
I have some code that even on a Galaxy S3 takes a few seconds to run. Drags data from database.
I want to add a progress bar popup (spinning circle) around this to give the user that the app is doing something.
I have tried Asyntasks elsewhere in app and work fine but for this type the main UI is not waiting for the Asyntask to finish before moving on and so the new activity that is called does not have all the data it needs and crashes.
Is AsyncTask the best way round this or is there an easier way to Puase the main Activity, show a progress bar and then move on once the long deley has been completed.
Thanks for time
UPDATE
public class UpdateDetailsAsyncTask extends AsyncTask<Void, Void, Boolean> {
private Context context;
private TaskCallback callback;
private ArrayList<Object> object;
private ProgressDialog progress;
public UpdateDetailsAsyncTask (
Context pContext,
ArrayList<Object> pObject,
TaskCallback pCallback) {
context = pContext;
callback = pCallback;
object = pObject;
}
#Override
protected void onPreExecute() {
Log.i("AsyncTask", "onPreExecuted");
progress = new ProgressDialog(context);
progress.setMessage(context.getResources().getString(R.string.loading));
progress.show();
}
#Override
protected Boolean doInBackground(Void... params) {
Log.i("Archery", "AsyncTask Excuted");
Log.i("Archery Scorepad", "Building Continue Round Details");
// Save Data to Database
return true;
}
protected void onPostExecute(Boolean result) {
Log.i("AsyncTask", "onPostExuted");
progress.dismiss();
callback.startNewActivity();
}
}
Task is called from main Activity
new UpdateDetailsAsyncTask(this, ArrayListOfObjects, this).exute();
UPDATE 2
..
UPDATE 3
The Code that does some work calls an a method within a Util Class which in calls a database class. I have log messages showing for all the rows of data I am saving to the database. It starts correctly and runs through it but the onPostExecute() appears to be called before the database method has completed.
Is my issue that I have nested classes within the activity and the task appears to have completed when the class below it has not?
Thanks
You must change to the next activity in onPostExecute from Asyntask
Yes!
Here is a simple code of AsuncTask
private class LoadImageAction extends AsyncTask<String, String, String>{
private Course course;
private ProgressBar pb;
public LoadImageAction(Course course, ProgressBar pb){
this.course = course;
this.pb = pb;
}
#Override
protected void onPreExecute(){
}
#Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
protected void onProgressUpdate(String... string){
}
#Override
protected void onPostExecute(String result){
}
}
You can run the action by
new LoadImageAction().execute();
I'm using "include" on my main layout. Each one of them is a RelativeLayout which needs an OnClick listener to be attached, and update some information related.
So I've tried to do it simply by:
setContentView(R.layout.allobjects);
ObjectListeners objectListeners = new ObjectListeners(objects);
for(int i=0;i<1;i++)
{
RelativeLayout objectBoxRelativeLayout = (RelativeLayout)findViewById(R.id.object1 + i);
objectBoxRelativeLayout.setOnClickListener(objectListeners.GetObjectListener(i));
SomeObject currentObject = this.objects.get(i);
Object viewObject = findViewById(R.id.object1 + i);
this.setObjectView(viewObject, currentObject);
}
The issue is that it takes too long after the "setContentView(R.layout.allobjects);" command, and the application shows black screen until it finish loading.
In addition, I use "setContentView(R.layout.allobjects);" after I perform the above commands. All of these commands have to be written after "setContentView(R.layout.allobjects);".
How can I handle that kind of situation ? Do I have to use onPreExecute and implement AsyncTask ?
Yes, AsyncTask is good solution to show loading dialog while these commands being executed.
UPDATE:
Add this class under your onCreate() function:
private class MyTask extends AsyncTask<Void, Void, Void> {
private ProgressDialog dialog;
private Context context;
public MyTask(Activity activity) {
context = activity;
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
dialog.setTitle("Loading...");
dialog.setMessage("Loading...");
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
//do your code here in background
protected void onPostExecute(Void res) {
dialog.dismiss();
}
}
then use the task inside onCreate() like this:
MyTask mt = new MyTask(this);
mt.execute();