Rox Rox - 19 days ago 7
Android Question

ProgressDialog froze when a table is beeing populated with big amount of data

You shall never work with UI stuff outside the UI thread (main thread). But what to do in this case:

Run a new thread to retrieve a huge load of data. Meanwhile, show a ProgressDialog indicating the work. When the data is retrieved, invoke the main thread and populate the table. When populating the table with the huge load of data, the ProgressDialog is "frozen" for some seconds (since the UI thread is busy with populating the table).

Is there any way to make the ProgressDialog not to "freeze" while the table is beeing populated?

public class MyActivity extends Activity {
private ProgressDialog progressDialog;
private TableLayout tableLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tableLayout = (TableLayout)v.findViewById(R.id.myTable);

progressDialog = ProgressDialog.show(this, "", "Loading...");
new Thread(new Runnable() {
@Override
public void run() {
final List<Data> data = fetchBigDataFromInternet();
MyActivity.runOnUiThread(new Runnable() {
@Override
public void run() {
tableLayout.removeAllViews();
for(Data d : data) { // This makes the progressDialog to freeze
TableRow tableRow = new TableRow(MyActivity.this);
tableLayout.addView(tableRow);
}
progressDialog.dismiss();
....
}
}

Answer

Yes there is a way to make the dialog not to freeze, and thats using the "Lazy Load" technique, if the ammount of data you are populating is huge, break it in smaller parts, and populate accordingly, you need to implement a mechanism to know when new a new "piece" of the data has to be populated, this is very common in apps that have a huge amount of data, usually an "updating" or "refreshing" message is used...

Hope this helps..

Regards!

Comments