我正在進行延遲加載並幾乎完成了它。但是想要實現一個進度對話框,因爲在開始活動和完成顯示內容之間需要大約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();
}
}
那麼我的情況建議什麼?我嘗試創建一個私人字符串[]。然後將方法更改爲void。但它沒有運行該方法。我需要通過使用AsyncTask來完成什麼?打電話給我的方法?謝謝 – Hend