AsyncTask (progressdialog parallel anzeigen)

M

mr.freeze

Ambitioniertes Mitglied
1
Hallo,
ich möchte einen progressdialog anzeigen, während ich was über
das netz beziehe.
ich will dazu die asynctask-klasse verwenden und habe folgenden code:

Code:
//in der UI
ShowProgressBarTask progressbar = new ShowProgressBarTask(meinContext);
progressbar.execute(null, null, null);

//die Async-Klasse
public class ShowProgressBarTask extends AsyncTask<Void, Void, Void> {
    
    ProgressDialog dialog = null;
    Context context = null;
    
    //Constructor
    public ShowProgressBarTask(Context context) {
        this.context = context;
    }
    
    
    @Override
    protected void onPreExecute() {
        
        dialog = new ProgressDialog(context);
        
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        super.onPostExecute(result);
    }

    @SuppressWarnings("static-access")
    @Override
    protected Void doInBackground(Void... params) {
        int i = 0;
        while(i < 1000){
            dialog.show(context, "", "Loading. Please wait...",true);
            i++;
        }
        return null;
    }
}

//dann in der UI -> zeugs übers netz beziehen
progressbar.cancel(true);
ich sehe leider keinen progressdialog!

Wenn sich da jemand auskennt, bitte ich um hilfe :D

Gruß
 
naja du solltest den dialog schon irgendwo in das layout legen. so wird er nicht erscheinen.
 
ich will, dass das ding gleichzeitig mit dem anderen layout angezeigt wird.

bzw. dass der dialog solange angezeigt wird, bis die daten aus dem netz da sind,
dann soll der weg und eine liste wird angezeigt....

geht das?
 
Zuletzt bearbeitet:
ja sicher geht das

lege eine progressbar in dein layout und mach sie visible=gone

in onPreExecute kannst sie visible setzen
dann im onPostExecute wieder auf gone
 
Ich bekomme es nicht hin ! :o

Ich habe eine main.xml mit einer liste.
in meinem adapter verwende ich ein anderes layout (image + textView)

code:
Activity
Code:
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
       
   ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar);

   ShowProgressBarTask showProgressbar = new ShowProgressBarTask(progressbar);
   showProgressbar.execute(null, null, null);
        

        
   List<Image> images = null;
                
   if(true){
       long bevor = System.currentTimeMillis();
       images = getPhotosFromURL();
       long danach = System.currentTimeMillis();
       System.out.println("dauer: " + (danach-bevor)); //ca. 18 sekunden
//            showProgressbar.cancel(true);
        }
//        else{
//            getPhotosFromDatabase();
//            //wird eventuell noch implementiert! Ich lasse es vorerst drin
//        }
        

        
   //Binding the apdater to the listView
   listView = (ListView) findViewById(R.id.list);
   customListAdapter = new CustomListAdapter(this, images);
   listView.setAdapter(customListAdapter);    
  }
Code:
public class ShowProgressBarTask extends AsyncTask<Void, Void, Void> {
    
    ProgressBar progressbar = null;
    
    //Constructor
    public ShowProgressBarTask(ProgressBar progressbar) {
        this.progressbar = progressbar;
    }
    
    
    @Override
    protected void onPreExecute() {
        
        progressbar.setVisibility(ProgressBar.VISIBLE);
        
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
        progressbar.setVisibility(ProgressBar.GONE);
        super.onPostExecute(result);
    }

    
    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }
}
main.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
<ListView 
    android:id="@+id/list" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    />

<ProgressBar 
    android:id="@+id/progressBar" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:visibility="gone"
    />
    
</LinearLayout>
layout mit imageview und textview (wird vom adapter verwendet)
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:id="@+id/LinearLayout01" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    >

<ImageView     
    android:id="@+id/icon" 
    android:layout_height="40px" 
    android:src="@drawable/icon" 
    android:layout_width="40px" 
    android:layout_marginRight="4px"
    />

<TextView    
    android:id="@+id/label" 
     android:text="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="40px" 
    android:textSize="20px"
    />

<CheckBox 
    android:id="@+id/listCheckBox" 
    android:layout_marginLeft="50px"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    />

</LinearLayout>
Ich sehe leider keine Progressbar!
sie soll erscheinen bevor die liste angezeigt wird!

gruß
 
hallo,

wenn ich folgendes mache, sehe ich den progressbar leider auch nicht:

Code:
//onOptionsItemSelected

ProgressBar bla = (ProgressBar) findViewById(R.id.progressBar);
bla.setVisibility(ProgressBar.VISIBLE);
int i = 0;
while(i< 10000){
       i++;
}
bla.setVisibility(ProgressBar.GONE);
 
Hier ist die AsyncTask-Klasse
ich sehe zwar den progressbar, aber ohne dass er
den verlauf anzeigt (es bleibt bei 0/100 und 0%)
woran liegt das?

Code:
public class ShowProgressBarTask extends AsyncTask<Void, Integer, Void> {
    
    Context context = null;
    ProgressDialog dialog = null;
    int percent = 0;
    
    //Constructor
    public ShowProgressBarTask(Context context) {
        this.context = context;
    }
    
    
    @Override
    protected void onPreExecute() {

        dialog = new ProgressDialog(context);
        dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        dialog.setMessage("Loading...");
        dialog.setCancelable(false);
        dialog.show();
        
        super.onPreExecute();
    }

    @Override
    protected void onPostExecute(Void result) {
        
        dialog.dismiss();
    
        super.onPostExecute(result);
    }

    
    @Override
    protected Void doInBackground(Void... params) {
        
        while(percent < 100){
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            percent +=2;
            publishProgress(percent);
        }
        return null;
    }


    @Override
    protected void onProgressUpdate(Integer... progress) {
    
        dialog.setProgress(progress[0]);
        super.onProgressUpdate(progress);
    }


    @Override
    protected void onCancelled() {
        dialog.dismiss();
        super.onCancelled();
    }
}

Gruß
 
Du solltest sicher einmal in den entsprechenden Methoden als erstes die super-Methoden aufrufen, nicht erst am Schluss.
 
hallo,
ja sicher!

daran liegt es aber leider nicht!

Gruß
 
ProgressDialog ist doch das Fenster mit dem Kreisel was eingeblendet wird.

ich habe in onCreate diesen Dialog erstellt:

dialog = new ProgressDialog(context);

dialog ist private Member der Activity

in onPreExecute wird dann nur dialog.show() und
in onPostExecute ein dialog.hide() aufgerufen.

In der Layout.xml habe ich nix von Progressbar stehen,
mir reicht aber auch der Kreisel.
 
Machst du was in "doInBackground"?
 
ja klar nutze ich die Methode
 
was machst du nach dem aufruf des progressbar-threads?
doInBackground wird ausgeführt,
der progressbar kommt

und der code nach dem progressbar-aufruf wird ausgeführt.

geht das, dass man während er was macht, nur den progressbar
anzeigt und danach den code weiter ausführt?

wenn ich so was mache funktionierts nicht:
progressbar.visible(visible)
mach was andres
progressbar.visible(gone)
 
mr.freeze schrieb:
was machst du nach dem aufruf des progressbar-threads?

garnichts, ich rufe die execute-Methode in onCreate auf, danach
ist Schluss. Der ProgessDialog wird, wie swordi schon schrieb in
onPreExecute zur Anzeige gebracht und in onPostExecute wieder
unsichtbar gemacht. Solange ist die GUI durch den Dialog blockiert.

Wenn der WorkerThread ( doInBackground-Methode ) fertig ist kann
der User wieder die GUI bedienen und weiter arbeiten. Der Code geht
dann in irgendeinem onClickHandler oder so weiter ^^

Bis es soweit ist, wird aus der doInBackground-Methode heraus der
ProgressDialog manipuliert, was in meinem Fall nun nicht ist, weil ich
nur den Kreisel anzeige.
 

Ähnliche Themen

D
  • Data2006
3 4 5
Antworten
84
Aufrufe
3.473
jogimuc
J
koje71
Antworten
1
Aufrufe
912
swa00
swa00
M
Antworten
8
Aufrufe
1.678
swa00
swa00
Zurück
Oben Unten