在多个片段中使用一个类的实例

时间:2019-04-25 09:01:04

标签: java android-fragments interface

在多个片段中使用类的一个实例的最佳方法是什么? (或只是一种方法)

在我的应用程序中,我有一个类RangeFetcher,该类下载XML文件并将其解析为模型类ItemRangeFetcher拥有一个ArrayList<Item> items,其中包含解析为模型实例的整个XML文件。

我正在从我的开始活动ShopFragment创建片段ShopMainViewScreen。在此片段中,我实例化了一个RangeFetcher。由于这在数据量和计算方面是一项非常昂贵的操作,因此我想将此RangeFetcher实例传递给我拥有的其他三个片段。

这种方法 getItems 基本上是我在片段中所需的。

public class RangeFetcher extends AsyncTask {

  ArrayList <Item> items = new ArrayList();

  //filling the List...

  public ArrayList <Item> getItems() {
    return items;
  }
}

到目前为止,我所考虑的是:

  • 我考虑过使用捆绑包并将ArrayList传递给参数,以将实例从一个Fragment传递到另一个Fragment,但这有点笨拙。这会给后堆栈带来麻烦吗?
  • 将方法getItems设为静态?我认为这是可行的,因为RangeFetcher将始终为所有片段产生完全相同的输出。 (我在这里想念东西吗?)
  • 关于如何从多个片段访问此实例,您还有其他建议吗?

RangeFetcher

    package com.XXX;

    import android.os.AsyncTask;

    import org.xmlpull.v1.XmlPullParser;
    import org.xmlpull.v1.XmlPullParserException;
    import org.xmlpull.v1.XmlPullParserFactory;

    import java.io.IOException;
    import java.io.InputStream;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.ArrayList;

    //I know using a switch statement would probably be more fitting here.
    //Furthermore I didn't know how to wait for the background task to
    //finish so i just introduced a boolean "processing". When this boolean
    //is set to false, i continue with my tasks on the fragments.
    //So feel free to suggest some improvements to my code ^^
    public class RangeFetcher extends AsyncTask {

     URL url;
     ArrayList < Item > items = new ArrayList();
     boolean processing = false;
     Item i;

     protected Object doInBackground(Object[] objects) {
      processing = true;

      try {
       url = new URL("XXXX");

       XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
       factory.setNamespaceAware(false);
       XmlPullParser xpp = factory.newPullParser();

       xpp.setInput(getInputStream(url), "UTF_8");


       boolean insideItem = false;
       boolean priceSet = false;

       int eventType = xpp.getEventType();
       while (eventType != XmlPullParser.END_DOCUMENT) {
        if (eventType == XmlPullParser.START_TAG) {

         if (xpp.getName().equalsIgnoreCase("item")) {
          insideItem = true;
          i = new Item();

         } else if (xpp.getName().equalsIgnoreCase("g:id")) {
          if (insideItem)
           i.setmID(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("title")) {
          if (insideItem)
           i.setmTitle(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("description")) {
          if (insideItem)
           i.setmDescription(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:product_type")) {
          if (insideItem) {
           String fullCategory = xpp.nextText();
           if (fullCategory.contains(" ")) {
            fullCategory = fullCategory.substring(0, fullCategory.indexOf(" "));
           }
           i.setmProductType(fullCategory);
          }
         } else if (xpp.getName().equalsIgnoreCase("g:image_link")) {
          if (insideItem)
           i.setmPictureLink(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:condition")) {
          if (insideItem)
           i.setmCondition(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:availability")) {
          if (insideItem)
           i.setmAvailability(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:price") && priceSet == false) {
          if (insideItem)
           i.setmPrice(xpp.nextText());
          priceSet = true;
         } else if (xpp.getName().equalsIgnoreCase("g:brand")) {
          if (insideItem)
           i.setmBrand(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:mpn")) {
          if (insideItem)
           i.setmMpn(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:country")) {
          if (insideItem)
           i.setmShippingCountry(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:service")) {
          if (insideItem)
           i.setmService(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:price")) {
          if (insideItem)
           i.setmShippingCosts(xpp.nextText());
         } else if (xpp.getName().equalsIgnoreCase("g:pubDate")) {
          if (insideItem)
           i.setMpubDate(xpp.nextText());
         }

        } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("item")) {
         insideItem = false;
         items.add(i);
         priceSet = false;
        }
        eventType = xpp.next(); //move to next element
       }
      } catch (MalformedURLException e) {
       e.printStackTrace();
      } catch (XmlPullParserException e) {
       e.printStackTrace();
      } catch (IOException e) {
       e.printStackTrace();
      }
      processing = false;
      return items;
     }

     private InputStream getInputStream(URL url) {
      try {
       return url.openConnection().getInputStream();
      } catch (IOException e) {
       return null;
      }
     }

     public ArrayList < Item > getItems() {
      while (processing == true) {}
      return items;
     }

     public Boolean processing() {
      return processing;
     }
    }

0 个答案:

没有答案