我试图在将3000条记录插入数据库时显示进度稀释但我不知道将它放在我的代码中的位置
这是我的主要活动
public class Screen1 extends Activity {
/** Called when the activity is first created. */
private SQLiteAdapter mySQLiteAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.s1);
Button gas = (Button) findViewById(R.id.gas);
mySQLiteAdapter = new SQLiteAdapter(this);
mySQLiteAdapter.openToWrite();
// mySQLiteAdapter.deleteAll();
Cursor cur = mySQLiteAdapter.isempty();
if (cur != null){
cur.moveToFirst();
if (cur.getInt(0) == 0) {
mySQLiteAdapter.insert("AUDI","A4","2001","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2002","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2003","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2004","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2005","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2006","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2007","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2008","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2009","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2010","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A4","2011","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A5","2001","MUMTAZ");
mySQLiteAdapter.insert("AUDI","A5","2002","MUMTAZ");
// and other 3000 records
}
//close database
这是db文件:
public class SQLiteAdapter {
//
// Databsae Related Constants
//
private static final String DATABASE_NAME = "car";
private static final String DATABASE_TABLE = "options";
private static final int DATABASE_VERSION = 1;
public static final String KEY_MANUFACTURER = "manufacturer";
public static final String KEY_MODEL = "model";
public static final String KEY_YEAR = "year";
public static final String KEY_TYPE = "type";
public static final String KEY_ROWID = "_id";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
/**
* Database creation SQL statement
*/
private static final String DATABASE_CREATE =
"create table " + DATABASE_TABLE + " ("
+ KEY_ROWID + " integer primary key autoincrement, "
+ KEY_MANUFACTURER + " text not null, "
+ KEY_MODEL + " text not null, "
+ KEY_YEAR + " text not null, "
+ KEY_TYPE + " text not null);";
private Context context;
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
}
}
public SQLiteAdapter(Context c){
context = c;
}
public long insert(String manufcturar, String model, String year ,String type) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_MANUFACTURER, manufcturar);
initialValues.put(KEY_MODEL, model);
initialValues.put(KEY_YEAR, year);
initialValues.put(KEY_TYPE, type);
return sqLiteDatabase.insert(DATABASE_TABLE, null, initialValues);
}
答案 0 :(得分:1)
您需要在工作线程中运行数据库更新并在UI线程上发布进度。用于此目的的工具是AsyncTask
。课程文档包含一些示例代码,还有更多内容可在指南主题Processes and Threads和Painless Threading文章中找到。最后一个链接是最容易开始阅读的地方。
答案 1 :(得分:0)
在运行db脚本之前,您可以显示忙/进度图标,并在执行代码后使其消失。
cur.moveToFirst(); //right before this line enable the progressbar
if (cur.getInt(0) == 0)
{
//Insert
}
//finish
答案 2 :(得分:0)
我认为你应该使用AsyncTask。 在您的活动中:
private static final int PROGRESS_DIALOG_ID = 0;
.....
@Override
protected Dialog onCreateDialog(int id) {
if (id == PROGRESS_DIALOG_ID) {
ProgressDialog dialog = new ProgressDialog(this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setMessage("Loading...");
dialog.setCancelable(false);
return dialog;
}
}
........
class SomeTask extens AsyncTask<Void, Void, Void>() {
@Override
protected void onPreExecute() {
showDialog(PROGRESS_DIALOG_ID);
}
@Override
protected void doInBackground(Void... voids) {
//fill your database
}
@Override
protected void onPostExecute(Void voids) {
dismissDialog(PROGRESS_DIALOG_ID);
}
}