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:com.manning.androidhacks.hack040.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);//from  w  ww . ja va2s. com
    mGridView.setOnItemClickListener(this);

    // 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;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }
                }
            }
        }
    });

    return v;
}

From source file:com.simplifynowsoftware.flickrdemo.UserFragment.java

protected void showPhotosets(Photosets photosets) {
    if (null == photosets) {
        if (FlickrDemoConstants.DEBUG_ENABLE) {
            Log.w("showPhotosets", "Failed to show photosets, null input");
        }/*from w  w w .  j a v a 2  s . c  o  m*/
    } else {
        final GridView gridView = (GridView) getActivity().findViewById(R.id.userFragment)
                .findViewById(R.id.photosetGridView);

        gridView.setAdapter(new PhotosetAdapter(getActivity(), photosets));

        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                // Now, show the photoset
                Photoset photoset = (Photoset) gridView.getItemAtPosition(position);

                if (FlickrDemoConstants.DEBUG_ENABLE) {
                    Log.i("showPhotosets", "Clicked on photoset id " + photoset.getId());
                }

                final Intent intent = new Intent(getActivity(), PhotosetViewer.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent.putExtra(PhotosetViewer.INTENT_EXTRA_PHOTOSET_ID, photoset.getId());
                intent.putExtra(PhotosetViewer.INTENT_EXTRA_USER_ID, mUserId);
                startActivity(intent);
            }
        });
    }
}

From source file:mad.codeforcommunity.CalendarView.java

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calendar);/*from www . j  a  va 2 s . c o  m*/
    month = Calendar.getInstance();
    //  onNewIntent(getIntent());

    items = new ArrayList<String>();
    adapter = new CalendarAdapter(this, month);

    GridView gridview = (GridView) findViewById(R.id.gridview);
    gridview.setAdapter(adapter);

    handler = new Handler();
    handler.post(calendarUpdater);

    TextView title = (TextView) findViewById(R.id.title);
    title.setText(android.text.format.DateFormat.format("MMMM yyyy", month));

    TextView previous = (TextView) findViewById(R.id.previous);
    previous.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (month.get(Calendar.MONTH) == month.getActualMinimum(Calendar.MONTH)) {
                month.set((month.get(Calendar.YEAR) - 1), month.getActualMaximum(Calendar.MONTH), 1);
            } else {
                month.set(Calendar.MONTH, month.get(Calendar.MONTH) - 1);
            }
            refreshCalendar();
        }
    });

    TextView next = (TextView) findViewById(R.id.next);
    next.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            if (month.get(Calendar.MONTH) == month.getActualMaximum(Calendar.MONTH)) {
                month.set((month.get(Calendar.YEAR) + 1), month.getActualMinimum(Calendar.MONTH), 1);
            } else {
                month.set(Calendar.MONTH, month.get(Calendar.MONTH) + 1);
            }
            refreshCalendar();

        }
    });

    gridview.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            TextView date = (TextView) v.findViewById(R.id.date);
            ImageView image = (ImageView) v.findViewById(R.id.date_icon);
            if (date instanceof TextView && !date.getText().equals("")) {
                if (image.getVisibility() == View.VISIBLE) {
                    /*OLD CODE
                     * Intent intent = new Intent();
                    String day = date.getText().toString();
                    if(day.length()==1) {
                       day = "0"+day;
                    }
                    // return chosen date as string format 
                    intent.putExtra("date", android.text.format.DateFormat.format("yyyy-MM", month)+"-"+day);
                    setResult(RESULT_OK, intent);
                    finish();
                    */

                    Bundle extras = new Bundle();
                    String day = date.getText().toString();
                    if (day.length() == 1) {
                        day = "0" + day;
                    }
                    // return chosen date as string format 
                    extras.putString("date",
                            android.text.format.DateFormat.format("yyyy-MM", month) + "-" + day);

                    Intent intent = new Intent(CalendarView.this, EventView.class);
                    intent.putExtras(extras);

                    startActivity(intent);
                }

            }

        }
    });
}

From source file:com.example.t_danbubbletea.MySelections.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_tea_selections, container, false);
    getActivity().setTitle("Tea Selections");
    // animation when enter home page
    rootView.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.image_click));
    //download the URL's asynchronously (put the info in the teaInfo object)
    try {/*from   ww  w  .  j  a va2s. c  o  m*/
        teaInfo = new GetTeaInfoTask().execute(new ApiConnector()).get();
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
    //make the gridview, set its adapter
    GridView gridView = (GridView) rootView.findViewById(R.id.grid_view_tea_selections);
    GridViewCustomAdapter gvAdapter = new GridViewCustomAdapter(getActivity(), teaInfo.imageURLs,
            teaInfo.teaNames);
    gridView.setAdapter(gvAdapter);
    //make the on click listeners
    gridView.setOnItemClickListener(new OnItemClickListener() {
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            Fragment newFragment;
            switch (position) {
            default:
                Bundle teaViewArgs = new Bundle();
                teaViewArgs.putString("teaName", teaInfo.teaNames.get(position));
                teaViewArgs.putString("teaImgUrl", teaInfo.imageURLs.get(position));
                teaViewArgs.putString("teaDesc", teaInfo.teaDescriptions.get(position));
                newFragment = new TeaViewFragment();
                newFragment.setArguments(teaViewArgs);
                break;

            }
            if (newFragment != null) {
                flipCard(newFragment);
            }
        }

        private void flipCard(Fragment newFragment) {
            mShowingBack = true;
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
                    .setCustomAnimations(R.anim.card_flip_right_in, R.anim.card_flip_right_out,
                            R.anim.card_flip_left_in, R.anim.card_flip_left_out)
                    .replace(R.id.frame_container, newFragment).addToBackStack(null).commit(); // create new fragment and allow user to go back to previous fragment          
        }
    });
    return rootView;
}

From source file:MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GridView gridView = new GridView(this);
    setContentView(gridView);/*from w  ww .j a  v  a 2s  .c  om*/
    String[] countries = new String[] { "China", "France", "Germany", "India", "Russia", "United Kingdom",
            "United States" };
    ListAdapter countryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, countries);
    gridView.setAdapter(countryAdapter);
    gridView.setNumColumns(2);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            String s = ((TextView) view).getText() + " " + position;
            Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
        }
    });
}

From source file:com.kubotaku.android.code4kyoto5374.fragments.SegregationFragment.java

private void setupViews() {
    View view = getView();/*from w w w  .  j  av  a 2  s  .  c o m*/

    GridView gridView = (GridView) view.findViewById(R.id.segregation_grid);
    segregationViewAdapter = new SegregationViewAdapter();
    gridView.setAdapter(segregationViewAdapter);

    inputTarget = (EditText) view.findViewById(R.id.segregation_edit_target);

    Button btnSearch = (Button) view.findViewById(R.id.segregation_btn_search);
    btnSearch.setOnClickListener(onClickSearchBtnListener);
}

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

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

    View view = inflater.inflate(R.layout.subject_fragment, container, false);

    viewHolder = new ViewHolder();
    viewHolder.subject_name = (TextView) view.findViewById(R.id.subject_name);
    viewHolder.subject_image = (NetworkImageView) view.findViewById(R.id.subject_image);
    viewHolder.subject_description_short = (TextView) view.findViewById(R.id.subject_description_short);
    viewHolder.subject_description_full = (TextView) view.findViewById(R.id.subject_description_full);
    viewHolder.subject_description_layout = (LinearLayout) view.findViewById(R.id.subject_description_layout);
    viewHolder.subject_description_layout_short = (RelativeLayout) view
            .findViewById(R.id.subject_description_layout_short);
    viewHolder.subject_description_layout_full = (RelativeLayout) view
            .findViewById(R.id.subject_description_layout_full);
    viewHolder.quotation_grid = (GridView) view.findViewById(R.id.quotations);

    viewHolder.subject_description_layout.setOnClickListener(new ViewToggleOnClickListener(
            viewHolder.subject_description_layout_short, viewHolder.subject_description_layout_full));

    GridView gridView = viewHolder.quotation_grid;
    gridView.setOnItemClickListener(new GridViewOnItemClickListener(gridView));
    gridView.setAdapter(new LoadingAdapter());

    Bundle bundle = new Bundle();
    bundle.putParcelable(LOADER_BUNDLE_URI, Subject.withAppendedId(getPick().getId()));
    bundle.putStringArray(LOADER_BUNDLE_PROJECTION, new String[] { Subject._ID, Subject.NAME,
            Subject.DESCRIPTION, Subject.IMAGE_ID, BookmarkSubject.BOOKMARK_ID });
    subjectLoaderId = initLoader(this, bundle);

    quotationLoaderId = initLoader(this, SubjectQuotation.withAppendedId(getPick().getId()),
            QuotationQuery.PROJECTION, null, null, null);

    return view;/*from  w  w w  .j  a  va  2  s  .  c  o  m*/
}

From source file:com.fabioarias.ui.Search.java

protected void search(String criteria) {
    ApiReader reader = new ApiReader(getActivity().getString(R.string.host), "searchByCriteria/" + criteria,
            "servers");
    try {/*  w ww .  ja va  2s .c o m*/
        categories = reader.getItems();
        Log.e("CATEGORIAS", "LEYENDO CATEGORIAS");
        if (categories != null) {
            Log.e("CATEGORIAS", "OK");
        } else {
            Log.e("CATEGORIAS", "NOK ");
        }
    } catch (Exception e) {
        e.printStackTrace();
        Log.e("CATEGORIAS", e.getMessage());
    }
    GridView grid = (GridView) v.findViewById(R.id.grid_search);
    grid.setVisibility(View.VISIBLE);
    grid.setAdapter(new GridAdapter());
}

From source file:imageUtils.ImageGridFragment.java

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

    final View v = inflater.inflate(R.layout.image_grid_fragment, container, false);
    final GridView mGridView = (GridView) v.findViewById(R.id.gridView);
    mGridView.setAdapter(mAdapter);
    mGridView.setOnItemClickListener(this);
    mGridView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override//from ww  w  . ja  va2 s .co  m
        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

            } 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;
                    mAdapter.setNumColumns(numColumns);
                    mAdapter.setItemHeight(columnWidth);
                    if (BuildConfig.DEBUG) {
                        Log.d(TAG, "onCreateView - numColumns set to " + numColumns);
                    }

                    mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }
            }
        }
    });

    return v;
}

From source file:com.example.android.popularmovies.app.MovieListingFragment.java

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

    movies = new ArrayList<MovieDetails>();

    movieAdapter = new MovieArrayAdapter(getActivity(), // The current context (this activity)
            R.layout.list_item_listing, // The name of the layout ID.
            R.id.list_item_listing_textview, // The ID of the textview to populate.
            movies);//from ww w.  j av a 2s .  co  m

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    Spinner spinner = (Spinner) rootView.findViewById(R.id.sort_spinner);
    // Create an ArrayAdapter using the string array and a default spinner layout
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(), R.array.sort_options,
            android.R.layout.simple_spinner_item);
    // Specify the layout to use when the list of choices appears
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    // Apply the adapter to the spinner
    spinner.setAdapter(adapter);

    FetchMovieListingTask movieListingTask = new FetchMovieListingTask();
    movieListingTask.execute(String.valueOf(spinner.getSelectedItemPosition()));

    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            FetchMovieListingTask movieListingTask = new FetchMovieListingTask();
            movieListingTask.execute(String.valueOf(i));
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });

    // Get a reference to the ListView, and attach this adapter to it.
    GridView gridView = (GridView) rootView.findViewById(R.id.grid_view_listing);
    gridView.setAdapter(movieAdapter);
    gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            // Log.i("MovieListingFragment", "Item clicked: " + view.toString() + " " + i + " " + l);
            getActivity().getSupportFragmentManager().beginTransaction().addToBackStack(null)
                    .replace(R.id.container, MovieDetailsFragment.newInstance(movies.get(i).id))
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE).commit();
        }
    });

    return rootView;
}