2016-11-19 168 views
0

我在一个活动(A)中有一个链接列表,我想与另一个活动(B)共享。 该列表包含一个字符串类型的用户名,并包含LatLng类型的坐标。我也使用Intent和捆绑来共享活动之间的数据。我尝试使用Parcelable,但无法弄清楚如何使用它。下面是我的代码有:将LinkedList传递给另一个活动

data.java

public class data implements Parcelable{ 
    private LatLng coordinates; 
    private String name; 


    public data() { 
     name = null; 
     coordinates = null; 
    } 

    public data(String name, LatLng coordinates) 
    { 
     this.name = name; 
     this.coordinates = coordinates; 
    } 

    public data(Parcel in) { 
     coordinates = in.readParcelable(LatLng.class.getClassLoader()); 
     name = in.readString(); 
    } 

    public static final Creator<data> CREATOR = new Creator<data>() { 
     @Override 
     public data createFromParcel(Parcel in) { 
      return new data(in); 
     } 

     @Override 
     public data[] newArray(int size) { 
      return new data[size]; 
     } 
    }; 

    public LatLng getLatLng() { 
     return coordinates; 
    } 

    @Override 
    public int describeContents() { 
     return hashCode(); 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(name); 
     dest.writeParcelable(coordinates, flags); 
    } 
} 

活动A

public class A extends FragmentActivity implements 
    OnMapReadyCallback, 
    GoogleApiClient.ConnectionCallbacks, 
    GoogleApiClient.OnConnectionFailedListener, 
    GoogleMap.OnMyLocationButtonClickListener, 
    ActivityCompat.OnRequestPermissionsResultCallback { 

    Button switchToSeek; 
    double mLatitude; 
    double mLongitude; 

    LinkedList<data> storedData = new LinkedList<>(); 

    protected void onCreate(Bundle savedInstanceState) { 
     ... 
     switchToSeek.setOnClickListener(new View.OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         getCurrentLocation(); 

         Intent intent = new Intent(A.this, B.class); 

         Bundle xy = new Bundle(); 
         xy.putDouble("x", mLatitude); 
         xy.putDouble("y", mLongitude); 
         xy.putParcelable("list", storedData); <---------- error: wrong second arugment 

         intent.putExtra("xy", xy); 
         A.this.startActivity(intent); 
        } 
       }); 

活动B

public class B extends FragmentActivity implements OnMapReadyCallback { 

    double mLatitude; 
    double mLongitude; 
    LatLng current; 
    GoogleMap gMap; 

    LinkedList <data> copyData = new LinkedList<>(); 

    @Override 
     public void onMapReady(GoogleMap googleMap) { 
      gMap = googleMap; 

      ... 

      Intent intent = getIntent(); 
      Bundle xy = intent.getBundleExtra("xy"); 

      if (xy != null) { 
       mLatitude = xy.getDouble("x"); 
       mLongitude = xy.getDouble("y"); 
      } 
      /***** Call linked list here and set equal to copyData *****/ 

      current = new LatLng(mLatitude, mLongitude); 
      gMap.moveCamera(CameraUpdateFactory.newLatLngZoom(current, 18.0f)); 

     } 
+0

您可以将列表转换为数组,然后转换为字符串。将该字符串意图传递给该活动,将其拆分为“,”并将这些元素存储在另一个数组中,最后遍历该数组并将该元素添加到另一个活动中的数组列表中。 – HaroldSer

回答

1

有没有简单的方法来做到这一点,因为链表没有实现可序列化或可分段。

你可以实现自己的链表类,并使其成为一个可序列化的/可供分析的对象,然后可以通过。

或者你可以将其内容转换成另一种数据类型转换如数组,然后重新创建链表。*这是非常低效的

我相信还有其他方法,但是这是在Android开发一个标准的问题。也许尝试使用,如果可能的片段,并通过LinkedList的通过一个setter()

+0

是的,我注意到了。所有我想要使用的bundle或intent的函数都是用于arrayList的。我想转换,但像你说的这是非常低效,特别是如果我想在列表中存储大量数据 –

0

如果列表不是很大,你可以用下面的辅助类做:

public class ParcelableLinkedList<E extends Parcelable> implements Parcelable { 

    private final LinkedList<E> linkedList; 

    public final Creator<ParcelableLinkedList> CREATOR = new Creator<ParcelableLinkedList>() { 
     @Override 
     public ParcelableLinkedList createFromParcel(Parcel in) { 
      return new ParcelableLinkedList(in); 
     } 

     @Override 
     public ParcelableLinkedList[] newArray(int size) { 
      return new ParcelableLinkedList[size]; 
     } 
    }; 

    public ParcelableLinkedList(Parcel in) { 
     // Read size of list 
     int size = in.readInt(); 
     // Read the list 
     linkedList = new LinkedList<E>(); 
     for (int i = 0; i < size; i++) { 
      linkedList.add((E)in.readParcelable(ParcelableLinkedList.class.getClassLoader())); 
     } 

    } 

    public ParcelableLinkedList(LinkedList<E> linkedList) { 
     this.linkedList = linkedList; 
    } 

    LinkedList<E> getLinkedList() { 
     return linkedList; 
    } 

    @Override 
    public int describeContents() { 
     return 0; 
    } 

    @Override 
    public void writeToParcel(Parcel parcel, int flags) { 
     // Write size of the list 
     parcel.writeInt(linkedList.size()); 
     // Write the list 
     for (E entry : linkedList) { 
      parcel.writeParcelable(entry, flags); 
     } 
    } 
} 

在你onClick()方法中,添加数据到Bundle这样的:

xy.putParcelable("list", new ParcelableLinkedList<data>(storedData)); 

要从Bundle提取数据,这样做:

copyData = ((ParcelableLinkedList<data>)xy.getParcelable("list")).getLinkedList(); 

我还没有真正编译和测试过这段代码,但它应该可以工作。

如果列表真的很大,最好将它存储在一个类的static成员变量中,然后仅从另一个类中引用它。这通常不是您想要在Android中执行任务的方式,但是有时候这样做比直接将大量数据序列化和反序列化以在两个可访问同一内存空间的活动之间传递数据更为方便。

+0

我在@Override上得到了java.util.ConcurrentModificationException异常 public void writeToParcel(Parcel parcel,int flags){ //写入列表大小 parcel.writeInt(linkedList.size()); (entry:linkedList){ parcel.writeParcelable(entry,flags); } } –

相关问题