2016-11-14 67 views
0

我想從我的活動通過Arraylist的定製的Ojects從我的活​​動到我的片段,我試圖通過Parcelable Arraylist傳遞它,但我無法做到任何人都可以幫助? anymethod傳遞數組列表是可接受的從活性片段傳遞陣列列表<CustomObject>從活動到varioud片段使用parcablearraylist

我的代碼

String driverId = jsonObject.getJSONArray("user_data").getJSONObject(count).getString("order_id"); 
String pickupaddress = jsonObject.getJSONArray("user_data").getJSONObject(count).getString("pickup_address"); 
String contact = jsonObject.getJSONArray("user_data").getJSONObject(count).getString("contact"); 
Log.d("HomeAcivity",": "+driverId+","+pickupaddress+","+contact); 
singleton = new Singleton(driverId,pickupaddress,contact); 
userDataParserArrayList.add(singleton); 

FragmentManager fragmentManager = getFragmentManager(); 
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
Jobs jobs = new Jobs(); 
Bundle bundle = new Bundle(); 
bundle.putParcelableArrayList("Order",userDataParserArrayList); 
jobs.setArguments(bundle); 
fragmentTransaction.commit(); 

片段1 oncreateview:檢索

userDataParserArrayList = getArguments().getParcelableArrayList("Order"); 

ParcableClass(),它不是一個單獨的類只有名稱是單

public class Singleton implements Parcelable { 

    private String orderId; 
    private String pickupAddress; 
    private String contact; 


    Singleton(String orderId,String pickupAddress,String contact){ 

     this.orderId = orderId; 
     this.pickupAddress = pickupAddress; 
     this.contact = contact; 
    } 

    protected Singleton(Parcel in) { 
     orderId = in.readString(); 
     pickupAddress = in.readString(); 
     contact = in.readString(); 
    } 

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

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

    public String getOrderId() { 
     return orderId; 
    } 

    public String getPickupAddress() { 
     return pickupAddress; 
    } 

    public String getContact() { 
     return contact; 
    } 

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

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
     dest.writeString(orderId); 
     dest.writeString(pickupAddress); 
     dest.writeString(contact); 
    } 
} 
+0

您不需要將活動的數組列表傳遞給片段。您可以通過片段中的getActivity.arraylist_name來訪問活動的數組列表。 –

+0

「我無法做到」 - >您是否收到某種錯誤? – clownba0t

+0

@bhargav你可以給我一個例子 – bipin

回答

0

@bipin首先,您需要將您的片段添加到ctivity,如下面的代碼

Jobs jobs = new Jobs(); 
FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.add("JOB_FRAGEMENT", jobs).commit(); 

創建後「工作片段」你可以從你的活動訪問的ArrayList像下面的代碼

((YourActivityName)getActivity()).yourArrayList(); in onViewCreated() method; 

然後使用這個列表!

+0

如何將作業對象添加到fragmentTransaction中? – bipin