我做了一個應用程序,可以上傳一些數據到我的數據庫。在活動中,用戶將輸入要上傳的數據,我創建了一個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。
請幫幫我。 謝謝。
你做一些必要的檢查,如果一個你點擊的按鈕ID是R.id.submit_Button?線程是否執行?傳球是否正確,如果輸入? – mobilepotato7
是的。一切運行良好。數據安全地上傳到數據庫。但ProgressDialog沒有顯示。 – user3756173
嘗試調試它,因爲我已經檢查了代碼,並且我嘗試在onClick中創建一個進度對話框,就像你所做的那樣,它在我的代碼中工作正常。 –