1

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!

ofirbt
  • 1,846
  • 7
  • 26
  • 41

2 Answers2

2

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

Community
  • 1
  • 1
David Olsson
  • 8,085
  • 3
  • 30
  • 38
  • Hmmm... interesting, but don't u think it would be even more annoying for the user to wait a bit each time he scrolls down? – ofirbt Jul 06 '11 at 13:24
  • As long as you plan a head depending on the number of rows you need to reach before you start. But eventually the user would still probably have to wait. Just a matter of convenience to wait a little bit each time or a longer bit another time. I would prefer to wait a small amount of time the first time. But it all depends on what kind of data will be shown, how likely will the user be to scroll down each time? – David Olsson Jul 06 '11 at 13:43
  • I've tried it and it works, Thanks! BTW - I couldn't even notice the tiniest lag while scrolling! – ofirbt Jul 07 '11 at 12:47
0

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

Community
  • 1
  • 1
nickfox
  • 2,835
  • 1
  • 26
  • 31