我想将文本文件中的数据插入到SQLite数据库中:
public class SyncActivity extends Activity {
...
private MessageDialogView dlg = null; // an AlertDialog showing a message
private Patienter wait = null; // an AlertDialog containing an imageview showing an animation-list
...
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.synchro);
...
dlg = new MessageDialogView(SyncActivity.this, getLayoutInflater());
wait = new Patienter(SyncActivity.this, getLayoutInflater());
...
}
public void synchroniser(View view) { // called when a button is clicked
wait.show();
new RequestTask().execute("http://192.168.1.8/impots/data/syncro/webVersAndroid/parcelles.txt");
}
private class RequestTask extends AsyncTask<String, Void, Void> {
private String err = "";
private boolean error = false;
private String[] enregs;
@Override
protected Void doInBackground(String... s_url) {
enregs = new String[s_url.length];
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if(networkInfo != null) {
if (networkInfo.isAvailable() && networkInfo.isConnected()) {
System.setProperty("http.keepAlive", "false");
HttpURLConnection con = null;
BufferedReader reader = null;
for (int u=0; u<s_url.length; u++) {
String tmp;
String lines = "";
try {
URL url = new URL(s_url[u]);
if (url != null) {
con = (HttpURLConnection) url.openConnection();
if (con != null && con.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream in = con.getInputStream();
reader = new BufferedReader(new InputStreamReader(in));
boolean firstLine = true;
while ((tmp = reader.readLine()) != null) {
if (firstLine) {
firstLine = false;
lines += tmp;
}
else
lines += "\r\n" + tmp;
}
enregs[u] = lines;
}
}
} catch (MalformedURLException e) {
error = true;
err = getResources().getString(R.string.errBadUrl);
} catch (IOException e) {
error = true;
err = getResources().getString(R.string.errAccessError);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
if (con != null)
con.disconnect();
}
}
}
else {
error = true;
err = getResources().getString(R.string.errNotConnected);
}
} else {
error = true;
err = getResources().getString(R.string.errNoNetwork);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
if (error) {
wait.dismiss();
displayError(err);
} else {
populateDB();
wait.dismiss();
}
}
private void displayError(String msg) {
dlg.setTitre(getString(R.string.titreErrMsgBox));
dlg.setMsg(msg);
dlg.show();
}
private void populateDB() {
String[] enreg = SpliterString.Split(enregs[0], "\r\n"); // get each record of "parcelle"
db = new Db(SyncActivity.this).open();
for (int p=0; p<enreg.length; p++) {
Parcelle parcelle = new Parcelle(SpliterString.getColumnValueAt(enreg[p],0),
SpliterString.getColumnValueAt(enreg[p],1),
SpliterString.getColumnValueAt(enreg[p],2),
SpliterString.getColumnValueAt(enreg[p],3),
SpliterString.getColumnValueAt(enreg[p],4),
SpliterString.getColumnValueAt(enreg[p],5),
SpliterString.getColumnValueAt(enreg[p],6),
SpliterString.getColumnValueAt(enreg[p],7),
SpliterString.getColumnValueAt(enreg[p],8),
SpliterString.getColumnValueAt(enreg[p],9),
SpliterString.getColumnValueAt(enreg[p],10),
SpliterString.getColumnValueAt(enreg[p],11),
SpliterString.getColumnValueAt(enreg[p],12),
SpliterString.getColumnValueAt(enreg[p],13),
SpliterString.getColumnValueAt(enreg[p],14));
db.insertParcelle(parcelle);
}
db.close();
}
}
}
文本文件中的数据只是一行(数据表示为csv,但分隔符是|):
1||010-01-02|PARC-0001|PARCELLE DROUDOUDOU|10||2000.00|500.00|1500.00||10, Av Place de l'Independance - CENTRE VILLE|01/02/2000|10/02/2005|CENTRE VILLE|
问题是AlertDialog永远不会被解雇!没有错误!那有什么不对?
- 编辑 -
public class Patienter extends AlertDialog {
private View contenu;
AnimationDrawable frameAnimation = null;
public Patienter(Context context, LayoutInflater inflater) {
super(context);
contenu = inflater.inflate(R.layout.patienter, null);
setCancelable(false);
setCanceledOnTouchOutside(false);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(contenu);
ImageView img = (ImageView)contenu.findViewById(R.id.imgWait);
img.setBackgroundResource(R.drawable.wait);
frameAnimation = (AnimationDrawable) img.getBackground();
frameAnimation.start();
}
}
public class Db {
public static final String DB_NAME = "bdd_impot.bd", T_BIEN = "bien", T_PARCELLE = "parcelle", T_DECOUP_TERRIT = "decoupage_territoire";
public static int VERSION = 1;
private DbHelper DBHelper;
private SQLiteDatabase bd;
public Db(Context context) {
DBHelper = new DbHelper(context);
}
private class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context,DB_NAME, null, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS "+T_BIEN+" (bien_code integer PRIMARY KEY AUTOINCREMENT,"
+ "bien_ident text,"
+ "bien_code_sig text);");
db.execSQL("CREATE TABLE IF NOT EXISTS "+T_PARCELLE+" (bien_code integer,"
+ "decoup_terri_code text,"
+ "dec_decoup_terri_code text,"
+ "bien_ident text,"
+ "parcel_denomination text,"
+ "parcel_porte_ppale text,"
+ "parcel_porte_second text,"
+ "parcel_superfi_totale numeric,"
+ "parcel_superf_batie numeric,"
+ "parcel_superf_non_batie numeric,"
+ "parcel_superf_plani numeric,"
+ "parcel_adresse text,"
+ "parcel_date_deb_construct text,"
+ "parcel_date_fin_construct text,"
+ "quartier_lib text);");
db.execSQL("CREATE TABLE IF NOT EXISTS "+T_DECOUP_TERRIT+" (decoup_terri_code TEXT PRIMARY KEY,"
+ "decoup_terri_nom TEXT);"); // rue
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS "+T_PARCELLE+";");
db.execSQL("DROP TABLE IF EXISTS "+T_DECOUP_TERRIT+";");
db.execSQL("DROP TABLE IF EXISTS "+T_BIEN+";");
onCreate(db);
}
}
public Db open() {
bd = DBHelper.getWritableDatabase();
return this;
}
public void close() {
bd.close();
}
public void insertBien(Bien b) {
ContentValues values = new ContentValues();
values.put("bien_ident", b.getBien_ident());
values.put("bien_code_sig", b.getBien_code_sig());
bd.insert(T_BIEN, null, values);
}
private int getBienLastInsertID() { // call this method after INSERT on table BIEN
Cursor c = bd.rawQuery("SELECT last_insert_rowid()", null);
if (c != null) {
c.moveToFirst();
int id = c.getInt(0);
c.close();
return id;
}
return 0;
}
public Parcelle getParcelle(int bien_code) {
Parcelle p = new Parcelle();
String[] columnsParcelle = {"bien_code","decoup_terri_code","dec_decoup_terri_code","bien_ident",
"parcel_denomination","parcel_porte_ppale","parcel_porte_second",
"parcel_superfi_totale","parcel_superf_batie","parcel_superf_non_batie",
"parcel_superf_plani","parcel_adresse","parcel_date_deb_construct","parcel_date_fin_construct","quartier_lib"};
Cursor c = bd.query(T_PARCELLE, columnsParcelle, "bien_code=?", new String[]{String.valueOf(bien_code)}, null, null, null);
if (c != null && c.getCount() > 0 ) {
c.moveToFirst();
p = new Parcelle(c.getString(0),c.getString(1),c.getString(2),c.getString(3),c.getString(4),c.getString(5),c.getString(6),c.getString(7),c.getString(8),c.getString(9),c.getString(10),c.getString(11),c.getString(12),c.getString(13),c.getString(14));
}
return p;
}
public ArrayList<Parcelle> getAllParcelles() {
ArrayList<Parcelle> parcelles = new ArrayList<Parcelle>();
String selectQuery = "SELECT * FROM " + T_PARCELLE;
Cursor c = bd.rawQuery(selectQuery, null);
if (c.moveToFirst()) {
do {
Parcelle p = new Parcelle();
p.setBien_code(c.getString(0));
p.setDecoup_terri_code(c.getString(1));
p.setDec_decoup_terri_code(c.getString(2));
p.setBien_ident(c.getString(3));
p.setParcel_denomination(c.getString(4));
p.setParcel_porte_ppale(c.getString(5));
p.setParcel_porte_second(c.getString(6));
p.setParcel_superfi_totale(c.getString(7));
p.setParcel_superf_batie(c.getString(8));
p.setParcel_superf_non_batie(c.getString(9));
p.setParcel_superf_plani(c.getString(10));
p.setParcel_adresse(c.getString(11));
p.setParcel_date_deb_construct(c.getString(12));
p.setParcel_date_fin_construct(c.getString(13));
p.setQuartier_lib(c.getString(14));
parcelles.add(p);
} while (c.moveToNext());
}
return parcelles;
}
public void insertParcelle(Parcelle p) {
ContentValues values = new ContentValues();
if (p.getBien_code() == "")
values.put("bien_code", getBienLastInsertID());
else
values.put("bien_code", p.getBien_code());
values.put("decoup_terri_code", p.getDecoup_terri_code());
values.put("dec_decoup_terri_code", p.getDec_decoup_terri_code());
values.put("bien_ident", p.getBien_ident());
values.put("parcel_denomination", p.getParcel_denomination());
values.put("parcel_porte_ppale", p.getParcel_porte_ppale());
values.put("parcel_porte_second", p.getParcel_porte_second());
values.put("parcel_superfi_totale", p.getParcel_superfi_totale());
values.put("parcel_superf_batie", p.getParcel_superf_batie());
values.put("parcel_superf_non_batie", p.getParcel_superf_non_batie());
values.put("parcel_superf_plani", p.getParcel_superf_plani());
values.put("parcel_adresse", p.getParcel_adresse());
values.put("parcel_date_deb_construct", p.getParcel_date_deb_construct());
values.put("parcel_date_fin_construct", p.getParcel_date_fin_construct());
values.put("quartier_lib", p.getQuartier_lib());
bd.insert(T_PARCELLE, null, values);
}
public int updateParcelle(Parcelle p) {
ContentValues values = new ContentValues();
values.put("decoup_terri_code", p.getDecoup_terri_code());
values.put("dec_decoup_terri_code", p.getDec_decoup_terri_code());
values.put("bien_ident", p.getBien_ident());
values.put("parcel_denomination", p.getParcel_denomination());
values.put("parcel_porte_ppale", p.getParcel_porte_ppale());
values.put("parcel_porte_second", p.getParcel_porte_second());
values.put("parcel_superfi_totale", p.getParcel_superfi_totale());
values.put("parcel_superf_batie", p.getParcel_superf_batie());
values.put("parcel_superf_non_batie", p.getParcel_superf_non_batie());
values.put("parcel_superf_plani", p.getParcel_superf_plani());
values.put("parcel_adresse", p.getParcel_adresse());
values.put("parcel_date_deb_construct", p.getParcel_date_deb_construct());
values.put("parcel_date_fin_construct", p.getParcel_date_fin_construct());
values.put("quartier_lib", p.getQuartier_lib());
return bd.update(T_PARCELLE, values, "bien_code = ?", new String[] { String.valueOf(p.getBien_code()) });
}
public int getParcelleCount() {
String countQuery = "SELECT count(*) as nb FROM " + T_PARCELLE;
Cursor cursor = bd.rawQuery(countQuery, null);
int nb = cursor.getInt(0);
cursor.close();
return nb;
}
}
即使我在onPostExecute中只写了这一行,那么对话框也不会被忽略:
@Override
protected void onPostExecute(Void result) { // appellé automatiquement quand le tache background est terminé
wait.dismiss();
}
答案 0 :(得分:0)
private class RequestTask extends AsyncTask<String, Void, Void> {
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}
@Override
protected Void doInBackground(String... s_url) {
return null;
}
@Override
protected void onPostExecute(Void result) {
pp.dismiss();
}
}
// // =========================================
public class Main extends Activity {
/** Called when the activity is first created. */
Patienter pp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // This needs to be done before
// trying to findViewById
final View vv = findViewById(R.id.my_webview);
vv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
pp = new Patienter(Main.this, getLayoutInflater());
pp.show();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
new RequestTask().execute();
}
}, 5*1000);
}
});
}
它在我的项目中工作。
请使用wait.dismiss();如果在onPostExecute()方法中有条件。