进度对话框时,第一次创建数据库 [英] Progress Dialog when Creating database for the first time

查看:188
本文介绍了进度对话框时,第一次创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个SQLite数据库(24记录)的应用程序,我注意到,当我加载应用程序的第一次需要它的几个实际加载主视图,而且我认为这是因为应用程式建立首次数据库。下面是它在做什么。
1)显示闪屏
2)黑屏 - 或任何布局未编程,这是它的创建数据库,我再次承担
3)显示主要布局 - 目标

I have an app that creates a SQLite database ( with 24 records ), I am noticing that when I load the app for the first time it takes it a few to actually load the main view, and I assume it is because the app is creating the database for the first time. Here is what it's doing. 1.) Shows Splash Screen 2.) Blank screen - not programmed in or in any layout, this is where it's creating the database, again I assume 3.) Shows Main layout - goal

我很想弄清楚,显示空白屏幕中的进度条的最佳方式,如果可能的话,也许通过计算数据库的插入,这里真的不知道。

I would love to figure out the best way to display a progress bar during the blank screen, if possible maybe by counting the inserts of the database, not really sure here.

下面是我的code

SplashActivity.java

SplashActivity.java

package com.ondrovic.boombozzpassport;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

public class SplashActivity extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    SplashHandler sHandler = new SplashHandler();
    setContentView(R.layout.splash);

    Message msg = new Message();
    msg.what = 0;
    sHandler.sendMessageDelayed(msg, 3000);

}

private class SplashHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        default:
            break;
        case 0:
            super.handleMessage(msg);

            startActivity(new Intent(getApplicationContext(), MainActivity.class));
            SplashActivity.this.finish();
        }
    }
}
 }

Database.java

Database.java

 package com.ondrovic.boombozzpassport;

 import java.io.ByteArrayOutputStream;
 import java.io.IOException;

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.provider.BaseColumns;
 import android.util.Log;

 public class Database extends SQLiteOpenHelper implements BaseColumns {
private final static String DB_NAME = "boombozz.db";
private final static int DB_VERSION = 1;
static final String TABLE_BEERS = "beers";
static final String COL_NAME = "name";
static final String COL_BREWER = "brewer";
static final String COL_ABV = "abv";
static final String COL_RATE = "rating";
static final String COL_BDESC = "breifdescription";
static final String COL_FDESC = "fulldescription";
static final String COL_TYPE = "type";
static final String COL_PIC = "picture";

private Context mContext;

public Database(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    mContext = context;
}

@Override
public void onCreate(SQLiteDatabase db) {
    Log.w("onCreate : ","Creating Database Version: " + DB_VERSION);
    db.execSQL("CREATE TABLE beers (" + "_id INTEGER PRIMARY KEY, "
            + "name TEXT, " + "brewer TEXT, " + "abv REAL, "
            + "rating REAL, " + "breifdescription TEXT, "
            + "fulldescription TEXT, " + "type TEXT, " + "picture BLOB);");

    Log.w("onCreate","Inserting records into database");

    addBeer(db, "NAME 1", "BREWER 1", "TYPE 1", "BDESC 1", "FDESC 1", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 2", "BREWER 2", "TYPE 2", "BDESC 2", "FDESC 2", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 3", "BREWER 3", "TYPE 3", "BDESC 3", "FDESC 3", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 4", "BREWER 4", "TYPE 4", "BDESC 4", "FDESC 4", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 5", "BREWER 5", "TYPE 5", "BDESC 5", "FDESC 5", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 6", "BREWER 6", "TYPE 6", "BDESC 6", "FDESC 6", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 7", "BREWER 7", "TYPE 7", "BDESC 7", "FDESC 7", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 8", "BREWER 8", "TYPE 8", "BDESC 8", "FDESC 8", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 9", "BREWER 9", "TYPE 9", "BDESC 9", "FDESC 9", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 10", "BREWER 10", "TYPE 10", "BDESC 10", "FDESC 10", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 11", "BREWER 11", "TYPE 11", "BDESC 11", "FDESC 11", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 12", "BREWER 12", "TYPE 12", "BDESC 12", "FDESC 12", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 13", "BREWER 13", "TYPE 13", "BDESC 13", "FDESC 13", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 14", "BREWER 14", "TYPE 14", "BDESC 14", "FDESC 14", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 15", "BREWER 15", "TYPE 15", "BDESC 15", "FDESC 15", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 16", "BREWER 16", "TYPE 16", "BDESC 16", "FDESC 16", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 17", "BREWER 17", "TYPE 17", "BDESC 17", "FDESC 17", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 18", "BREWER 18", "TYPE 18", "BDESC 18", "FDESC 18", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 19", "BREWER 19", "TYPE 19", "BDESC 19", "FDESC 19", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 20", "BREWER 20", "TYPE 20", "BDESC 20", "FDESC 20", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 21", "BREWER 21", "TYPE 21", "BDESC 21", "FDESC 21", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 22", "BREWER 22", "TYPE 22", "BDESC 22", "FDESC 22", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 23", "BREWER 23", "TYPE 23", "BDESC 23", "FDESC 23", 0, 0, R.drawable.beer1);
    addBeer(db, "NAME 24", "BREWER 24", "TYPE 24", "BDESC 24", "FDESC 24", 0, 0, R.drawable.beer1);

}

private void addBeer(SQLiteDatabase db, String name, String brewer,
        String type, String bdesc, String fdesc, int abv, int rate, int icon) {
    final ContentValues cv = new ContentValues();
    cv.put(COL_NAME, name);
    cv.put(COL_BREWER, brewer);
    cv.put(COL_TYPE, type);
    cv.put(COL_BDESC, bdesc);
    cv.put(COL_FDESC, fdesc);
    cv.put(COL_ABV, abv);
    cv.put(COL_RATE, rate);

    final Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(), icon);
    writeBitmap(cv, COL_PIC, bitmap);

    db.insert(TABLE_BEERS, null, cv);
}

static void writeBitmap(ContentValues cv, String name, Bitmap bitmap) {
    if (bitmap != null) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            out.flush();
            out.close();

            cv.put(name, out.toByteArray());
        } catch (IOException e) {
            // Ignore
        }
    }

}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("onUpgrade", "Upgrading database from version: " + oldVersion
            + " to version: " + newVersion);
    db.execSQL("DROP TABLE IF EXISTS beers");
}
 }

MainActivity.java

MainActivity.java

 package com.ondrovic.boombozzpassport;

 import java.util.concurrent.atomic.AtomicBoolean;

 import com.pushlink.android.PushLink;

 import android.app.Activity;
 import android.content.Context;
 import android.content.Intent;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.Bundle;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
 import android.widget.AdapterView;
 import android.widget.CursorAdapter;
 import android.widget.ListView;
 import android.widget.RatingBar;
 import android.widget.TextView;

 public class MainActivity extends Activity {

AtomicBoolean isActive = new AtomicBoolean(true);

BeerAdapter adapter = null;
Cursor model = null;

SQLiteDatabase db = null;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    PushLink.start(this, R.drawable.ic_launcher, 10, "269f78b325ebda1c", true);

    setContentView(R.layout.main);

    db = (new Database(this)).getWritableDatabase();

    ListView list = (ListView) findViewById(R.id.ListViewBeers);

    model = Beer.getAll(db);
    startManagingCursor(model);
    adapter = new BeerAdapter(model);
    list.setAdapter(adapter);
    list.setOnItemClickListener(onListClick);
}

private AdapterView.OnItemClickListener onListClick = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View v, int pos, long id) {
        startActivity(new Intent(getApplicationContext(), BeerForm.class).putExtra(BeerForm.INTENT_BEER_ID, String.valueOf(id)));
    }
};

@Override
public void onPause() {
    super.onPause();
    isActive.set(false);
}

@Override
public void onResume() {
    super.onResume();
    isActive.set(true);
}

@Override
public void onDestroy() {
    super.onDestroy();
    isActive.set(false);
    db.close();
}
class BeerAdapter extends CursorAdapter {

    public BeerAdapter(Cursor c) {
        super(MainActivity.this, c);
    }

    @Override
    public void bindView(View row, Context ctxt, Cursor c) {
        BeerWrapper wrapper = (BeerWrapper)row.getTag();
        wrapper.populateFrom(c);
    }

    @Override
    public View newView(Context context, Cursor c, ViewGroup parent) {
        LayoutInflater inflater = getLayoutInflater();
        View row = inflater.inflate(R.layout.row, parent, false);
        BeerWrapper wrapper = new BeerWrapper(row);

        row.setTag(wrapper);
        wrapper.populateFrom(c);

        return row;
    }
}

class BeerWrapper {

    private TextView name = null;
    private TextView brewer = null;
    private TextView bdesc = null;
    private RatingBar rating = null;
    private View row = null;

    BeerWrapper(View row) {
        this.row = row;
    }

    void populateFrom(Cursor c) {
        getName().setText(c.getString(c.getColumnIndex("name")));
        getBrewer().setText(c.getString(c.getColumnIndex("brewer")));
        getDesc().setText(c.getString(c.getColumnIndex("breifdescription")));
        getRating().setRating(c.getFloat(c.getColumnIndex("rating")));

    }

    TextView getName() {
        if (name == null) {
            name = (TextView)row.findViewById(R.id.TextViewRowName);
        }
        return name;
    }

    TextView getBrewer() {
        if (brewer == null) {
            brewer = (TextView)row.findViewById(R.id.TextViewRowBrewer);
        }
        return brewer;
    }

    TextView getDesc() {
        if (bdesc == null) {
            bdesc = (TextView)row.findViewById(R.id.TextViewRowBriefDescription);
        }
        return bdesc;
    }

    RatingBar getRating() {
        if (rating == null) {
            rating = (RatingBar)row.findViewById(R.id.RatingBarRatingSmall);
        }
        return rating;
    }
}
 }

不知道把这个最好的地方或如何正确地实现只在创建数据库时出现。任何意见或建议?

Not sure the best place to put this or how to correctly implement to only show up when the database is created. Any ideas or suggestions?

推荐答案

好了,所以我决定改变了整个过程,并结束了与下载从我的网站,并将其存储在SD卡上的文件的AsyncTask的打算。我然后只打开数据库和一切似乎工作得更快,它显示进度对话框,同时下载的文件。对于任何人有兴趣在这里是code:

Okay so I decided to change the whole process and ended up going with an AsyncTask that downloads the file from my site and stores it on the SD card. I am then just opening that database and all seems to work faster, it shows the progress dialog while downloading the file. For anyone that is interested here is the code:

请注意:你必须已经在此之前创建数据库

Note: you have to already have created your database prior to this.

Downloader.java

Downloader.java

 package com.ondrovic.downloader;

 import java.io.File;
 import java.io.FileOutputStream; 
 import java.io.InputStream;
 import java.net.HttpURLConnection;
 import java.net.URL;

 import android.app.Activity;
 import android.app.Dialog;
 import android.app.ProgressDialog;
 import android.content.Intent;
 import android.database.SQLException;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Environment;
 import android.util.Log;

 public class Downloader extends Activity {
SQLiteDatabase db = null;
public static final String LOG_TAG = "DOWNLOADER";
private ProgressDialog bar;
public static final int PROGRESS = 0;

File rDIR = Environment.getExternalStorageDirectory();

public String fDIR = "<location you want to store file on SD card>";
public String fNAME = "<your database.db>";
public String fURL = "<your url>";

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    checkDB();


        //DIRExists(fDIR);
        //new Download().execute(fURL);
}

class Download extends AsyncTask <String, String, String> {

    @Override
    public void onPreExecute() {
        super.onPreExecute();
        showDialog(PROGRESS);
    }

    @Override
    protected String doInBackground(String... aurl) {

        try {
            URL url = new URL(fURL);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setDoOutput(true);
            con.connect();

            int fSIZE = con.getContentLength();

            FileOutputStream out = new FileOutputStream(new File(rDIR + fDIR, fNAME));

            InputStream in = con.getInputStream();

            byte[] buffer = new byte[1024];
            int len = 0;
            long total = 0;

            while ((len = in.read(buffer)) > 0) {
                total += len;
                publishProgress("" + (int)((total*100)/fSIZE));
                out.write(buffer, 0, len);
            }
            out.close();
        } catch (Exception e) {
            Log.d(LOG_TAG, e.getMessage());
        }

        return null;
    }

    protected void onProgressUpdate(String... progress) {

        Log.d(LOG_TAG, progress[0]);
        bar.setProgress(Integer.parseInt(progress[0]));
    }

    @Override
    protected void onPostExecute(String unused) {

        dismissDialog(PROGRESS);
        startActivity(new Intent(getApplicationContext(), Main.class));
        Downloader.this.finish();
    }
}

public void DIRExists(String dirName) {

    File nDIR = new File(rDIR + dirName);
    if (!nDIR.exists()) {
        nDIR.mkdirs();
    }
}

private boolean checkDB() {

    try {

        db = SQLiteDatabase.openDatabase(rDIR + fDIR + fNAME, null,
                SQLiteDatabase.OPEN_READONLY);
        db.close();
        startActivity(new Intent(getApplicationContext(), Main.class));
        Downloader.this.finish();
    } catch (SQLException sqle) {
        Log.d(LOG_TAG, sqle.getMessage());
        DIRExists(fDIR);
        new Download().execute(fURL);
    }

    return db != null;
}

@Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case PROGRESS:
        bar = new ProgressDialog(this);
        bar.setTitle("Database");
        bar.setMessage("Downloading database...");
        bar.setIndeterminate(false);
        bar.setMax(100);
        bar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        bar.setCancelable(false);
        bar.show();

        return bar;
    default:
        return null;
    }
}
}

然后你只需通过执行以下操作访问:

And then you would just access it by doing the following:

db = SQLiteDatabase.openDatabase(<file location on sd card>, null, SQLiteDatabase.OPEN_READWRITE);

这篇关于进度对话框时,第一次创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆