2012-03-09 12 views
2

SQLiteデータベースを作成するアプリケーション(24レコードあり)があります。初めてアプリケーションを読み込むときに、実際にメインをロードするのに少し時間がかかります私はそれがアプリケーションが初めてデータベースを作成しているためと考えています。それは何をしているのですか? 1)スプラッシュ画面 2)ブランク画面表示 - 任意のレイアウトやでプログラムされていないが、それは私が 3を想定して、再び、データベースを作成するのWHERE、これは)メインのレイアウトを表示 - 目標初めてデータベースを作成するときの進捗ダイアログ

私が大好きです空白の画面で進行状況バーを表示する最善の方法を見つけてください。可能であれば、データベースの挿入を数えることで可能です。ここで

SplashActivity.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

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

わからない

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

ですまたはホデータベースが作成されたときにだけ表示するように正しく実装する必要があります。任意のアイデアや提案?

答えて

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); 
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(); 

     } 
+0

私はそれに打撃を与え、あなたはあなたが歓迎されている – ondrovic

+0

感謝を知っているので、後になりますテストが解決された場合は、解決済みのトピックをマークしてください^^ –

+0

は動作しませんでした。ここに力を入れてくれました:03-21 17:25:58.750:E/AndroidRuntime(376):java.lang。 RuntimeException:Looper.prepare()を呼び出していないスレッド内でハンドラを作成できない – ondrovic

0

あなたは、ユーザーにそのような操作のためのHandler & Runnableクラスを必要としています。

私の回答hereを確認してください。

関連する問題