Example usage for android.widget GridView setAdapter

List of usage examples for android.widget GridView setAdapter

Introduction

In this page you can find the example usage for android.widget GridView setAdapter.

Prototype

@Override
public void setAdapter(ListAdapter adapter) 

Source Link

Document

Sets the data behind this GridView.

Usage

From source file:io.github.sunggu.searchimage.ui.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.image_search_fragment, container, false);

    edtSearchKeyword = (EditText) v.findViewById(R.id.search_keyword);
    edtSearchKeyword.addTextChangedListener(new TextWatcher() {
        @Override/*from  w  w w.j a  v  a2 s . c  o  m*/
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void afterTextChanged(Editable s) {
            OpenApi.getInstance().requestSearchImage(s.toString(), mResponseListener, mErrorListener);
        }
    });

    final GridView mGridView = (GridView) v.findViewById(R.id.search_result_grid);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Before Honeycomb pause image loading on scroll to help with performance
                if (!Utils.hasHoneycomb()) {
                    mImageFetcher.setPauseWork(true);
                }
            } else {
                mImageFetcher.setPauseWork(false);
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
        }
    });

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @TargetApi(VERSION_CODES.JELLY_BEAN)
        @Override
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                    if (Utils.hasJellyBean()) {
                        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            }
        }
    });

    return v;
}

From source file:com.wulian.icam.view.album.AlbumGridFragment.java

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

    final View v = inflater.inflate(R.layout.fragment_album_grid, container, false);
    (v.findViewById(R.id.titlebar_back)).setOnClickListener(new View.OnClickListener() {

        @Override// w  w  w.  ja va 2 s .c  om
        public void onClick(View v) {
            AlbumGridFragment.this.getActivity().finish();
        }
    });
    ((TextView) v.findViewById(R.id.titlebar_title)).setText(mAlbumEntity.getDeviceName());
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Before Honeycomb pause image loading on scroll to help
                // with performance
                if (!Utils.hasHoneycomb()) {
                    mImageResizer.setPauseWork(true);
                }
            } else {
                mImageResizer.setPauseWork(false);
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
        }
    });

    // This listener is used to get the final width of the GridView and then
    // calculate the
    // number of columns and the width of each column. The width of each
    // column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used
    // to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @TargetApi(VERSION_CODES.JELLY_BEAN)
        @Override
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (Utils.hasJellyBean()) {
                        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            }
        }
    });

    return v;
}

From source file:br.com.bioscada.apps.biotracks.fragments.ChooseActivityTypeDialogFragment.java

public static Dialog getDialog(final Activity activity, final String category,
        final ChooseActivityTypeCaller caller) {
    View view = activity.getLayoutInflater().inflate(R.layout.choose_activity_type, null);
    GridView gridView = (GridView) view.findViewById(R.id.choose_activity_type_grid_view);
    final View weightContainer = view.findViewById(R.id.choose_activity_type_weight_container);

    TextView weightLabel = (TextView) view.findViewById(R.id.choose_activity_type_weight_label);
    weightLabel.setText(PreferencesUtils.isMetricUnits(activity) ? R.string.description_weight_metric
            : R.string.description_weight_imperial);

    final TextView weight = (TextView) view.findViewById(R.id.choose_activity_type_weight);

    List<Integer> imageIds = new ArrayList<Integer>();
    for (String iconValue : TrackIconUtils.getAllIconValues()) {
        imageIds.add(TrackIconUtils.getIconDrawable(iconValue));
    }/*from w w  w  .j a  v a 2s  .  co m*/

    Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(activity.getResources(), R.drawable.ic_track_airplane, options);
    int padding = 32;
    int width = options.outWidth + 2 * padding;
    int height = options.outHeight + 2 * padding;
    gridView.setColumnWidth(width);

    final ChooseActivityTypeImageAdapter imageAdapter = new ChooseActivityTypeImageAdapter(activity, imageIds,
            width, height, padding);
    gridView.setAdapter(imageAdapter);

    final String weightValue = StringUtils.formatWeight(PreferencesUtils.getWeightDisplayValue(activity));
    final AlertDialog alertDialog = new AlertDialog.Builder(activity)
            .setNegativeButton(R.string.generic_cancel, null)
            .setPositiveButton(R.string.generic_ok, new Dialog.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    boolean newWeight = false;
                    if (weightContainer.getVisibility() == View.VISIBLE) {
                        String newValue = weight.getText().toString();
                        if (!newValue.equals(weightValue)) {
                            newWeight = true;
                            PreferencesUtils.storeWeightValue(activity, newValue);
                        }
                    }
                    int selected = imageAdapter.getSelected();
                    caller.onChooseActivityTypeDone(TrackIconUtils.getAllIconValues().get(selected), newWeight);
                }
            }).setTitle(R.string.track_edit_activity_type_hint).setView(view).create();
    alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialog) {
            int position = getPosition(activity, category);
            alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(position != -1);
            if (position != -1) {
                imageAdapter.setSelected(position);
                imageAdapter.notifyDataSetChanged();
            }
            updateWeightContainer(weightContainer, position);
            weight.setText(weightValue);
            DialogUtils.setDialogTitleDivider(activity, alertDialog);
        }
    });

    gridView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            alertDialog.getButton(Dialog.BUTTON_POSITIVE).setEnabled(true);
            imageAdapter.setSelected(position);
            imageAdapter.notifyDataSetChanged();
            updateWeightContainer(weightContainer, position);
        }
    });
    return alertDialog;
}

From source file:com.android.gpstest.GpsStatusFragment.java

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

    mRes = getResources();//from  w ww.  ja  v  a2 s. co m
    View v = inflater.inflate(R.layout.gps_status, container, false);

    mLatitudeView = (TextView) v.findViewById(R.id.latitude);
    mLongitudeView = (TextView) v.findViewById(R.id.longitude);
    mFixTimeView = (TextView) v.findViewById(R.id.fix_time);
    mTTFFView = (TextView) v.findViewById(R.id.ttff);
    mAltitudeView = (TextView) v.findViewById(R.id.altitude);
    mAccuracyView = (TextView) v.findViewById(R.id.accuracy);
    mSpeedView = (TextView) v.findViewById(R.id.speed);
    mBearingView = (TextView) v.findViewById(R.id.bearing);

    mLatitudeView.setText(EMPTY_LAT_LONG);
    mLongitudeView.setText(EMPTY_LAT_LONG);

    flagUsa = getResources().getDrawable(R.drawable.ic_flag_usa);
    flagRussia = getResources().getDrawable(R.drawable.ic_flag_russia);
    flagJapan = getResources().getDrawable(R.drawable.ic_flag_japan);
    flagChina = getResources().getDrawable(R.drawable.ic_flag_china);

    GridView gridView = (GridView) v.findViewById(R.id.sv_grid);
    mAdapter = new SvGridAdapter(getActivity());
    gridView.setAdapter(mAdapter);
    gridView.setFocusable(false);
    gridView.setFocusableInTouchMode(false);

    GpsTestActivity.getInstance().addListener(this);

    return v;
}

From source file:com.entertailion.android.launcher.Dialogs.java

/**
 * Display dialog to allow user to add an app to a row. The user can add the
 * app to an existing row or a new row./*ww  w  . j  a v  a2 s .co  m*/
 * 
 * @param context
 * @param applications
 */
public static void displayAddApps(final Launcher context, final ArrayList<ApplicationInfo> applications) {
    final Dialog dialog = new Dialog(context);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setContentView(R.layout.add_apps_grid);

    final EditText nameEditText = (EditText) dialog.findViewById(R.id.rowName);
    final RadioButton currentRadioButton = (RadioButton) dialog.findViewById(R.id.currentRadio);
    currentRadioButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // hide the row name edit field if the current row radio button
            // is selected
            nameEditText.setVisibility(View.GONE);
        }

    });
    final RadioButton newRadioButton = (RadioButton) dialog.findViewById(R.id.newRadio);
    newRadioButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // show the row name edit field if the new radio button is
            // selected
            nameEditText.setVisibility(View.VISIBLE);
            nameEditText.requestFocus();
        }

    });
    final GridView gridView = (GridView) dialog.findViewById(R.id.grid);
    gridView.setAdapter(new AllItemAdapter(context, getApplications(context, applications, true)));
    gridView.setOnItemClickListener(new android.widget.AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            // if the new row radio button is selected, the user must enter
            // a name for the new row
            String name = nameEditText.getText().toString().trim();
            if (newRadioButton.isChecked() && name.length() == 0) {
                nameEditText.requestFocus();
                displayAlert(context, context.getString(R.string.dialog_new_row_name_alert));
                return;
            }
            ItemInfo itemInfo = (ItemInfo) parent.getAdapter().getItem(position);
            boolean currentRow = !newRadioButton.isChecked();
            context.addItem(itemInfo, currentRow ? null : name);
            context.showCover(false);
            dialog.dismiss();
            if (currentRow) {
                Analytics.logEvent(Analytics.DIALOG_ADD_APP);
            } else {
                Analytics.logEvent(Analytics.ADD_APP_WITH_ROW);
            }
        }

    });
    gridView.setDrawingCacheEnabled(true);
    gridView.setOnKeyListener(onKeyListener);
    dialog.setOnDismissListener(new OnDismissListener() {

        @Override
        public void onDismiss(DialogInterface dialog) {
            context.showCover(false);
        }

    });
    context.showCover(true);
    dialog.show();
    Analytics.logEvent(Analytics.DIALOG_ADD_APP);
}

From source file:org.xbmc.android.remotesandbox.ui.phone.DashboardFragment.java

private void setupDashboardItems(GridView menuGrid) {

    final ArrayList<HomeItem> homeItems = new ArrayList<HomeItem>();
    final SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getActivity().getApplicationContext());
    if (prefs.getBoolean("setting_show_home_music", true))
        homeItems.add(new HomeItem(HOME_ACTION_MUSIC, R.drawable.home_ic_music, "Music", "Listen to"));

    menuGrid.setAdapter(new HomeAdapter(getActivity(), homeItems));
    menuGrid.setOnItemClickListener(getHomeMenuOnClickListener());
    menuGrid.setSelected(true);//from w  w  w .j  a  v a 2s  .co  m
    menuGrid.setSelection(0);
}

From source file:org.bwgz.quotation.fragment.HomeSubjectsFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    super.onCreateView(inflater, container, savedInstanceState);
    Log.d(TAG, String.format("onCreateView - this: %s  inflater: %s  container: %s  savedInstanceState: %s",
            this, inflater, container, savedInstanceState));

    GridView gridView = (GridView) inflater.inflate(R.layout.standard_grid_view, container, false);

    picksView = (PicksView) inflater.inflate(R.layout.subject_picks_unexpanded_view, null);
    picksView.setActivityClass(SubjectActivity.class);
    initLoader(this, PickSubject.CONTENT_URI, new String[] { Subject.FULL_ID, Subject.NAME, Subject.DESCRIPTION,
            Subject.IMAGE_ID, Subject.QUOTATION_COUNT, BookmarkSubject.BOOKMARK_ID }, null, null, null);

    gridView.setAdapter(new ActivityAdapter(new View[] { picksView }));

    return gridView;
}

From source file:com.example.android.bitmapfun.ui.SearchStreamGridFragment.java

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

    final View v = inflater.inflate(R.layout.search_image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridViewS);
    mGridView.setAdapter(mAdapter);

    final EditText editText = (EditText) v.findViewById(R.id.editText1);
    editText.setText(searchCriteria);//from   ww w  .  ja  va2  s.  c o m

    TextView streamText = (TextView) v.findViewById(R.id.textView1);
    streamText.setText(String.valueOf(streamList.size()) + " results for: " + searchCriteria
            + ", click on an image to view stream...");

    Button button = (Button) v.findViewById(R.id.button_searchS);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button click
            String newSearchCriteria = editText.getText().toString();
            Intent intent = new Intent(getActivity(), SearchStreamGridActivity.class);
            intent.putExtra(SearchStreamGridActivity.STREAM_SEARCH, newSearchCriteria);
            startActivity(intent);
        }
    });

    button = (Button) v.findViewById(R.id.button_streams);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button click
            Intent intent = new Intent(getActivity(), ImageGridActivity.class);
            startActivity(intent);
        }
    });

    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                mImageFetcher.setPauseWork(true);
            } else {
                mImageFetcher.setPauseWork(false);
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
        }
    });

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {

                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth =
                            //(mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                            (mGridView.getWidth() / 4) - mImageThumbSpacing;
                    //mAdapter.setNumColumns(numColumns);
                    mAdapter.setNumColumns(4);
                    mAdapter.setItemHeight(columnWidth);
                    //ViewGroup.LayoutParams params = new GridView.LayoutParams(mGridView.getWidth(), 4*(columnWidth + mImageThumbSpacing));
                    //mGridView.setLayoutParams(params);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                } /* MM: change to portrait mode 4 rows*/

            }
        }
    });

    return v;
}

From source file:net.wespot.pim.view.InqImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.fragment_data_collect_grid, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override//w w w .  j  a  v  a2s.  c  om
        public void onScrollStateChanged(AbsListView absListView, int scrollState) {
            // Pause fetcher to ensure smoother scrolling when flinging
            if (scrollState == AbsListView.OnScrollListener.SCROLL_STATE_FLING) {
                // Before Honeycomb pause image loading on scroll to help with performance
                if (!Utils.hasHoneycomb()) {
                    mImageFetcher.setPauseWork(true);
                }
            } else {
                mImageFetcher.setPauseWork(false);
            }
        }

        @Override
        public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount,
                int totalItemCount) {
        }
    });

    // This listener is used to get the final width of the GridView and then calculate the
    // number of columns and the width of each column. The width of each column is variable
    // as the GridView has stretchMode=columnWidth. The column width is used to set the height
    // of each view so we get nice square thumbnails.
    mGridView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @TargetApi(VERSION_CODES.JELLY_BEAN)
        @Override
        public void onGlobalLayout() {
            if (mAdapter.getNumColumns() == 0) {
                final int numColumns = (int) Math
                        .floor(mGridView.getWidth() / (mImageThumbSize + mImageThumbSpacing));
                if (numColumns > 0) {
                    final int columnWidth = (mGridView.getWidth() / numColumns) - mImageThumbSpacing;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                    if (Utils.hasJellyBean()) {
                        mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    } else {
                        mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                    }
                }
            }
        }
    });

    return v;
}

From source file:com.ylsg365.pai.activity.dialog.EmojiconGridFragment.java

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    GridView gridView = (GridView) view.findViewById(R.id.Emoji_GridView);
    Bundle bundle = getArguments();/*from  ww  w.  j  a v  a2 s.  c o m*/
    if (bundle == null) {
        mData = People.DATA;
        mUseSystemDefault = false;
    } else {
        Object[] o = (Object[]) getArguments().getSerializable("emojicons");
        mData = Arrays.asList(o).toArray(new Emojicon[o.length]);
        mUseSystemDefault = bundle.getBoolean(USE_SYSTEM_DEFAULT_KEY);
    }
    gridView.setAdapter(new EmojiAdapter(view.getContext(), mData, mUseSystemDefault));
    gridView.setOnItemClickListener(this);
}