我正在尝试将一个arraylist从一个活动传递到另一个活动的片段。我搜索了很多解决方案,但无法得到合适的答案。
这是我传递arraylist的地方。
Intent intent = new Intent(SizeActivity.this,CartActivity.class);
intent.putExtra("PrintList", photoPrintsList);
startActivity(intent);
使用片段接受活动。
public class ExampleActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
if (savedInstanceState == null) {
Fragment newFragment = new CartActivityFragment();
newFragment.setArguments(getIntent().getExtras());
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.commit();
}
}
这就是碎片。如何将数据从一个活动传递到另一个活动。
private ArrayList<PhotoPrints> printsArrayList = new ArrayList<>();
private String TAG = "CartActivityFragment";
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cart, container, false);
return view;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Bundle b = getArguments();
if(b != null) {
Log.d(TAG,b.size() + "");
printsArrayList = b.getParcelableArrayList("PrintList");
Log.d(TAG,"Got arraylist");
} else {
Log.d(TAG,"Null");
}
}
编辑:
我发现片段的onCreateView在保持活动的onCreate之前被调用。这样我就无法将数据从活动中继到片段。看一下Log。
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivityFragment﹕ Null checker
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivityFragment﹕ Null
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ Received Arraylist
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 0 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150913-WA0014.jpg
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 0 4 X 6
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 0 Glossy
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 0 Economy
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 0 6
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 1 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150913-WA0015.jpg
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 1 4 X 6
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 1 Glossy
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 1 Economy
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 1 6
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 2 /storage/emulated/0/WhatsApp/Media/WhatsApp Images/Sent/IMG-20150915-WA0003.jpg
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 2 4 X 6
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 2 Glossy
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 2 Economy
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ 2 6
09-21 17:21:32.539 22672-22672/in.reduxpress D/CartActivity﹕ Size of bundle 1
如何绕过此行为?
Edit2:我已经进行了广泛的测试。我能够成功地接收活动中的数据。我把它放在一个包中,并将bundle设置为参数。但我无法在片段中收到捆绑包。它总是给我一个空值。
我尝试将getArguments放在onCreateView和onActivityCreated中,两者都不起作用。请帮忙。
答案 0 :(得分:0)
试试这个,
这是我传递arraylist的代码:
Intent intent = getIntent();
intent.putExtra("key", selectImages);
setResult(RESULT_OK, intent);
finish();
这里是onActivityResult()。
public void onActivityResult(int requestCode, int resultCode, Intent data){
if (requestCode == 999) {
@SuppressWarnings("unchecked")
ArrayList<String> ar1 = data.getStringArrayListExtra("key");
if(ar1.size()!= 0){
for (int i = 0; i < ar1.size(); i++) {
String value = ar1.get(i);
Toast.makeText(getActivity(),
"Path of array in home Fragment: "+ar1,
Toast.LENGTH_LONG).show();
} } } }
这里的Myparcelable类代码:
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
public class ObjectA implements Parcelable {
public ArrayList<String> choices;
public ObjectA (ArrayList<String> choices) {
this.choices = choices;
}
@SuppressWarnings("unchecked")
public ObjectA (Parcel parcel) {
parcel.readArrayList(null);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeStringList(choices);
}
// Method to recreate a ObjectA from a Parcel
public static Creator<ObjectA> CREATOR = new Creator<ObjectA>() {
@Override
public ObjectA createFromParcel(Parcel source) {
return new ObjectA(source);
}
@Override
public ObjectA[] newArray(int size) {
return new ObjectA[size];
}
};
}
那就是它。