Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7048551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:54:20+00:00 2026-05-28T02:54:20+00:00

I am reading a sqlite database into a tableLayout. I wan’t to do this

  • 0

I am reading a sqlite database into a tableLayout. I wan’t to do this in a seperate thread instead of having a long wait with no ui updates. So I used an AsyncTask to do some of the work and publish the results. However only about 1/4th of the item in my list actually make it on to the TableLayout. It works fine without the AsyncTask. Most of the items on the list throw an error (which I caught) java.util.concurrent.RejectedExecutionException. I’m not sure why this is. Here is my code.

myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                + TableName
                + " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");

        Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);         

        c.moveToFirst();
        if (c != null) {
            int color = 0xFFdfe8ea;
            this.startManagingCursor(c);
            // Loop through all Results
            do {
                try{
                    MyAsyncTask aTask = new MyAsyncTask();
                    String[]strings= {c.getString(c.getColumnIndex("title")),c.getString(c.getColumnIndex("artist")),c.getString(c.getColumnIndex("time")),c.getString(c.getColumnIndex("album")),""+color};
                    aTask.execute(strings);
                }catch(Exception e){
                    Log.w("****", e);
                }
        if (color == 0xFFdfe8ea) {
                    color = 0xFFf2f8fa;
                } else {
                    color = 0xFFdfe8ea;
                }
            } while (c.moveToNext());
        }

    } catch (SQLException e) {
        Log.e("****", e.toString());
    } finally {
        if (myDB != null) {
            myDB.close();
        }
    }

and here is the AsyncTask

class MyAsyncTask extends AsyncTask<String, Void, View> {
    @Override
    protected View doInBackground(String... params) {
        int color = Integer.parseInt(params[4]);

        TableRow tr = new TableRow(MainActivity.this);
        tr.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));

        TextView space = new TextView(MainActivity.this);
        space.setText("");
        space.setBackgroundColor(color); //0xFFf2f8fa alternating
        space.setSingleLine();
        space.setPadding(2, 2, 2, 2);
        space.setGravity(Gravity.LEFT);
        space.setTextColor(0xFF000000);
        space.setLayoutParams(new LayoutParams(
                findViewById(R.id.spaceColumn).getWidth(),
                LayoutParams.WRAP_CONTENT));

        /* Create a Button to be the row-content. */
        TextView title = new TextView(MainActivity.this);
        title.setText(params[0]);
        title.setBackgroundColor(color); //0xFFf2f8fa alternating
        title.setSingleLine();
        title.setPadding(2, 2, 2, 2);
        title.setGravity(Gravity.LEFT);
        title.setTextColor(0xFF000000);
        title.setEllipsize(TruncateAt.END);
        title.setLayoutParams(new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 1));

        /* Create a Button to be the row-content. */
        TextView artist = new TextView(MainActivity.this);
        artist.setText(params[1]);
        artist.setBackgroundColor(color); //0xFFf2f8fa alternating
        artist.setSingleLine();
        artist.setPadding(2, 2, 2, 2);
        artist.setGravity(Gravity.LEFT);
        artist.setTextColor(0xFF000000);
        artist.setEllipsize(TruncateAt.END);
        artist.setLayoutParams(new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 1));

        /* Create a Button to be the row-content. */
        TextView time = new TextView(MainActivity.this);
        time.setText(params[2]);
        time.setBackgroundColor(color); //0xFFf2f8fa alternating
        time.setSingleLine();
        time.setPadding(2, 2, 2, 2);
        time.setGravity(Gravity.LEFT);
        time.setTextColor(0xFF000000);
        time.setLayoutParams(new LayoutParams(
                findViewById(R.id.timeColumn).getWidth(),
                LayoutParams.WRAP_CONTENT));

        /* Create a Button to be the row-content. */
        TextView album = new TextView(MainActivity.this);
        album.setText(params[3]);
        album.setBackgroundColor(color); //0xFFf2f8fa alternating
        album.setSingleLine();
        album.setPadding(2, 2, 2, 2);
        album.setGravity(Gravity.LEFT);
        album.setTextColor(0xFF000000);
        album.setEllipsize(TruncateAt.END);
        album.setLayoutParams(new LayoutParams(
                0,
                LayoutParams.WRAP_CONTENT, 1));

        /* Add Button to row. */
        tr.addView(space);
        tr.addView(title);
        tr.addView(artist);
        tr.addView(time);
        tr.addView(album);

        /* Add row to TableLayout. */
        return tr;
    }

    @Override
    protected void onPostExecute(View tr) {
        ((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
    }

    @Override
    protected void onPreExecute() {
    }
 }

For reference this is how I fixed it.

class MyAsyncTask extends AsyncTask<Void, Song, Void> {

    @Override
    protected Void doInBackground(Void... params) {

        SQLiteDatabase myDB = openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
        String TableName = "songs";

        myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                + TableName
                + " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");

        Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);

        c.moveToFirst();
        int filepathIndex=c.getColumnIndex("filepath");
        int titleIndex=c.getColumnIndex("title");
        int artistIndex=c.getColumnIndex("artist");
        int albumIndex=c.getColumnIndex("album");
        int timeIndex=c.getColumnIndex("time");
        int playcountIndex=c.getColumnIndex("playcount");

        if (c != null) {
            int color = 0xFFdfe8ea;
         //   this.startManagingCursor(c);
            // Loop through all Results
            do {
                Song song = new Song(c.getString(filepathIndex),c.getString(titleIndex),c.getString(artistIndex),c.getString(albumIndex),c.getString(timeIndex),c.getInt(playcountIndex),color);
                // Add to song the data from your cursor
                publishProgress(song);

                if (color == 0xFFdfe8ea) {
                    color = 0xFFf2f8fa;
                } else {
                    color = 0xFFdfe8ea;
                }
            } while (c.moveToNext());
        }

        return null;
    }

    @Override
    protected void onPostExecute(Void item) {
    }

    @Override
    protected void onPreExecute() {
    }

    @Override
    protected void onProgressUpdate(Song... items) {
        for (Song song : items) {
            TableRow tr = new TableRow(MainActivity.this);
            tr.setLayoutParams(new LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));

            TextView space = new TextView(MainActivity.this);
            space.setText("");
            space.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            space.setSingleLine();
            space.setPadding(2, 2, 2, 2);
            space.setGravity(Gravity.LEFT);
            space.setTextColor(0xFF000000);
            space.setLayoutParams(new LayoutParams(
                    findViewById(R.id.spaceColumn).getWidth(),
                    LayoutParams.WRAP_CONTENT));

            /* Create a Button to be the row-content. */
            TextView title = new TextView(MainActivity.this);
            title.setText(song.title);
            title.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            title.setSingleLine();
            title.setPadding(2, 2, 2, 2);
            title.setGravity(Gravity.LEFT);
            title.setTextColor(0xFF000000);
            title.setEllipsize(TruncateAt.END);
            title.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Create a Button to be the row-content. */
            TextView artist = new TextView(MainActivity.this);
            artist.setText(song.artist);
            artist.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            artist.setSingleLine();
            artist.setPadding(2, 2, 2, 2);
            artist.setGravity(Gravity.LEFT);
            artist.setTextColor(0xFF000000);
            artist.setEllipsize(TruncateAt.END);
            artist.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Create a Button to be the row-content. */
            TextView time = new TextView(MainActivity.this);
            time.setText(song.time);
            time.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            time.setSingleLine();
            time.setPadding(2, 2, 2, 2);
            time.setGravity(Gravity.LEFT);
            time.setTextColor(0xFF000000);
            time.setLayoutParams(new LayoutParams(
                    findViewById(R.id.timeColumn).getWidth(),
                    LayoutParams.WRAP_CONTENT));

            /* Create a Button to be the row-content. */
            TextView album = new TextView(MainActivity.this);
            album.setText(song.album);
            album.setBackgroundColor(song.color); //0xFFf2f8fa alternating
            album.setSingleLine();
            album.setPadding(2, 2, 2, 2);
            album.setGravity(Gravity.LEFT);
            album.setTextColor(0xFF000000);
            album.setEllipsize(TruncateAt.END);
            album.setLayoutParams(new LayoutParams(
                    0,
                    LayoutParams.WRAP_CONTENT, 1));

            /* Add Button to row. */
            tr.addView(space);
            tr.addView(title);
            tr.addView(artist);
            tr.addView(time);
            tr.addView(album);

            // Add the row to the table
            ((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
        }
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:54:21+00:00Added an answer on May 28, 2026 at 2:54 am

    If you want to do it using an AsyncTask, consider using publishProgress(), that way each item will be added as it is fetched from the database. This way:

    NOTE: Consider that Song is a class with name, album, artist and time attributes.

    class MyAsyncTask extends AsyncTask<Void, Song, Void> {
        @Override
        protected Void doInBackground(Void... params) {
            myDB.execSQL("CREATE TABLE IF NOT EXISTS "
                + TableName
                + " (_id INTEGER PRIMARY KEY, filepath TEXT UNIQUE, title TEXT, artist TEXT, album TEXT, time TEXT, playcount NUMERIC);");
    
            Cursor c = myDB.rawQuery("SELECT * FROM " + TableName, null);         
    
            c.moveToFirst();
            if (c != null) {
                int color = 0xFFdfe8ea;
                this.startManagingCursor(c);
                // Loop through all Results
                do {
                    Song song = new Song();
                    // Add to song the data from your cursor
                    publishProgress(song);
                } while (c.moveToNext());
            }
    
            return null;
        }
    
        @Override
        protected void onPostExecute(Void item) {
        }
    
        @Override
        protected void onPreExecute() {
        }       
    
        @Override
        protected void onProgressUpdate(Song... items) {
            for (Song song : items) {    
                TableRow tr = new TableRow(MainActivity.this);
                tr.setLayoutParams(new LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
    
                TextView space = new TextView(MainActivity.this);
                space.setText("");
                space.setBackgroundColor(color); //0xFFf2f8fa alternating
                space.setSingleLine();
                space.setPadding(2, 2, 2, 2);
                space.setGravity(Gravity.LEFT);
                space.setTextColor(0xFF000000);
                space.setLayoutParams(new LayoutParams(
                        findViewById(R.id.spaceColumn).getWidth(),
                        LayoutParams.WRAP_CONTENT));
    
                /* Create a Button to be the row-content. */
                TextView title = new TextView(MainActivity.this);
                title.setText(song.getTitle());
                title.setBackgroundColor(color); //0xFFf2f8fa alternating
                title.setSingleLine();
                title.setPadding(2, 2, 2, 2);
                title.setGravity(Gravity.LEFT);
                title.setTextColor(0xFF000000);
                title.setEllipsize(TruncateAt.END);
                title.setLayoutParams(new LayoutParams(
                        0,
                        LayoutParams.WRAP_CONTENT, 1));
    
                /* Create a Button to be the row-content. */
                TextView artist = new TextView(MainActivity.this);
                artist.setText(song.getArtist());
                artist.setBackgroundColor(color); //0xFFf2f8fa alternating
                artist.setSingleLine();
                artist.setPadding(2, 2, 2, 2);
                artist.setGravity(Gravity.LEFT);
                artist.setTextColor(0xFF000000);
                artist.setEllipsize(TruncateAt.END);
                artist.setLayoutParams(new LayoutParams(
                        0,
                        LayoutParams.WRAP_CONTENT, 1));
    
                /* Create a Button to be the row-content. */
                TextView time = new TextView(MainActivity.this);
                time.setText(song.getTime());
                time.setBackgroundColor(color); //0xFFf2f8fa alternating
                time.setSingleLine();
                time.setPadding(2, 2, 2, 2);
                time.setGravity(Gravity.LEFT);
                time.setTextColor(0xFF000000);
                time.setLayoutParams(new LayoutParams(
                        findViewById(R.id.timeColumn).getWidth(),
                        LayoutParams.WRAP_CONTENT));
    
                /* Create a Button to be the row-content. */
                TextView album = new TextView(MainActivity.this);
                album.setText(song.getAlbum());
                album.setBackgroundColor(color); //0xFFf2f8fa alternating
                album.setSingleLine();
                album.setPadding(2, 2, 2, 2);
                album.setGravity(Gravity.LEFT);
                album.setTextColor(0xFF000000);
                album.setEllipsize(TruncateAt.END);
                album.setLayoutParams(new LayoutParams(
                        0,
                        LayoutParams.WRAP_CONTENT, 1));
    
                /* Add Button to row. */
                tr.addView(space);
                tr.addView(title);
                tr.addView(artist);
                tr.addView(time);
                tr.addView(album);
    
                // Add the row to the table
                ((TableLayout) findViewById(R.id.tableLayout)).addView(tr, new TableLayout.LayoutParams(
                                LayoutParams.FILL_PARENT,
                                LayoutParams.WRAP_CONTENT));
            }
        }
     }
    

    I believe you understood the concept behind AsyncTask wrongly, I strongly suggest you reread its documentation at Android Developers as its concept is a bit tricky to understand but very powerfull when you do so. As Romain Guy commented on your answer you can only execute UI code on the onPreExecute(), onProgressUpdate() and onPostExecute() methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making an app that parses an RSS Feed into a SQLite database
I have an sqlite database I am writing and reading from. Most of the
I have a SQLite database that is created by reading some data in PDF
I tried to insert a base64 encoded string into my SQLite database, but when
So I'm trying to get the values from a SQLite database into a cursor,
I have a list view populated by an adapter reading from SQLite database. When
I have an app reading from an SQLite database using an Adapter to display
I am reading the sqlite but unable to get data instead it showing nil
Possible Duplicate: Reading and writing images to an SQLite DB for iPhone use How
Reading this question I found this as (note the quotation marks) code to solve

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.