Android 如何将TypedArray传递到一个包中
有人能解释一下我们如何在Android中将Android 如何将TypedArray传递到一个包中,android,android-fragments,bundle,typed-arrays,Android,Android Fragments,Bundle,Typed Arrays,有人能解释一下我们如何在Android中将类型Darray传递到一个包中吗? 我正在使用TypedArray将@Drawable中的一些图像存储在资源文件中,我想将该数组传递给一个带有setArguments的片段,该片段接受一个包 代码段: { fragmentTransaction = getSupportFragmentManager().fragmentManager.beginTransaction(); Fragment f = new fFragment();
类型Darray
传递到一个包中吗?
我正在使用TypedArray
将@Drawable
中的一些图像存储在资源文件中,我想将该数组传递给一个带有setArguments
的片段,该片段接受一个包
代码段:
{
fragmentTransaction = getSupportFragmentManager().fragmentManager.beginTransaction();
Fragment f = new fFragment();
fImages = (Parcelable)getResources().obtainTypedArray(R.array.fImages);
fNames = getResources().getStringArray(R.array.fNames);
Bundle b = new Bundle();
b.putStringArray("name", fNames);
b.putParcelable("image", fImages);
sub.setArguments(b);
fragmentTransaction.replace(R.id.mainFrameLayout, f).commit();
}
对pass TYPE DARRAY使用toString()
方法,查看
TypedArray typedArray = null;
Bundle bundle = new Bundle();
bundle.putString("myArray",typedArray.toString());
您不需要传递TypeArray。只需将资源id传递给下一个片段。然后,下一个片段必须从资源中获取TypeArray 在片段1中
Bundle bundle = new Bundle();
bundle.putInt("TYPE_ARRAY_RES_ID", R.array.fNames);
final Fragment f = new Fragment2();
f.setArguments(b);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.mainFrameLayout, f)
.commit();
在片段2中
@ArrayRes int typeArrayRes = getArguments().getInt("TYPE_ARRAY_RES_ID");
String[] stringArray = getResources().getStringArray(typeArrayRes);
// TODO
您能添加一些代码吗?似乎有更好的方法可以做到这一点?创建一个可包裹的对象来保存该数据instead@cricket_007谢谢你的建议,但我试过了,不合身。当代码到达将键/值插入捆绑包的行时,它崩溃。我将把代码片段上传到moment@Eselfar我为您的请求添加了一段代码片段,这应该是显而易见的,现在,如何将其作为TypedArray输出?获取为字符串,但已手动拆分字符串,并在TypedArray中再次将其设置为。@HemantParmar感谢您的建议,但我有一些疑问,因为TypedArray是以二进制方式存储的整数集合。将它转换回来可能不会有好的结果:只要稍微改变一下传递资源id的方法,我的问题就解决了。问题是数组的类型是int而不是string。谢谢