ofirbt
ofirbt

Reputation: 1896

Displaying large tables without choking UI thread

I have a table with about 1k rows that I want to display. This task obviously chokes the UI thread, resulting in a black screen while the onCreate() builds the table.

I've solved this by using AsyncTask which builds the wanted TableLayout in the "doInBackground" function and display it on the "onPostExecute" function.

Question #1: Is there any better practice that I'm not familiar with?

Question #2: My (simplified) "doInBackground" function looks like this:

protected Void doInBackground(Void... v) {

        tmpTableLayout = populateTable("");
        return null;
    }

And my (simplified) "onPostExecute" function looks like this:

protected void onPostExecute(Void v) {

        TableLayout ct = (TableLayout)findViewById(R.id.RealTable);
        ct.removeAllViews();
        /* WHATS HERE? */
    }

What should I write instead of the "WHATS HERE?" in the last line of code in order to display the content of "tmpTableLayout" in "ct" ?

Thanks in advance!

Upvotes: 1

Views: 1416

Answers (2)

nickfox
nickfox

Reputation: 2835

I would probably use a ListView and CursorAdapter and let Android manage fetching the data for you. See the accepted answer here.

Upvotes: 0

David Olsson
David Olsson

Reputation: 8225

Are you sure you want to display it all in one go?

One approach would be to dynamically load in more lines as the user scrolls down. So have a scroll listener that checks if the user is approaching the end of the content that is displayed and therefore start an AsyncTask or a thread loading more content.

Example: Android List Activity with dynamically loaded images form the web in android

Upvotes: 2

Related Questions