2014-01-13 33 views
0

这是一个小小的XML动画,我试图在应用程序从服务器获取JSON时关闭它。它需要大约2秒钟来获取所有的数据,所以我试图展示一些加载动画。来自异步任务的Android更新视图

我遇到的问题是图像不会改变,直到一切完成并onPostExecute()熄灭。

我需要传递一个视图到AsyncTask中吗?还是其他的东西?

这个AsyncTask完成后的最后一件事。如果您再次运行它,它会运行平稳。

XML

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
android:oneshot="false"> 
<item android:drawable="@drawable/loadingtext1" android:duration="100" /> 
<item android:drawable="@drawable/loadingtext2" android:duration="100" /> 
<item android:drawable="@drawable/loadingtext3" android:duration="100" /> 
<item android:drawable="@drawable/loadingtext4" android:duration="100" /> 
</animation-list> 

代码

private class LOGMEIN extends AsyncTask<String, Integer, String> { 

      @Override 
      protected String doInBackground(String... params) { 
       CRY.CInit(DI, DU, DP); 
       publishProgress(0); 
       while(data != "whatineed"){ 
        try { 
         Thread.sleep(450); 
        } catch (InterruptedException e) { 
         e.printStackTrace(); 
        } 
       } 
       completecheck = 1; 
       return "complete"; 
      } 

      @Override 
      protected void onPreExecute() { 
       LOADINGTEXT.setBackgroundResource(R.drawable.animation); 
       AnimationDrawable LT = (AnimationDrawable) LOADINGTEXT.getBackground(); 
       LT.start(); 
      } 

      @Override 
      protected void onPostExecute(String result) { 
       super.onPostExecute(result); 
       if (result.contentEquals("complete") == true) { 
        domorework(); 
       } else { 
        finish(); 
       } 
      } 
} 

回答

0

有你的AsyncTask内设置onPreExecute()?

保护无效onPreExecute(){

//显示你的动画装载机这里...
progress.show();

初始化:

}

我的onCreate()使用这个公共ProgressDialog进展;

progress = new ProgressDialog(this); 
progress.setCancelable(true); 
progress.setInverseBackgroundForced(false); 
progress.setCanceledOnTouchOutside(true); 
progress.setMessage("Loading....."); 
+0

我还没有想出来,现在 – Xjasz

0

我不知道这是唯一的问题,但你是比较你的String错在你loop

while (data != "whatineed") 

应该

while (!data.equals("whatineed")) 

甚至更​​好的将是

while (!"whatineed".equals(data)) 

的值进行比较,而不是对象引用

+0

那没有解决它,但我会做出改变。 – Xjasz