在gridLayout上显示默认图标而不是真实图标

时间:2017-04-18 12:35:38

标签: android android-gridlayout

首先,我想原谅自己,英语不是我的母语。 我尝试在网格内膨胀Imageview。这部分工作正常,但是当我从我的AppModel类中检索App并在视图上显示它时,我总是有默认图标而不是真实图标,我无法理解为什么。感谢任何帮助

public class EmptyFragment extends Fragment {

    private ArrayList<AppModel> _model;
    private ImageView[]         _arrayView;

    private final static String LOG_TAG = "DEBUG EMTPTY FRAG";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
        }

        public ArrayList<AppModel> loadApp() {

            final PackageManager mPm = getActivity().getPackageManager();
            List<ApplicationInfo> apps = mPm.getInstalledApplications(0);
            if (apps == null) {
                apps = new ArrayList<ApplicationInfo>();
            }
            final Context context = getContext();
            ArrayList<AppModel> items = new ArrayList<AppModel>(apps.size());
            for (int i = 0; i < apps.size(); i++) {
                String pkg = apps.get(i).packageName;
                if (context.getPackageManager().getLaunchIntentForPackage(pkg) != null) {
                    AppModel app = new AppModel(context, apps.get(i));
                    app.loadLabel(context);
                    items.add(app);
                }
            }
            Collections.sort(items, ALPHA_COMPARATOR);
            return items;
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View view = inflater.inflate(R.layout.gridlayout, container, false);
            GridLayout gridLayout = (GridLayout) view.findViewById(R.id.gridgrid);

            gridLayout.removeAllViews();

            int total = 16;
            int column = 4;
            final PackageManager pute = getActivity().getPackageManager();
            int row = total / column;
            gridLayout.setColumnCount(column);
            gridLayout.setRowCount(row + 1);
            final ArrayList<AppModel> listApp = loadApp();
            if (listApp.size() >= 1) {
                for (int test = 0; test < column; test++) {
                    for (int index = 0; index < row; index++) {
                        Iterator<AppModel> it = listApp.iterator();
                        if (!it.hasNext())
                            return null;
                        else {
                            AppModel m = it.next();
                            ImageView oImageView = new ImageView(getActivity());
                            ViewGroup.LayoutParams param =
                                    new GridLayout.LayoutParams(new ViewGroup.LayoutParams(100, 100));


                            Drawable d = pute.getApplicationIcon(m.getAppInfo());
                            if (d == null)
                                Log.d(LOG_TAG, "DRAW IS NULL");
                            else
                                Log.d(LOG_TAG, "DRAW IS NOT NULL");
                            oImageView.setImageDrawable(d);


                            oImageView.setLayoutParams(param);
                            GridLayout.Spec rowSpan = GridLayout.spec(test, 1);
                            GridLayout.Spec colspan = GridLayout.spec(index, 1);
                            GridLayout.LayoutParams gridParam = new GridLayout.LayoutParams(
                                    rowSpan, colspan);
                            gridParam.setMargins(50, 25, 25, 15);
                            gridLayout.addView(oImageView, gridParam);
                        }
                    }
                }
                return view;
            }
            return null;
        }

0 个答案:

没有答案