第一次创建数据库时的进度对话框

时间:2012-03-09 20:09:20

标签: java android sqlite progressdialog

我有一个创建SQLite数据库的应用程序(有24条记录),我注意到当我第一次加载应用程序时需要一些实际加载主视图,我认为这是因为app正在第一次创建数据库。这是它正在做的事情。 1.)显示启动画面 2.)空白屏幕 - 没有在任何布局中编程,这是它创建数据库的地方,我再次假设 3.)显示主要布局 - 目标

我想知道在空白屏幕中显示进度条的最佳方法,如果可能的话,可能是通过计算数据库的插入数,这里不太确定。

这是我的代码

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

 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

 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;
    }
}
 }

不确定放置此内容或如何正确实现的最佳位置仅在创建数据库时显示。有什么想法或建议吗?

3 个答案:

答案 0 :(得分:1)

好的,所以我决定改变整个过程,最后使用AsyncTask从我的网站下载文件并将其存储在SD卡上。我只是打开那个数据库,所有似乎工作得更快,它在下载文件时显示进度对话框。对于任何感兴趣的人都是代码:

注意:您必须在此之前创建数据库。

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;
    }
}
}

然后您只需执行以下操作即可访问它:

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

答案 1 :(得分:0)

请测试一下:

ProgressDialog bar;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

loading();

bar.dismiss();

}

private void loading() {
          bar = ProgressDialog.show(this, "Please wait",
                  "Long operation starts...", true);

          new Thread((new Runnable() {
              @Override
              public void run() {
                  bar.setMessage("Loading...");
                  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);

              }
          })).start();

        }

答案 2 :(得分:0)

您需要使用Handler&amp; Runnable此类操作的类。

请检查我的回答here