活动创作速度快

时间:2012-07-03 10:08:28

标签: android android-activity

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


          }




}

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

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

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