【问题标题】:Pass Drawable from Fragment to Dialogfragment using Bundle使用 Bundle 将 Drawable 从 Fragment 传递到 Dialogfragment
【发布时间】:2017-11-18 19:50:09
【问题描述】:

我需要将可绘制的 (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");
...

【问题讨论】:

  • getImageId()) 返回什么?
  • 可绘制。 public Drawable getImageId() { return imageId; } public void setImageId(Drawable imageId) { this.imageId = imageId; }
  • 也许这会有所帮助:stackoverflow.com/questions/8407336/…

标签: android android-fragments bundle drawable


【解决方案1】:

您不能通过 Bundle 传递 Drawable,因为它没有实现 Serializable,而且我认为您不能实现 Parcelable

如果它的 Drawable 已经在你的包中,你可以传递一个字符串或资源 ID,这样你就可以再次查找它。

如果是位图,则需要将其写入本地存储并传递路径。

【讨论】:

  • 嗨科迪,谢谢你的答案,更清楚。这是我从内部存储上的 .apk 文件中抓取的可绘制图标。 (没有安装apk,只是下载了)。我像这样传递它“mydlModel.setImageId(info.applicationInfo.loadIcon(pm));”
【解决方案2】:

如果有人遇到同样的问题,我通过传递适配器的位置来使用“解决方法”:

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());
            }
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-20
    • 2014-02-15
    • 1970-01-01
    • 2016-12-18
    • 2012-10-22
    • 1970-01-01
    相关资源
    最近更新 更多