2011-12-13 82 views
0

我正在进行延迟加载并几乎完成了它。但是想要实现一个进度对话框,因为在开始活动和完成显示内容之间需要大约10秒。一旦我点击一个按钮开始,它将停留在当前页面(Main.java)约4秒钟,然后移动到下一页面(Activity.java)。然后显示内容大约需要2-4秒钟。Android - 单击按钮后右键单击显示进度对话框

尝试了这里和网上可用的例子,但它们工作不正常(能够显示对话框,但在内容全部下载后无法做适当的解雇)。

问题是,一旦用户点击按钮,我该如何立即实现一个进度指示器?

Activity.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   

    list=(ListView)findViewById(R.id.list);   
    adapter=new LazyAdapter(this, mStrings, dStrings); 
    list.setAdapter(adapter); 
}  

private String[] mStrings = {}; 
private String[] dStrings = {}; 

public Activity() 
{ 
    String imageC = ""; 
    String textC = ""; 

    try { 
     // Get the URL from text box and make the URL object 

     URL url = new URL(targetURL); 

     // Make the connection 
     URLConnection conn = url.openConnection(); 
     BufferedReader reader = new BufferedReader(
     new InputStreamReader(conn.getInputStream())); 

     String line = reader.readLine(); 
     Pattern sChar = Pattern.compile("&.*?;"); 
     Matcher msChar = sChar.matcher(line); 
     while (msChar.find()) line = msChar.replaceAll(""); 

     while (line != null) { 

      if(line.contains("../../")) 
      { 

       int startIndex = line.indexOf("../../") + 6; 
       int endIndex = line.indexOf(">", startIndex + 1); 
       String abc = "http://www.petschannel.com/"; 
       String imageSrc = line.substring(startIndex,endIndex); 
       //complete full url 
       String xyz = abc +imageSrc; 
       xyz = xyz.substring(0,xyz.indexOf('"')); 
       xyz = xyz +";"; 
       imageC += xyz;     
       mStrings = imageC.split(";"); 
       line = reader.readLine(); 
      } 

      if(line.contains("../../") == false) 
      { 
       line = reader.readLine(); 
      } 

      if (line.contains("Gnametag")) 
      { 
       int startIndex = line.indexOf("Gnametag") + 10; 
       int endIndex = line.indexOf("<", startIndex + 1); 
       String gname = line.substring(startIndex,endIndex); 
       textC += "Name: "+gname+ "\n"; 
      }    

      if (line.contains("Last Update")) 
      { 
       reader.close(); 
      }        
     }   

     // Close the reader 
     reader.close(); 

    } catch (Exception ex) { 
     ex.printStackTrace();   
    } 
}  

回答

0

首先,你在主线程上做你的网络呼叫,这是一个典型的没有没有因为性能原因,除其他。切勿在主(ui)线程上执行任何可能耗费时间的操作。

我建议使用AsyncTask,它确保在这种情况下,您的网络调用将在工作线程中完成,并且结果返回到主线程。

AsyncTask有管理进度条的方法,onProgressUpdatepublishProgress这将帮助您解决您陈述的问题。有很多关于此的好文章,here is one

+0

那么我的情况建议什么?我尝试创建一个私人字符串[]。然后将方法更改为void。但它没有运行该方法。我需要通过使用AsyncTask来完成什么?打电话给我的方法?谢谢 – Hend

0

事情是这样的:

final ProgressDialog myDialog = ProgressDialog.show(this, "Title", "Message"); 

Thread thread = new Thread(new Runnable() { 
    public void run() { 
     /* Your code goes here */ 
     mHandler.sendEmptyMessage(0); 
    } 

    private Handler mHandler = new Handler() { 
     public void handleMessage(Message msg) { 
      myDialog.dismiss(); 
     } 
    } 
} 

thread.start();