我有一个应用程序,其中有 4 个屏幕,单击按钮后创建屏幕,每个屏幕都有一个新活动,但活动运行速度太慢(创建 3 秒)。在第二个活动中,我的信息取决于第一个活动,因此在单击按钮之前我无法创建活动。有没有其他方法可以让4个屏幕一个接一个,速度又快?
package your.pack.namespace;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Collection;
import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class ParsActivity extends Activity implements OnClickListener {
private Collection<String> title = new LinkedList<String>();
public Collection<String> desc = new LinkedList<String>();
public NodeList Items = getNodeList();
Collection<String> secTitle = new LinkedList<String>();
Document doc;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setScrollContainer(true);
ll.setBackgroundResource(R.drawable.gradientbackground);
CreateScr(ll,Items);
}
private void CreateScr(LinearLayout l1,NodeList btns){
for(int i=0;i<btns.getLength();i++){
Element eElement= (Element)btns.item(i);
this.title.add(getTagValue("title",eElement));
this.desc.add(getTagValue("description",eElement));
}
btnCreate(title,l1);
this.setContentView(l1);
}
public void btnCreate (Collection<String> name,LinearLayout l1)
{
int k=0;
for(String i:name ){
Button btnNew = new Button(this);
btnNew.setId(k);
//btnNew.setBackgroundResource(R.drawable.bottomrow) ;
btnNew.setText(Html.fromHtml("<b>" + i + "</b>" + "<br />"));
l1.addView(btnNew);
k++;
btnNew.setOnClickListener(this);
}
this.setContentView(l1);
}
public static Document getDocument(String url)
throws MalformedURLException, IOException, Exception {
URL documentUrl = new URL(url);
URLConnection conn = documentUrl.openConnection();
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document;
InputStream stream = null;
try {
stream = conn.getInputStream();
document = builder.parse(stream);
} finally {
if (stream != null) stream.close();
}
return document;
}
public NodeList getNodeList (){
try {
doc = getDocument("http://afishadroid.about.od.ua/main.xml");
} catch (Exception ioe) {
//Обрабатываем ошибку
}
NodeList nd = doc.getElementsByTagName("item");
return nd;
}
public Element getElement (NodeList nl,int id1){
Element eElement = (Element)nl.item(id1);
return eElement;
}
protected static String getTagValue(String sTag, Element eElement) {
NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();
Node nValue = (Node) nlList.item(0);
return nValue.getNodeValue();
}
public void onClick(View v) {
Intent intent = new Intent();
intent.setClass(this, SecondActivity.class);
// intent.putExtra("ID",v.getId());
startActivity(intent);
//Intent intent = new Intent(this, SecondActivity.class) .putExtra("ID", v.getId()); startActivity(intent);
}
}