Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
418 views
in Technique[技术] by (71.8m points)

android - How to perform database operations using Async Task

My application is getting stuck while performing database operation after googling for solutions it was suggested that I use AsyncTask so that main thread doesn't get blocked.

I have created seperate class and extended SQLiteOpenHelper and implemented "OnCreate" and "OnUpgrade" method. But to implement AsyncTask I need to extend "AsyncTask". Now my issue here is that I have already extended one class and I can't extend another class for the same class.

My code is something like this.

import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

import com.example.project.R;

public class Database  extends SQLiteOpenHelper{

    static final String dbname="Project";
    static final int dbversion=1;

    TableA r=new TableA();


    SQLiteDatabase db;
    private Context Context;
    public Database(Context context) {
        super(context,  dbname, null, dbversion);
        try{
        db=getWritableDatabase();
        // TODO Auto-generated constructor stub
        if (db.isOpen()){

            System.out.println("Database is opened");
        }
            else{


                System.out.println("Database is not opened");
            }

        }
        catch(Exception e){

            Log.e(dbname, e.getMessage());

        }
    }


    @Override
    public void onCreate(SQLiteDatabase db) {

        // TODO Auto-generated method stub
        try{
        db.beginTransaction();

        db.execSQL(r.r_Table);


        db.insert(r.Tablename, null, r.insertdata());
        db.setTransactionSuccessful();
        retrivedata();
        }
        catch(Exception e){

            android.util.Log.e(r.Tablename, e.getMessage());

        }

        finally{

            db.endTransaction();
        }


    }//

Can anyone suggest how I can perform database operations with SQLiteOpenHelper within an AsyncTask.

Thanks Siva

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

In your Actvity add the following code, do the required changes,

private class GetContacts extends AsyncTask<Object, Object, Cursor> {
    DatabaseConnector dbConnector = new DatabaseConnector(
            getApplicationContext());

    @Override
    protected Cursor doInBackground(Object... params) {
        dbConnector.open();
        if (dbConnector.getAllValues() != null) {
            return dbConnector.getAllValues();
        } else
            return null;
    }

    @Override
    protected void onPostExecute(Cursor result) {
        if (result != null) {
            conAdapter.changeCursor(result); // set the adapter's Cursor
            dbConnector.close();
        }
    }
}

execute this by new GetContacts().execute((Object[]) null);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...