2016-11-19 167 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); } } –

相關問題