这里是代码,如果用户点击按钮,当前显示progressdialog,并且'ring'没有旋转。但是如果我将progreedialog的代码粘贴在onCreate下,那么一旦加载了屏幕,'ring'就会旋转。帮我找到哪里出了问题..
StaffChoice类:
public class StaffChoice extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.staffchoices);
}
public void onClickCategory(final View view)
{
final ProgressDialog progress=ProgressDialog.show(this, "Please wait", "Loading ...", true);
new Thread()
{
public void run()
{
Intent intent = new Intent(view.getContext(), Category.class);
startActivity(intent);
progress.dismiss();
}
}.start();
}
}
onCreate in Category class:
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
final ListView lvCategory = (ListView) findViewById(R.id.lvCategory);
SoapObject Request = new SoapObject (NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
soapEnvelope.dotNet = true;
soapEnvelope.setOutputSoapObject(Request);
AndroidHttpTransport aht = new AndroidHttpTransport(URL);
try
{
aht.call(SOAP_ACTION, soapEnvelope);
SoapObject resultString = (SoapObject) soapEnvelope.getResponse();
final String[] strCategory = new String[resultString.getPropertyCount()];
for(int i =0; i<resultString.getPropertyCount(); i++)
{
SoapObject array = (SoapObject) resultString .getProperty(i);
strCategory[i] = array.getProperty(0).toString(); //get category
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strCategory);
lvCategory.setAdapter(adapter);
lvCategory.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> arg0, final View arg1, int arg2, long arg3) {
Intent intent = new Intent(arg1.getContext(), CategoryGames.class);
startActivity(intent);
}
});
}
catch(Exception e)
{
String[] items = { "No Internet Connection, Please try again" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);
lvCategory.setAdapter(adapter);
}
答案 0 :(得分:1)
当线程正在运行时,你必须在线程中放置任务或时间,显示进度对话框,但是当任务/时间完成时,进行对话解除。
最好使用AsynTask in Android而不是Thread。
new Thread()
{
public void run()
{
try{
Thread.sleep(10*1000); //10 seconds
}catch(Exception e){
}
handler.sendEmptyMessage(0);
}
}.start();
答案 1 :(得分:1)
而不是Thread,我在新意图中使用AsyncTask
protected void onCreate(Bundle savedInstanceState)
{
new loadPage().execute(null, null, null);
}
public class loadPage extends AsyncTask<String, Integer, Void> {
private ProgressDialog pdia;
@Override
protected void onPreExecute(){
super.onPreExecute();
pdia = new ProgressDialog(AppDetails.this);
pdia.setMessage("Loading...");
pdia.show();
}
@Override
protected Void doInBackground(String... arg0) {
// TODO Auto-generated method stub
}
@Override
protected void onPostExecute(Void unused) {
pdia.dismiss();
}
}