如何为数据库插入编写Android Async任务

时间:2015-07-01 06:46:09

标签: android sqlite

我是android的新手。我需要在我的应用程序的第一次运行时将大量数据堆栈插入数据库(最小行数复制到此处)。 此数据库插入将需要几个小步骤才能完成。 我想在这个时候添加进度条。 我查看了互联网并复制了一些代码,但我从未正确完成此任务。 你能帮我通过异步任务进行这个数据库插入吗? 我使用数据库打开助手来插入数据。坦克你。

buttonInitiate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {               

                dbConn = new DatabaseConnect(getApplicationContext());              

                dbConn.insertContact("1","g1", "m1", "apostolic  nunciature  in  sri  lanka ", "112582554","112597685", "", "112580906");
                dbConn.insertContact("1","g1", "m1", "Cardinal - Archbishop of Colombo His Eminence  Malcolm Cardinal Ranjith", "112695471","112695472", "112695473", "112692009");
                dbConn.insertContact("1","g1", "m1", "Conference  of  major  religious  superiors  sri lanka, Executive Secretary Sister Laetitia Coorey RGS", "766594466","713452929", "", "");
                dbConn.insertContact("1","g1", "bh", "Anuradhapura", "252222503","252234332", "", "252234901");
                dbConn.insertContact("1","g1", "bh", "Badulla", "552229241","552222867", "552222079", "552229241");
                dbConn.insertContact("1","g1", "bh", "Batticaloa", "652227642","652222723", "", "652224667");
                dbConn.insertContact("1","g1", "bh", "Chilaw", "322220625","322223377", "322222332", "322223233");
                dbConn.insertContact("1","g1", "bh", "Galle", "912234256","912223318", "", "914385602");
                dbConn.insertContact("1","g1", "bh", "Jaffna", "212222649","212222161", "", "212229953");
                dbConn.insertContact("1","g1", "bh", "Kandy", "814471601","814471602", "812222300", "812222300");
                dbConn.insertContact("1","g1", "bh", "Kurunegala", "372222854","372224935", "", "");
                dbConn.insertContact("1","g1", "bh", "Mannar", "232222503","232222710", "", "232222709");
                dbConn.insertContact("1","g1", "bh", "Ratnapura", "362232368","362232369", "362232370", "");
                dbConn.insertContact("1","g1", "bh", "Trincomalee", "262222677","262050546", "", "262222238");

                dbConn.insertKey("g1", "General Information");
                dbConn.insertKey("m1", "Main Information");
                dbConn.insertKey("bh", "Bishop`s Houses");
                dbConn.insertKey("nda", "National Directors / Animators");
                dbConn.insertKey("nc", "National Chaplains");
                dbConn.insertKey("cmr", "Congregations of Men Religious");
                dbConn.insertKey("cwr", "Congregations of Women Religious");
                dbConn.insertKey("ccwr", "Contemplative Congregations of Women Religious");


                buttonGo.setEnabled(true);

            }

        });

这是我全班...>>>>

public class FirstRun extends ActionBarActivity {


    Button buttonGo;
    Button buttonInitiate;
    //DatabaseConnect dbConn;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first_run);

        buttonGo = (Button) findViewById(R.id.buttonGo);
        buttonInitiate = (Button) findViewById(R.id.buttonInitiate);


        SharedPreferences pref = getSharedPreferences("ActivityPREF",
                Context.MODE_PRIVATE);
        if (pref.getBoolean("activity_executed", false)) {
            Intent intent = new Intent(this, MenuActivity.class);
            startActivity(intent);
            finish();
        } else {
            Editor ed = pref.edit();
            ed.putBoolean("activity_executed", true);
            ed.commit();

            buttonGo.setEnabled(false);
        }

        buttonInitiate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {               

                //dbConn = new DatabaseConnect(getApplicationContext());                
                new DbTask(getApplicationContext()).execute();              
                buttonGo.setEnabled(true);

            }

        });

        buttonGo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(FirstRun.this, MenuActivity.class);
                startActivity(intent);
                finish();

            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.first_run, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}


class DbTask extends AsyncTask<Void, Void, Void>{

    Context context;
    ProgressDialog dialog;
    DatabaseConnect dbConn;    

    public DbTask(Context context) {
        this.context = context;

    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub

        dialog = new ProgressDialog(context);
        dialog.setMessage("please wait..");
        dialog.setTitle("Db insertion progress");
        dialog.show();
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {

        dbConn = new DatabaseConnect(context);              

        dbConn.insertContact("1","g1", "m1", "apostolic  nunciature  in  sri  lanka ", "112582554","112597685", "", "112580906");
        dbConn.insertContact("1","g1", "m1", "Cardinal - Archbishop of Colombo His Eminence  Malcolm Cardinal Ranjith", "112695471","112695472", "112695473", "112692009");
        dbConn.insertContact("1","g1", "m1", "Conference  of  major  religious  superiors  sri lanka, Executive Secretary Sister Laetitia Coorey RGS", "766594466","713452929", "", "");
        dbConn.insertContact("1","g1", "bh", "Anuradhapura", "252222503","252234332", "", "252234901");
        dbConn.insertContact("1","g1", "bh", "Badulla", "552229241","552222867", "552222079", "552229241");
        dbConn.insertContact("1","g1", "bh", "Batticaloa", "652227642","652222723", "", "652224667");
        dbConn.insertContact("1","g1", "bh", "Chilaw", "322220625","322223377", "322222332", "322223233");
        dbConn.insertContact("1","g1", "bh", "Galle", "912234256","912223318", "", "914385602");
        dbConn.insertContact("1","g1", "bh", "Jaffna", "212222649","212222161", "", "212229953");
        dbConn.insertContact("1","g1", "bh", "Kandy", "814471601","814471602", "812222300", "812222300");
        dbConn.insertContact("1","g1", "bh", "Kurunegala", "372222854","372224935", "", "");
        dbConn.insertContact("1","g1", "bh", "Mannar", "232222503","232222710", "", "232222709");
        dbConn.insertContact("1","g1", "bh", "Ratnapura", "362232368","362232369", "362232370", "");
        dbConn.insertContact("1","g1", "bh", "Trincomalee", "262222677","262050546", "", "262222238");
        dbConn.insertContact("1","g1", "bh", "Archbishop Emeritus Most Rev. Dr. Nicholas Marcus Fernando ", "112958208","714929328", "", "");
        dbConn.insertContact("1","g1", "bh", "Emeritus Most Rev. Dr. Oswald Gomis", "112958676","", "", "");
        dbConn.insertContact("1","g1", "bh", "Bishop Emeritus Most Rev. Dr. Elmo Perera", "913923130","", "", "");
        dbConn.insertContact("1","g1", "bh", "Bishop Emeritus Most Rev. Dr. L.R. Anthony", "262222677","", "", "");
        dbConn.insertContact("1","g1", "bh", "Bishop Emeritus Most Rev. Dr. Raymond Peiris", "372233484","", "", "3722333484");
        dbConn.insertContact("1","g1", "bh", "catholic   bishops’  conference   secretariate  in  sri  lanka ", "112697062","112697110", "", "112699619");
        dbConn.insertContact("1","g1", "nda", "Pontifical Mission Societies,Rev. Fr. Reginald Saparamadu ", "112685673","114877837", "", "112685673");
        dbConn.insertContact("1","g1", "nda", "Social Communications,Rev. Fr. Cyril Gamini Fernando ", "777370699","112693425", "112697110", "112692372");
        dbConn.insertContact("1","g1", "nda", "Liturgy and Culture, Rev. Fr. Ignatius L. Varnakulasingham ", "773992899","112697110", "112697062", "112688804");
        dbConn.insertContact("1","g1", "nda", "SEDEC CARITAS,Rev. Fr. George Sigamony ", "112679735","112691885", "112693989", "112695136");
        dbConn.insertContact("1","g1", "nda", "Migrants, Rev. Fr. Damian Arsakularatne", "773688196","112665725", "112697062", "112697110");
        dbConn.insertContact("1","g1", "nda", "Catholic Education,Very Rev. Fr. Ivan Perera", "773823666","112695471", "112695472", "112692009");
        dbConn.insertContact("1","g1", "nda", "Catechetics and Bible Apostolate,  Rev. Fr. Piyal  Janaka", "112685459","372299213", "", "112665021");
        dbConn.insertContact("1","g1", "nda", "Laity,Rev. Fr. Leo Perera", "774535867","112524584", "112526860", "");
        dbConn.insertContact("1","g1", "nc", "Catholic National Association of the Laity(CNAL) ,Rev. Fr. Tony Martyrn (Moderator)", "714048769","112697110", "112697062", "112699619");
        dbConn.insertContact("1","g1", "nc", "Legion of Mary, Rev. Fr. Ignatius L. Varnakulasingham", "773992899","112446669", "", "");
        dbConn.insertContact("1","g1", "nc", "Society of St. Vincent de Paul,Rev. Fr. Leo Perera ", "774535867","112524584", "112526860", "112524584");
        dbConn.insertContact("1","g1", "nc", "University Students, Rev. Fr. Claude Perera, O.M.I.", "717158130","812388292", "", "");
        dbConn.insertContact("1","g1", "nc", "Catholic Youth Federation,  Rev. Fr. Gihan Gunathilake ", "716842848","112240655", "", "");
        dbConn.insertContact("1","g1", "nc", "Cursillo,Rev. Fr. Tony Martyn", "714048769","112699619", "112446393", "112699619");
        dbConn.insertContact("1","g1", "nc", "St. Luke’s Doctor’s Guild, Rev. Fr. Crispin Leo", "777347001","112588249", "", "112587782");
        dbConn.insertContact("1","g1", "nc", "Kithudhana Pubuduwa, Rev. Fr. Oscar Abeyaratna", "112230300","115764123", "", "112231490");
        dbConn.insertContact("1","g1", "nc", "Small Christian Community,(ASIPA)   Rev. Fr. P. A. Laily Fernando ", "777202981","312249516", "114932321", "112697110");
        dbConn.insertContact("1","g1", "nc", "Family Apostolate, Rev. Fr. Claude Nishantha Nonis ", "112236457","", "", "");
        dbConn.insertContact("1","g1", "cmr", "Society of Jesus", "777874242","313718271", "", "");
        dbConn.insertContact("1","g1", "cmr", "Sylvestro Benedictines", "812223684","812219858", "714476325", "812235132");
        dbConn.insertContact("1","g1", "cmr", "Missionary Oblates of Mary Immaculate (Colombo Province) ", "112540752","112521180", "714250276", "112521583");
        dbConn.insertContact("1","g1", "cmr", "Missionary Oblates of Mary Immaculate (Jaffna Province) ", "212222721","212228086", "", "");
        dbConn.insertContact("1","g1", "cmr", "Third Order Regular of St. Francis", "771705869","312224386", "", "312220722");
        dbConn.insertContact("1","g1", "cmr", "Congregation of the Rosarians", "779152015","217200861", "", "");
        dbConn.insertContact("1","g1", "cmr", "Congregation of the Most Holy Redeemer/Redemptorists", "716053130","115623898", "", "");
        dbConn.insertContact("1","g1", "cmr", "Congregation of the Blessed Sacrament  ", "112421367","777840868", "", "");
        dbConn.insertContact("1","g1", "cmr", "Salesians of Don Bosco ", "777710613","115362559", "115368082", "115378561");
        dbConn.insertContact("1","g1", "cmr", "Institute Voluntas Dei", "312273010","", "", "");
        dbConn.insertContact("1","g1", "cmr", "Claretian Missionaries", "772207340","312231455", "", "");
        dbConn.insertContact("1","g1", "cmr", "Order of Friars Minor", "778391253","112785740", "112785988", "");
        dbConn.insertContact("1","g1", "cmr", "Order of Capuchin Friars ", "312273479","", "", "");
        dbConn.insertContact("1","g1", "cmr", "Conventual Franciscans", "312238133","", "", "");
        dbConn.insertContact("1","g1", "cmr", "Clerics Regular of Somasca / Somascan Fathers ", "812216039","756711824", "", "");
        dbConn.insertContact("1","g1", "cmr", "Order of Preachers (Dominicans ) ", "812406270","775426302", "", "");
        dbConn.insertContact("1","g1", "cmr", "Society of the Divine Saviour", "322220019","", "", "");
        dbConn.insertContact("1","g1", "cmr", "Cistercian Fathers ", "812219856","773447394", "", "");
        dbConn.insertContact("1","g1", "cmr", "Carmelite Fathers ", "770350782","", "", "");
        dbConn.insertContact("1","g1", "cmr", "De La Salle Brothers (Brothers Of Christian Schools)", "112527135","112523988", "722865507", "112522442");
        dbConn.insertContact("1","g1", "cmr", "Marist Brothers Of the Schools", "312237011","312238130", "777347702", "312237194");
        dbConn.insertContact("1","g1", "cmr", "Franciscan Missionary Brothers", "112230555","2241670", "777400260", "");
        dbConn.insertContact("1","g1", "cmr", "Brothers Of Charity", "112237360","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Holy Family of Bordeaux (Colombo Province) ", "112694265","771560358", "", "112673464");
        dbConn.insertContact("1","g1", "cwr", "Holy Family of Bordeaux (Jaffna Province)", "112714116","778173465", "", "112732641");
        dbConn.insertContact("1","g1", "cwr", "Good Shepherd Sisters ", "112932575","718051888", "", "112930255");
        dbConn.insertContact("1","g1", "cwr", "Franciscan Missionaries of Mary ", "112691730","", "", "112691730");
        dbConn.insertContact("1","g1", "cwr", "Sisters of Charity of Jesus and Mary ", "11258496","11255481", "775060721", "112505160");
        dbConn.insertContact("1","g1", "cwr", "Little Sisters of the Poor", "112693878","", "", "112691779");
        dbConn.insertContact("1","g1", "cwr", "Sisters of the Holy Angels ", "342226833","776322241", "", "");
        dbConn.insertContact("1","g1", "cwr", "Apostolic Carmel Sisters ", "112667983","778840647", "", "112695045");
        dbConn.insertContact("1","g1", "cwr", "Sisters of Perpetual Help ", "313710151","774318498", "", "312235302");
        dbConn.insertContact("1","g1", "cwr", "Sisters of the Holy Cross  of  Menzigen", "312274560","779947092", "", "312274561");
        dbConn.insertContact("1","g1", "cwr", "Sisters of Providence ", "112958270","719585452", "", "112953766");
        dbConn.insertContact("1","g1", "cwr", "Daughters of Our Lady of the Sacred Heart ", "312255390","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Sisters of the Cross of Chavanod ", "812388114","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Sisters of the Blessed Virgin of Cremona ", "312275233","", "", "312275233");
        dbConn.insertContact("1","g1", "cwr", "Sisters of the Divine Saviour (Salvatorian Sisters) ", "372222516","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Dominican Sisters of Malta ", "312228628","773075551", "", "");
        dbConn.insertContact("1","g1", "cwr", "Dominican Sisters of the Philippines", "112841838","114373795", "", "");
        dbConn.insertContact("1","g1", "cwr", "Benedictine Sisters of Grace and Compassion ", "112658606","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Missionaries of Charity, (Sisters of Mother Teresa) ", "112522443","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Franciscan Minim Sisters of the Sacred Heart ", "112232403","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Daughters of Providence for the Deaf and Dumb ", "312277880","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Somascan Missionary Sisters ", "313613125","711952798", "", "313613125");
        dbConn.insertContact("1","g1", "cwr", "Daughters of St. Camillus ", "112932558","776671845", "", "");
        dbConn.insertContact("1","g1", "cwr", "Claretian Missionary Sisters", "112054769","774404744", "", "");
        dbConn.insertContact("1","g1", "cwr", "Daughters of Mary Help Of Christians (Salesian Sisters of Don Bosco) ", "313619898","77887881", "723823300", "");
        dbConn.insertContact("1","g1", "cwr", "Oblate Sisters of the of the Virgin Mary Of Fatima ", "112659270","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Congregation of the Immaculate Conception ", "232250686","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Sisters of the Holy Eucharist ", "522279213","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Comboni Missionary Sisters ", "522258354","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Sisters of St. Joseph of Lyon ", "773727865","", "", "");
        dbConn.insertContact("1","g1", "cwr", "Capuchin Tertiary Sisters of the Holy Family ", "770866967","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "1. Corpus Christi Carmel Prioress ", "112520006","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "2. St. Joseph’s Carmel ", "812212890","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "3. Carmel of the Immaculate Heart of Mary ", "912234057","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "Poor Clare Collettines ", "112958200","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "Rosarian Sisters of Sri Lanka ", "312278001","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "Holy Family Contemplatives ", "112236909","", "", "");
        dbConn.insertContact("1","g1", "ccwr", "Benedictines Nuns of Mater Unitas ", "312255467","", "", "");

        dbConn.insertKey("g1", "General Information");
        dbConn.insertKey("m1", "Main Information");
        dbConn.insertKey("bh", "Bishop`s Houses");
        dbConn.insertKey("nda", "National Directors / Animators");
        dbConn.insertKey("nc", "National Chaplains");
        dbConn.insertKey("cmr", "Congregations of Men Religious");
        dbConn.insertKey("cwr", "Congregations of Women Religious");
        dbConn.insertKey("ccwr", "Contemplative Congregations of Women Religious");

        dbConn.insertSetting(false, "");
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
         //buttonGo.setEnabled(true);

        super.onPostExecute(result);
    }

}

1 个答案:

答案 0 :(得分:1)

试试这个,

创建一个asynctask类

class DbTask extends AsyncTask<Void, Void, Void>
{

    Context context;
    ProgressDialog dialog;
    public DbTask(Context context) {
        this.context = context;
    }
    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        dialog = new ProgressDialog(context);
        dialog.setMessage("please wait..");
        dialog.setTitle("Db insertion progress");
        dialog.show();
        super.onPreExecute();
    }
    @Override
    protected Void doInBackground(Void... params) {

        dbConn = new DatabaseConnect(context);              

        dbConn.insertContact("1","g1", "m1", "apostolic  nunciature  in  sri  lanka ", "112582554","112597685", "", "112580906");
        dbConn.insertContact("1","g1", "m1", "Cardinal - Archbishop of Colombo His Eminence  Malcolm Cardinal Ranjith", "112695471","112695472", "112695473", "112692009");
        dbConn.insertContact("1","g1", "m1", "Conference  of  major  religious  superiors  sri lanka, Executive Secretary Sister Laetitia Coorey RGS", "766594466","713452929", "", "");
        dbConn.insertContact("1","g1", "bh", "Anuradhapura", "252222503","252234332", "", "252234901");
        dbConn.insertContact("1","g1", "bh", "Badulla", "552229241","552222867", "552222079", "552229241");
        dbConn.insertContact("1","g1", "bh", "Batticaloa", "652227642","652222723", "", "652224667");
        dbConn.insertContact("1","g1", "bh", "Chilaw", "322220625","322223377", "322222332", "322223233");
        dbConn.insertContact("1","g1", "bh", "Galle", "912234256","912223318", "", "914385602");
        dbConn.insertContact("1","g1", "bh", "Jaffna", "212222649","212222161", "", "212229953");
        dbConn.insertContact("1","g1", "bh", "Kandy", "814471601","814471602", "812222300", "812222300");
        dbConn.insertContact("1","g1", "bh", "Kurunegala", "372222854","372224935", "", "");
        dbConn.insertContact("1","g1", "bh", "Mannar", "232222503","232222710", "", "232222709");
        dbConn.insertContact("1","g1", "bh", "Ratnapura", "362232368","362232369", "362232370", "");
        dbConn.insertContact("1","g1", "bh", "Trincomalee", "262222677","262050546", "", "262222238");

        dbConn.insertKey("g1", "General Information");
        dbConn.insertKey("m1", "Main Information");
        dbConn.insertKey("bh", "Bishop`s Houses");
        dbConn.insertKey("nda", "National Directors / Animators");
        dbConn.insertKey("nc", "National Chaplains");
        dbConn.insertKey("cmr", "Congregations of Men Religious");
        dbConn.insertKey("cwr", "Congregations of Women Religious");
        dbConn.insertKey("ccwr", "Contemplative Congregations of Women Religious");
        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
         buttonGo.setEnabled(true);

        super.onPostExecute(result);
    }

}

使用

buttonInitiate.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                new DbTask(FirstRun.this).execute();
            }

        });