使用 Bundle 将 Drawable 从 Fragment 传递到 Dialogfragment
Pass Drawable from Fragment to Dialogfragment using Bundle
我需要将可绘制对象 (mDLlist.get(position).getImageId()) 从 Fragment 传递到 DialogFragment。
我似乎找不到如何做到这一点的方法,任何输入都很好。
先谢谢你。
...
FragmentManager ft = ((FragmentActivity)context1).getSupportFragmentManager();
DialogFragment newFragment = MyNotification.newInstance();
Bundle args = new Bundle();
// How to pass this value ?
args.put?("appdraw", mDLlist.get(position).getImageId());
//--//
args.putString("appname", (String)mDLlist.get(position).getLabelnameText());
args.putString("appversion", mDLlist.get(position).getVersionName());
args.putString("appinstalltime", "Downloaded");
newFragment.setArguments(args);
newFragment.show(ft, "mydialog");
...
您不能通过 Bundle 传递 Drawable
,因为它没有实现 Serializable
,而且我认为您无法实现 Parcelable
。
如果它是一个已在您的包中的 Drawable,您只需传递一个字符串或资源 ID,以便您可以再次查找它。
如果是位图,您需要将其写入本地存储并传递路径。
如果有人遇到同样的问题,我通过传递适配器的位置使用了 "workaround":
args.putInt("appdrawpos", position);
并将它与我在我写的方法中使用的计数器 (i) 进行比较:
int appDrawPos = 0;
Drawable drawIcon;
int i = 0;
...
//Get the adapter position
appDrawPos = getArguments().getInt("appdrawpos");
//Get apk icon
getAPKicon(folder.getAbsolutePath());
...
public void getAPKicon(String directoryName) {
File directory = new File(directoryName);
final PackageManager pm = getActivity().getPackageManager();
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
//If counter is the same as adapter position
//Grab the icon from the apk
if (i == appDrawPos)
if (file.getName().endsWith(".apk")) {
PackageInfo info = pm.getPackageArchiveInfo(file.getAbsolutePath(), 0);
info.applicationInfo.sourceDir = file.getAbsolutePath();
info.applicationInfo.publicSourceDir = file.getAbsolutePath();
drawIcon = (info.applicationInfo.loadIcon(pm));
appDrawPos = 0;
i = 0;
}
i++;
} else if (file.isDirectory()) {
getAPKicon(file.getAbsolutePath());
}
}
}
我需要将可绘制对象 (mDLlist.get(position).getImageId()) 从 Fragment 传递到 DialogFragment。
我似乎找不到如何做到这一点的方法,任何输入都很好。
先谢谢你。
...
FragmentManager ft = ((FragmentActivity)context1).getSupportFragmentManager();
DialogFragment newFragment = MyNotification.newInstance();
Bundle args = new Bundle();
// How to pass this value ?
args.put?("appdraw", mDLlist.get(position).getImageId());
//--//
args.putString("appname", (String)mDLlist.get(position).getLabelnameText());
args.putString("appversion", mDLlist.get(position).getVersionName());
args.putString("appinstalltime", "Downloaded");
newFragment.setArguments(args);
newFragment.show(ft, "mydialog");
...
您不能通过 Bundle 传递 Drawable
,因为它没有实现 Serializable
,而且我认为您无法实现 Parcelable
。
如果它是一个已在您的包中的 Drawable,您只需传递一个字符串或资源 ID,以便您可以再次查找它。
如果是位图,您需要将其写入本地存储并传递路径。
如果有人遇到同样的问题,我通过传递适配器的位置使用了 "workaround":
args.putInt("appdrawpos", position);
并将它与我在我写的方法中使用的计数器 (i) 进行比较:
int appDrawPos = 0;
Drawable drawIcon;
int i = 0;
...
//Get the adapter position
appDrawPos = getArguments().getInt("appdrawpos");
//Get apk icon
getAPKicon(folder.getAbsolutePath());
...
public void getAPKicon(String directoryName) {
File directory = new File(directoryName);
final PackageManager pm = getActivity().getPackageManager();
File[] fList = directory.listFiles();
for (File file : fList) {
if (file.isFile()) {
//If counter is the same as adapter position
//Grab the icon from the apk
if (i == appDrawPos)
if (file.getName().endsWith(".apk")) {
PackageInfo info = pm.getPackageArchiveInfo(file.getAbsolutePath(), 0);
info.applicationInfo.sourceDir = file.getAbsolutePath();
info.applicationInfo.publicSourceDir = file.getAbsolutePath();
drawIcon = (info.applicationInfo.loadIcon(pm));
appDrawPos = 0;
i = 0;
}
i++;
} else if (file.isDirectory()) {
getAPKicon(file.getAbsolutePath());
}
}
}