無論我做什麼,我都無法阻止我的AsyncTask
; cancel()
,把所有的工作放在while循環中,然後再打破。無法停止執行的AsyncTask
我的任務是從一個Mysql服務器獲取/發送數據。我知道這是最糟糕的方式,但這是我在java中執行了一些步驟後的第一次嘗試。
所以我想執行並完成所有工作,並在顯示佈局之前停止在onCreate()
(在setContentView()
之前)。因爲,如果我點擊一個按鈕,使用數據庫中的數據來填充微調器可能還沒有被下載。
我不想檢查我的arraylist,我用來填補微調,如果它是「空」導致工作不正常。
所以,如果我能做到這一點,我可以顯示一些關於連接失敗的警告給用戶。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
connect2 cnc=new connect2();
cnc.execute();
if (cnc.getStatus()==AsyncTask.Status.FINISHED)
{
setContentView(R.layout.window2);
}
}
private class connect2 extends AsyncTask<Void , Void, Void> {
@Override
protected Void doInBackground(Void... params) {
try {
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(a, b, c);
PreparedStatement prs = conn.prepareStatement("select * from film , kategori,yonetmen where film.kategori_id=kategori.id and film.yonetmen_id=yonetmen.id");
ResultSet rs = prs.executeQuery();
PreparedStatement prs2 = conn.prepareStatement("select * from kategori ");
ResultSet rs2 = prs2.executeQuery();
PreparedStatement prs3 = conn.prepareStatement("select * from yonetmen ");
ResultSet rs3 = prs3.executeQuery();
while (rs.next()) {
Kategori k = new Kategori(rs.getInt("kategori.id"), rs.getString("kategori.ad"));
Yonetmen y = new Yonetmen(rs.getInt("yonetmen.id"), rs.getString("yonetmen.ad"));
Film f = new Film(rs.getInt("id"), rs.getString("ad"), rs.getInt("sene"), k, y);
ab.add(f);
}
rs.close();
size = ab.size();
while (rs2.next()) {
Kategori k = new Kategori(rs2.getInt("kategori.id"), rs2.getString("kategori.ad"));
ac.add(k);
}
rs2.close();
while (rs3.next()) {
Yonetmen k = new Yonetmen(rs3.getInt("yonetmen.id"), rs3.getString("yonetmen.ad"));
ad.add(k);
}
rs3.close();
switch (sw) {
case 1:
StringBuilder cmd = new StringBuilder();
cmd.append("insert into film (ad,sene,yonetmen_id,kategori_id) values ('");
cmd.append(fa + "',");
cmd.append(ya + ",");
cmd.append(idy + ",");
cmd.append(idk + ")");
sw = 0;
try {
PreparedStatement sql = conn.prepareStatement(cmd.toString());
rslt = sql.executeUpdate();
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 2:
try {
PreparedStatement sql = conn.prepareStatement("delete from film where id=" + idy);
rslt2 = sql.executeUpdate();
sw = 0;
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
default:
break;
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return null;
} @Override
protected void onPostExecute(Void aVoid) {
list=ab;
listk=ac;
listy=ad;
if (rslt==1) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setMessage("Film Başarıyla Kaydedildi");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Tamam",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
rslt=0;
}
if (rslt2==1) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(MainActivity.this);
builder1.setMessage("Silindi");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Tamam",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
rslt2=0;
}
super.onPostExecute(aVoid);
}
謝謝, 但我在哪裏呼叫cancel()? –
@ Gogo-the-Cat:你必須調用它才能返回true,如果它被取消 –
通過你不能返回一個值的方式,如果它在一個塊內。 –