2014-12-29 57 views
0

我做了一個應用程序,可以上傳一些數據到我的數據庫。在活動中,用戶將輸入要上傳的數據,我創建了一個ProgressDialog。 ProgressDialog是在onClick()方法中創建的。ProgressDialog沒有在主線程入門

從理論上講,它會被創建,因爲我不想在主UI線程以外的線程中創建ProgerssDialog。 仍然沒有顯示。我不知道爲什麼。

package com.example.demoapp; 

import android.widget.*; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.os.Handler; 
import android.os.Message; 
import android.util.Log; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener{ 

private EditText planID, name, number, address, handsetValue, planAmount, validity, pass, confirm_pass; 
private Button call; 
private SharedPreferences prefs; 
private SharedPreferences.Editor editor; 
private String response; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    prefs = getSharedPreferences("PREF", MODE_PRIVATE); 

    boolean startup = prefs.getBoolean("FIRST_STARTUP", true); 
    if(startup) 
    { 
    planID = (EditText) findViewById(R.id.planIDET); 
    name = (EditText) findViewById(R.id.nameET); 
    number = (EditText) findViewById(R.id.contactET); 
    address = (EditText) findViewById(R.id.addressET); 
    handsetValue = (EditText) findViewById(R.id.handsetValueET); 
    planAmount = (EditText) findViewById(R.id.planAmountET); 
    validity = (EditText) findViewById(R.id.validityET); 
    pass = (EditText) findViewById(R.id.editText1); 
    confirm_pass = (EditText) findViewById(R.id.editText2); 

    call = (Button) findViewById(R.id.submit_Button); 

    planID.setText(""); 
    name.setText(""); 
    number.setText(""); 
    address.setText(""); 
    handsetValue.setText(""); 
    planAmount.setText(""); 
    validity.setText(""); 
    pass.setText(""); 
    confirm_pass.setText(""); 


    call.setOnClickListener(this); 
    } 

    else 
    { 
     Intent intent = new Intent(this, LoginActivity.class); 
     startActivity(intent); 
     finish(); 
    } 


} 


@Override 
public void onClick(View v) { 

    if(pass.getText().toString().equals(confirm_pass.getText().toString())) 
    { 
     ProgressDialog progress = ProgressDialog.show(v.getContext(), "Please wait...", "Connecting to server", true, true); 

     String query = "INSERT INTO user_info (name, password, address, plan_id, contact, handset_value, plan_amount, validity) " 
      + "VALUES('" + name.getText() + "','"+ pass.getText() +"','" + address.getText() + "','" + planID.getText() + "','" + number.getText() + "','" + handsetValue.getText() + "','" + planAmount.getText() + "','" + validity.getText() + "');"; 


    ConnectDBThread connect = new ConnectDBThread(query, Resources.INSERT); 
    Thread t1 = new Thread(connect); 
    t1.start(); 
    //####################################// 
    Log.i("onClick() ThreadName",String.valueOf(Thread.currentThread().getId())); 

    while(true) 
    { 
     try { 
      Thread.sleep(100); 
     } catch (InterruptedException e) 
     {e.printStackTrace();} 



     if(Resources.serverResponse!=null) 
     { 
      Toast.makeText(this, "Registered Successfully", Toast.LENGTH_LONG).show(); 
      Log.i("SERVER", Resources.serverResponse); 
      prefs = getSharedPreferences("PREF", MODE_PRIVATE); 
      editor = prefs.edit(); 
      editor.putBoolean("FIRST_STARTUP", false); 
      editor.putString("USERNAME", name.getText().toString()); 
      editor.putString("USERPASS", pass.getText().toString()); 
      editor.commit(); 

      //--- PROGRESSBAR STOPPED 
      progress.dismiss(); 

      break; 

     } 

    }//LOOP 
    } 

    else 
     Toast.makeText(this, "Password mismatch!", Toast.LENGTH_LONG).show(); 

}// onClick() 


} 

這裏,ConnectDBThread是一個Runnable接口的實現,它連接到服務器並將查詢和數據一起發佈。

如果我在onClick()的If語句之外創建了ProgressDialog,那麼應用程序首先執行查詢。查詢完成後,它顯示ProgressDialog。

請幫幫我。 謝謝。

+0

你做一些必要的檢查,如果一個你點擊的按鈕ID是R.id.submit_Button?線程是否執行?傳球是否正確,如果輸入? – mobilepotato7

+0

是的。一切運行良好。數據安全地上傳到數據庫。但ProgressDialog沒有顯示。 – user3756173

+0

嘗試調試它,因爲我已經檢查了代碼,並且我嘗試在onClick中創建一個進度對話框,就像你所做的那樣,它在我的代碼中工作正常。 –

回答

0

我認爲問題與ProgressDialog的上下文有關。從設計的角度來看,我認爲最好將ProgressDialog聲明爲一個實例變量。

public class YourClass{ 

    private ProgressDialog progressDialog; // instance variable 
    .... 
} 

然後在點擊方法創造這樣的:

// I believe the main issue is with the context you bind to the progressDialog 
// provide the activity as context as an Activity extends Context 
progressDialog= ProgressDialog.show(this, "Please wait...", "Connecting to server", true, true); 

而且最後不要把這樣的OnClick方法內while循環。它會導致按鈕粘滯,這是不希望的,也會使UI無響應。讓你的ConnectDBThread的的AsyncTask並通過從那裏更新用戶界面(如關閉該對話框)onPostExecute一旦後臺進程在doInBackground完成。

這裏是一個建議的解決方案:

public class InsertTask extends AsyncTask<String, Void, String> { 

    ProgressDialog dialog; 
    Context ctx; 

    public InsertTask(Context ctx){ 
     this.ctx = ctx; 
    } 

    protected void onPreExecute(Void result) { 
     // do UI work here 
     dialog = ProgressDialog.show(ctx, "Please wait...", "Connecting to server", true, true); 

    } 

    protected Void doInBackground(String... args) { 


     // do insert operation here ... 
     String username = args[0]; 
     String password = args[1]; 

     ......  
     String query = .... // generate the query 
     String serverResponse = doDBInsert(query); // create a method to run the query and return response 

     return serverResponse; 
    } 

    protected void onPostExecute(String result) { 
     // do UI work here 
     // display your serverResponse(result) somewhere if needed 
     if(dialog!=null && dialog.isShowing()){ 
      dialog.dismiss(); 
     } 
    } 
} 

內部的onClick方法:

//If check condition is met (password = confirm password) 
new InsertTask(getApplicationContext()).execute(username, password, address ...); 
+0

使其實例變量不起作用。 – user3756173

+0

我在If語句之前創建了ProgressDialog,它正在工作。怎麼樣? – user3756173

+0

我相信問題在於代碼中的while循環。您將需要使用AsyncTask來使其工作。你用來完成工作的設計是錯誤的。 – ZakiMak