0

I have a fragment dialog containing a list of all the apps installed on the device.
Sometimes the loading takes some time and I would like to show a ProgressDialog while it loads.
I've tried the following code which didn't do any good :

@Override
public void onAttach(Activity activity) {
    // Show progress dialog
    showProgressDialog(activity);
    super.onAttach(activity);
}

private void showProgressDialog(Activity activity) {
    mProgressDialog = new ProgressDialog(activity);
    mProgressDialog.setTitle(R.string.loading);
    mProgressDialog.setMessage(getString(R.string.shared_options_wait_while_applist_loads));
    mProgressDialog.show();
}

The onCreate loads the whole list and the app images, and then I use :

@Override
public void onStart() {
    stopProgressDialog();
    super.onStart();
}

    private void stopProgressDialog() {
        mProgressDialog.dismiss();
}

Now I'm thinking about loading the whole list in a async task, but I can't figure what should the async task do, it should probably load the list, but how can I wait for the list and get the list when it's ready ? (I believe something like a callback should be used?)

Thanks

SagiLow
  • 5,721
  • 9
  • 60
  • 115
  • you can work with `UI` on `onPostExecute` method on `AsyncTask` class, create adapter and set to your list on that method, you can show your `progressDialog` on `onPreExecute`, this two method worked with `UI thread` – Shayan Pourvatan Mar 06 '14 at 20:07
  • Is there any example you can please refer me to ? Thanks – SagiLow Mar 06 '14 at 20:12
  • if you search you can find good example. you can see [this](http://developer.android.com/reference/android/os/AsyncTask.html) and [this](http://stackoverflow.com/questions/9671546/asynctask-android-example) – Shayan Pourvatan Mar 06 '14 at 20:15

2 Answers2

0

You can watch dataset changes in your adapter by adding a DataSetObserver.

@Override
public void onCreate(Bundle savedInstanceSate){
  super.onCreate(savedInstanceState);
  setContentview(R.layout.main_view);
  ListView myList = (ListView) findViewById(R.id.my_list);
  MyDataTypeAdapter adapter = new MyDataTypeAdapter();
  myList.setAdapter(adapter);
  adapter.registerDataSetObserver( new DataSetObserver(){
     @Override
     public void onChange(){
          //do stuff here
     }
  });
}

This way onChange will be loaded when your dataSet changes and you can download your image there.

However, I will better do an AsyncTask for each row in your adapter and download it's image independently. You could also use UniversalImageLoader library for this purpose.

Makerhack
  • 231
  • 1
  • 9
  • But my adapter is created with all the data (this is the constructor), should I just set it to `null` and let the async task add items one by one to the adapter ? – SagiLow Mar 06 '14 at 20:37
  • In your ListAdapter getView() method, you can program an asynctask to populate the view's image. This way if you have no view, the asynctask would not be triggered, but when you do, it will load the view. Also, to avoid launching too many asynctak, check that you have the image before starting the download – Makerhack Mar 06 '14 at 20:43
0

you can try Android Bitmap Fun example

Android Image Demo : http://developer.android.com/training/displaying-bitmaps/index.html

This example with GridView, you can use the same adapter for ListView by changing view in listItem.

Sachin Shelke
  • 449
  • 4
  • 12