Java tutorial
/* * Copyright (C) 2012 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.kowd.pcapp.client.android.fragment; import android.annotation.SuppressLint; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.LoaderManager; import android.support.v4.content.CursorLoader; import android.support.v4.content.Loader; import android.util.DisplayMetrics; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.GridView; import com.kowd.pcapp.client.android.R; import com.kowd.pcapp.client.android.activity.ProductItemActivity; import com.kowd.pcapp.client.android.adapter.ProductsGridViewAdapter; import com.kowd.pcapp.client.android.constants.PCConstants; import com.kowd.pcapp.client.android.constants.PCContentProviderConst; public class ProductItemsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>, AdapterView.OnItemClickListener { private ProductsGridViewAdapter mAdapter; private GridView mGridView; private int mColumnWidth; private int mCategoryId; public ProductItemsFragment(final int categoryId) { mCategoryId = categoryId; } @SuppressLint("InflateParams") @Override public View onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle bundle) { super.onCreateView(inflater, container, bundle); final View localView = inflater.inflate(R.layout.pc_products_list, container, false); if (null != bundle) { mCategoryId = bundle.getInt(PCConstants.CATEGORY_ID_KEY, -1); } initLoader(mCategoryId); mGridView = (GridView) localView.findViewById(R.id.pc_products_list); final DisplayMetrics localDisplayMetrics = new DisplayMetrics(); getActivity().getWindowManager().getDefaultDisplay().getMetrics(localDisplayMetrics); final int pixelSize = getResources().getDimensionPixelSize(R.dimen.pc_thumb_size); final int widthScale = localDisplayMetrics.widthPixels / pixelSize; mColumnWidth = (localDisplayMetrics.widthPixels / widthScale); mAdapter = new ProductsGridViewAdapter(container.getContext(), mColumnWidth); mGridView.setColumnWidth(mColumnWidth); mGridView.setNumColumns(2); mGridView.setAdapter(mAdapter); mGridView.setOnItemClickListener(this); mGridView.setEmptyView(localView.findViewById(R.id.pc_products_progress_root)); return localView; } public void initLoader(final int categoryId) { mCategoryId = categoryId; final Bundle categoryParam = new Bundle(); categoryParam.putInt(PCConstants.CATEGORY_ID_KEY, categoryId); getLoaderManager().restartLoader(PCContentProviderConst.PRODUCTS_LOADER, categoryParam, this); } @Override public void onDestroyView() { mGridView = null; super.onDestroyView(); } @Override public void onActivityCreated(final Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); } @Override public void onSaveInstanceState(final Bundle outState) { super.onSaveInstanceState(outState); outState.putInt(PCConstants.CATEGORY_ID_KEY, mCategoryId); } @Override public void onItemClick(final AdapterView<?> adapterView, final View view, final int viewId, final long rowId) { final Cursor cursor = (Cursor) mAdapter.getItem(viewId); final Bundle bundle = new Bundle(); final int id = cursor.getInt(PCContentProviderConst.PRODUCTS_ID_CURSOR_INDEX); final String name = cursor.getString(PCContentProviderConst.PRODUCTS_NAME_CURSOR_INDEX); final String desc = cursor.getString(PCContentProviderConst.PRODUCTS_DESCRIPTION_CURSOR_INDEX); final String imgLoc = cursor.getString(PCContentProviderConst.PRODUCTS_IMAGE_LOC_CURSOR_INDEX); bundle.putInt(PCConstants.PRODUCTS_ID_KEY, id); bundle.putString(PCConstants.PRODUCTS_NAME_KEY, name); bundle.putString(PCConstants.PRODUCTS_DESC_KEY, desc); bundle.putString(PCConstants.PRODUCTS_IMAGE_LOC_KEY, imgLoc); final Intent viewProduct = ProductItemActivity.newInstance(getActivity(), bundle); startActivity(viewProduct); } @Override public Loader<Cursor> onCreateLoader(final int localView, final Bundle bundle) { switch (localView) { case PCContentProviderConst.PRODUCTS_LOADER: final int categoryId = bundle.getInt(PCConstants.CATEGORY_ID_KEY, -1); return new CursorLoader(getActivity(), // Context PCContentProviderConst.PRODUCTS_TABLE_CONTENTURI, // Table to query PCContentProviderConst.PRODUCTS_PROJECTION, // Projection to return PCContentProviderConst.CATEGORY_ID + " = ?", // No selection clause new String[] { String.valueOf(categoryId) }, // No selection arguments PCContentProviderConst.ROW_ID + " ASC" // Default sort order ); default: // An invalid id was passed in return null; } } @Override public void onLoadFinished(final Loader<Cursor> loader, final Cursor cursor) { mAdapter.changeCursor(cursor); } @Override public void onLoaderReset(final Loader<Cursor> loader) { mAdapter.changeCursor(null); } }