Java tutorial
/** * Copyright 2012 C. A. Fitzgerald * * 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.github.riotopsys.shoppinglist.fragment; import java.net.MalformedURLException; import java.sql.SQLException; import java.util.UUID; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.ListView; import android.widget.ShareActionProvider; import com.github.riotopsys.shoppinglist.AppKeys; import com.github.riotopsys.shoppinglist.Configurations; import com.github.riotopsys.shoppinglist.IConfigurations; import com.github.riotopsys.shoppinglist.R; import com.github.riotopsys.shoppinglist.activity.ListItemEdit; import com.github.riotopsys.shoppinglist.adapter.ShoppingListAdapter; import com.github.riotopsys.shoppinglist.model.DatabaseHelper; import com.github.riotopsys.shoppinglist.model.ShoppingList; import com.j256.ormlite.android.apptools.OpenHelperManager; public class ShoppingListFragment extends Fragment implements OnItemLongClickListener { private ShoppingList mList; private ShoppingListAdapter mListAdapter; private BroadcastReceiver mItemUpdateReciever = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { mListAdapter.notifyDataSetChanged(context); } }; private IntentFilter mFilter = new IntentFilter(AppKeys.ITEM_UPDATE_ACTION); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ListView view = (ListView) inflater.inflate(R.layout.shopping_list_fragment, null); UUID uuid = (UUID) getArguments().getSerializable(AppKeys.LIST_KEY); DatabaseHelper databaseHelper = OpenHelperManager.getHelper(getActivity(), DatabaseHelper.class); try { mList = databaseHelper.getShoppingList(uuid); } catch (SQLException e) { throw new RuntimeException(e); } finally { OpenHelperManager.releaseHelper(); } mListAdapter = new ShoppingListAdapter(getActivity(), mList); view.setAdapter(mListAdapter); setHasOptionsMenu(true); view.setOnItemLongClickListener(this); return view; } @Override public void onResume() { getActivity().registerReceiver(mItemUpdateReciever, mFilter); mListAdapter.notifyDataSetChanged(getActivity()); super.onResume(); } @Override public void onPause() { getActivity().unregisterReceiver(mItemUpdateReciever); super.onPause(); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.shopping_list_fragment, menu); MenuItem item = menu.findItem(R.id.menu_share); ShareActionProvider shareActionProvider = (ShareActionProvider) item.getActionProvider(); IConfigurations confs = new Configurations(); Intent shareIntent = new Intent(Intent.ACTION_SEND); try { shareIntent.putExtra(Intent.EXTRA_TEXT, String.format("%s%s", confs.shareUrlBase(), mList.getGuid().toString())); } catch (MalformedURLException e) { throw new RuntimeException(e); } shareIntent.setType("text/plain"); shareActionProvider.setShareIntent(shareIntent); super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { if (item.getItemId() == R.id.menu_add_item) { Intent i = new Intent(getActivity(), ListItemEdit.class); i.putExtra(AppKeys.LIST_KEY, mList.getGuid()); startActivity(i); } return super.onOptionsItemSelected(item); } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(getActivity(), ListItemEdit.class); i.putExtra(AppKeys.LIST_ITEM_KEY, mListAdapter.getListItem(position).getGuid()); i.putExtra(AppKeys.LIST_KEY, mList.getGuid()); startActivity(i); return false; } }