我在準備一個應用程序。最初我必須將大量數據放入數據庫中。要做到這一點,我已經如下使用AsyncTask
:無法顯示進度對話框
這是我的Java代碼:
package com.hippieapp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
import android.os.Bundle;
import android.preference.PreferenceManager;
public class Home extends Activity {
static SQLiteDatabase database;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this);
if (!prefs.getBoolean("firstTime", false)) {
new CheckTypesTask().execute();
SharedPreferences.Editor editor = prefs.edit();
editor.putBoolean("firstTime", true);
editor.commit();
}
}
private void insert_in_questions() {
// TODO Auto-generated method stub
loadDatabase();
String currentDate = new SimpleDateFormat("yyyy/MM/dd").format(Calendar
.getInstance().getTime());
ContentValues values = new ContentValues();
values.put("question",
"How many inch your hair long reaches past your shoulders ?");
values.put("ans_type", "textinput");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Are you male ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"For males- Have you gotten flack for having long hair ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"For Females- Have you gotten flack for wearing no makeup ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Are you over the age of 40 and still have not cut your hair ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Do you have a brownie recipe that includes ingredients you can not get at the local A&P ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Is your dwelling is decorated with beads hanging from the door, a lava lamp or fillmore posters ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you have a bean bag chair in your \"groovy pad\" ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Does the bean bag chair is the only piece of furniture you have ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Is your home decorated with homemade art ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you do pottery ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you know how to do macrame ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you know how to do the macaroon ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Did you were at Woodstock ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you think you may have been at Woodstock, but aren't entirely sure ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Who is your favorite Beatle ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Do you have fire drills to practice getting all your Grateful Dead tapes out of the house ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"How many state line you've crossed on your longest road trip to see a 60s era musical artist or band in concert ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Have you have traveled to another country to see such a band or artist ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Were you were affected personally by Jerry Garcia's death ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you know what bag you're in ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you own an incense burner ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you own, or have owned a VW bus ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Is the bus painted multiple colors ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you play the sitar ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you written a song protesting war, bucking the establishment, or containing a reference to illegal drugs ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you written songs about all three protesting war, bucking the establishment, or containing a reference to illegal drugs ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Were you actually alive in the 1960s ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you really, really wish you had been alive in the 1960s ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you believe that \"All You Need is Love\" ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you believe in good medical plan ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Does the word \"trip\" not make you think of a vacation ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Does the word \"man\" peppers your conversations ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you used the word \"groovy\" or the phrase \"Can you dig it ?\" with a straight face?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you used words \"groovy\" and the phrase \"Can you dig it ?\" both in the same sentence ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Have you taken part in a peace march ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Have you started a petition ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Have you taken part in a sit-in ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you \"taken part in a peace march\", \"started a petition\" and \"taken part in a sit-in\" all three ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Have you ever received junk mail addressed to \"dear radical\" ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you think \"social action\" is a frat party on a saturday night ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have your children names like Windsong, Freedom or Peace Love Understanding Smith ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you wish your parents had named you Peace Love Understanding Smith ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you say \"thank you\" when someone calls you a hippy ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Do you say \"Groovy man, like, you think I'm a hippy? Far out, can you dig that?\"");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you wear Birkenstocks ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you make your own sandals ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Do you have a bumper sticker reading \"save the whales\", \"think green\" or has dancing bears on it ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Does bumper stickers like \"save the whales\", \"think green\" or has dancing bears on it; are holding your car together ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Are you a vegetarian ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Does the only reason you call yourself a \"vegetarian\" is that you stopped eating Big Macs ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you smell like patchouli ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Have you have sold any hand-made item in the parking lot of a concert ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you think money is the root of all evil ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you add the phrase \"and may the devil smite me with it\" ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Do you think Jim Morrison's poetry is profound ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you think John Lennon's drawings are art ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you like Bob Dylan's singing voice ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Have you ever heard of Jim Morrison, John Lennon or Bob Dylan?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Do you have a necklace with either wooden beads, seeds, a peace or yin yang symbol ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Do you have \"wooden beads\", \"seeds\" and \"a peace or yin yang symbol\" all together ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Did you use to have \"wooden beads\", \"seeds\" and \"a peace or yin yang symbol\" all of the three but you gave them away or traded them for concert tickets");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Does your computer screen name or password is related to a 60s musical performer ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Are the only CDs or records in your collection that were recorded after 1980 were recorded by artists who were most famous in the 60s or 70s ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Have you been to San Francisco ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Have you wore flowers in your hair ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Have you lived in a commune ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you quit the commune because you were tired of being the one to do all the work ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you own a tie dyed article of clothing ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Did you dye article of clothing yourself ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question", "Do you own any clothing items made of hemp ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you practiced Transcendental Meditation, have seriously studied Eastern religion, or been a Jesus freak ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put(
"question",
"Have you done \"practiced Transcendental Meditation\", \"have seriously studied Eastern religion\", and \"been a Jesus freak\" all of the these ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
values.put("question",
"Have you had a chemically induced spiritual experience ?");
values.put("ans_type", "yesno");
values.put("creation_date", currentDate);
values.put("is_delete", "0");
values.put("is_publish", "1");
database.insert("questions", null, values);
values.clear();
.
.
.
}
private void create_database_and_tables() {
// TODO Auto-generated method stub
database = openOrCreateDatabase("HippieDatabase.db",
SQLiteDatabase.CREATE_IF_NECESSARY, null);
database.setLocale(Locale.getDefault());
database.setVersion(1);
String create_table_questions = "create table if not exists questions (q_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "question TEXT, "
+ "ans_type TEXT, "
+ "creation_date TEXT, "
+ "is_delete INTEGER, "
+ "is_publish INTEGER);";
database.execSQL(create_table_questions);
String create_table_answer_option = "create table if not exists answer_option (ao_id INTEGER PRIMARY KEY AUTOINCREMENT, "
+ "q_id INTEGER, "
+ "ans_option TEXT, "
+ "ans_point INTEGER, "
+ "is_delete INTEGER, "
+ "is_publish INTEGER);";
database.execSQL(create_table_answer_option);
}
private void loadDatabase() {
database = openOrCreateDatabase("HippieDatabase.db",
SQLiteDatabase.OPEN_READWRITE, null);
}
class CheckTypesTask extends AsyncTask<Void, Void, Void> {
ProgressDialog Asycdialog = new ProgressDialog(Home.this);
String typeStatus;
@Override
protected void onPreExecute() {
Asycdialog.setMessage("First time initialization. Please wait...");
Asycdialog.show();
super.onPreExecute();
}
@Override
protected Void doInBackground(Void... arg0) {
create_database_and_tables();
insert_in_questions();
// insert_in_answer_option();
return null;
}
@Override
protected void onPostExecute(Void result) {
database.close();
Asycdialog.dismiss();
super.onPostExecute(result);
}
}
// public void insert_in_answer_option() {
// // TODO Auto-generated method stub
//
//
// }
}
正如你看到的,我已經做了在數據庫76項,但我仍無法查看ProgressDialog
。第一次運行的應用程序中仍然會插入更多的數據。
有誰能告訴我爲什麼?代碼有沒有錯,或者數據不夠大,無法顯示ProgressDialog
?請幫幫我。這是我第一次使用AsyncTask
完成工作。
嘗試添加了Thread.sleep();確保進度對話框正常工作。 通常插入76個條目不會超過幾毫秒。 – 2013-04-23 14:49:38
你可以給我一個代碼片段,在我的代碼中寫代碼Thread.sleep(); – 2013-04-23 14:54:00
嘗試在insert_in_questions()中寫入它,當一些插入完成時。 – 2013-04-23 14:56:44