0

我有一个应用程序,其中有 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);


          }




}
4

2 回答 2

1

如果你想快速加载你的活动,那么你需要在 onCreate 中做的尽可能少。使用 AsyncTask 加载可能需要几秒钟的数据。

于 2012-07-03T10:13:04.767 回答
0

如果您在本地加载数据(意味着本地数据),那么它不会花费太多时间,但是如果您正在显示一些将从服务器获取的数据,请尝试在单独的线程中执行此操作,以便您的 UI 线程不会块,一旦获取数据完成,在 UI 线程中刷新屏幕。

尝试尽快完成您的 onCreate() 并完成所有计算并在 onResume() 或 onPause() 方法中休息。

于 2012-07-03T10:26:02.483 回答