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 .
Related
I'm using volley to fetch data using volley and display in User Interface, and it takes time to load the data. What i want to happen is, I want to fetch the data in background during the splash screen or loading screen and display it in User interface. I want the fetching part to be done in another activity and it should be a background service and this should be called in "main activity" to populate the fields.
You can try using the AsyncTask. So the process goes like that:
In onPreExecute() you can show a ProgresDialog or a ProgresBar to visualize your loading process
in doInBackground(Params...) you start loading your data
and in onPostExecute(Result) you display your data in UI and hide your ProgresDialog/ProgresBar.
Example of AsyncTask usage.
Do it like this:
private class LoadDataBaseData extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
//Start loading your data
return "Data loaded";
}
#Override
protected void onPostExecute(String result) {
//Update your UI with data you loaded/start your activity with loaded data
}
#Override
protected void onPreExecute(){
}
#Override
protected void onProgressUpdate(Void... values) {
}
}
use AsyncTask like that
private class LongOperation extends AsyncTask<String, Void, String>
{
#Override
protected String doInBackground(String... params)
{
//do your work here
return "Executed";
}
#Override
protected void onPostExecute(String result)
{
progressBar.dismiss();
}
#Override
protected void onPreExecute()
{
progressBar = ProgressDialog.show(getActivity(), null, "message...");
progressBar.show();
}
#Override
protected void onProgressUpdate(Void... values)
{
}
}
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 am working on android project in which I have to apply loading screen until the data loads from database. I know that how to start proegressBar but I need to know how can I stop the loading screen as data loaded from database.If u can please give me one simple example from which I can understand that how can I do this because I am new to android.
Since you are getting data from a database you should do it asynchronously.
The best way to do it would be using an AsyncTask.
class GetDataTask extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
#Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog(YourActivity.this);
dialog.setMessage("Loading...");
dialog.setIndeterminate(true);
dialog.show();
}
#Override
protected Void doInBackground(Void... params) {
//get data from DB here
return null;
}
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
dialog.dismiss();
}
}
And to make it execute: new GetDataTask().execute();
I am working on an android app, in that app i have intent2 which on click redirects to intent3 and takes some time then loads a table and displays server data into it.
Sometimes if there is a lot of data, it tales pretty much time to get the dataload and the time blank screen is displayed increases.
i wish to show a loading bar till the data loads.
how can i show the ProgrssBar till only when data is not displayed ?
Probably your best bet would be to use AsyncTask in your "intent3":
You could do it like this:
private class performBackgroundTask extends AsyncTask <Void, Void, Void>
{
private ProgressDialog Dialog = new ProgressDialog(ClassName.this);
protected void onPreExecute()
{
Dialog.setMessage("Please wait...");
Dialog.show();
}
protected void onPostExecute(Void unused)
{
try
{
if(Dialog.isShowing())
{
Dialog.dismiss();
}
// do your Display and data setting operation here
}
catch(Exception e)
{
}
#Override
protected Void doInBackground(Void... params)
{
// Do your background data fetching here
return null;
}
}
You probably need to run an AsyncTask on onCreate when you open the new activity, the structure of the asynctask would be like this (taken from the google doc), notice that if you want to increament a progress bar you have to implement onProgressUpdate and call publishProgress in the doInBackground method
private class DownloadFilesTask extends AsyncTask<Void, Integer, Void> {
protected void onPreExecute()
{
// show your progress bar
}
protected Void doInBackground(Void... params) {
// do your work and publish the progress
publishProgress(progress);
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Void result) {
//dismiss your progress bar
}
}
This code is just an example, of course you need to adapt it to your logic/code.
Check out this simple and complete example