java - putParcelableArrayListExtra 和黑屏

标签 java android parcelable

我有一个实现Parcelable的自定义类,并将其用作自定义数组列表。

当我使用 putParcelableArrayListExtra 和 400 行时,它工作正常,但 1000 行则不行。我的屏幕黑屏并且应用程序锁定。怎么了?

编辑: 我将其发送到此处,并且没有在其他 Activity 中使用它。

Intent intent = new Intent().setClass(getApplicationContext(), ArtActivity.class);
intent.putParcelableArrayListExtra ("mylist", list);
startActivityForResult(intent, SECONDARY_ACTIVITY_REQUEST_CODE);  

我的数组:

ArrayList<Piece> list = new ArrayList<Piece>();

这是我的类(class):

public class Piece implements Parcelable { 
    private String id;
    private String name;
    private int type;
    private String text;
    private String mp3;

   public Piece (String id,String name,int type)
   {
     this.id=id;
     this.name=name;
     this.type=type;
   }

   public Piece(Piece ele)
   {
      this.id=ele.id;
      this.name=ele.name;
      this.type=ele.type;
      this.text=ele.text;
   }

   public Piece (Parcel in) 
   { 
        id = in.readString (); 
        name = in.readString (); 
        type = in.readInt();
        text= in.readString();
        mp3=in.readString();
   } 

   public static final Parcelable.Creator<Piece> CREATOR 
   = new Parcelable.Creator<Piece>()  
  { 
        public Piece createFromParcel(Parcel in)  
        { 
            return new Piece(in); 
        } 

        public Piece[] newArray (int size)  
        { 
            return new Piece[size]; 
        } 
   }; 


   public void makeText(String text)
   {
       this.text=text;
   }



   public void makeMp3(String mp3)
   {
     this.mp3= mp3;
   }

   public String getMp3()
   {
   return this.mp3;
   }

   public String getId()
   {
       return id;
   }
   public String getName()
   {
       return name;
   }
   public int getType()
   {
       return type;
   }
   public String getText()
   {
       return text;
   }

  public int describeContents() {
    // TODO Auto-generated method stub
    return 0;
  }

  public void writeToParcel(Parcel dest, int flags) {
    // TODO Auto-generated method stub
    dest.writeString (id); 
    dest.writeString (name);
    dest.writeInt(type);
    dest.writeString (text); 
    dest.writeString (mp3);
  } 
}

最佳答案

我认为在这种情况下您不应该使用 Parcelable。我要么静态地访问数据(如果您只想拥有一个持久的数据实例),要么使用缓存系统来保存数据。

这是公开可用的静态变量的示例:

public static List<Piece> list;

可以从应用程序中具有该类可见性的任何位置访问它。

但是,这样做非常困惑,并且被认为是一种不好的做法。或者,您可以创建一个对象来作为静态类或单例来管理数据:

public class MyListManager {
    private static List<Piece> mList;

    public static List<Piece> getMyList() {
        return mList;
    }

    public static void setList(List<Piece> list) {
        mList = list;
    }
}

或者,您可以实现某种缓存系统来管理您的数据。

关于java - putParcelableArrayListExtra 和黑屏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11216608/

相关文章:

java - 安卓/Java : How to get last row value in sqlite?

android - 扩展 Parcelable 以读取另一个 Parcelable

android - 在android中的activity之间传递对象为什么要Parcelable?为什么不是 JSON 字符串?

java - hibernate 多对多加入条件

java - 如何在 h2 db 中存储 LocalDateTime?

Android Ndk Basic 关于 native 字符串

android - 更改youtube api android的进度栏

java - Parcelable 协议(protocol)要求该类实现 Parcelable

Java并发使电梯停在附近楼层

java - 无法解析符号 'v' 和预期的 ) 和 ;