Back to project page trivial-lists.
The source code is released under:
MIT License
If you think the Android project trivial-lists listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package org.hbabcock.triviallists; //w w w.j a va 2 s .c o m import java.util.ArrayList; import java.util.UUID; import android.annotation.TargetApi; import android.app.Activity; import android.content.Intent; import android.os.Build; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.util.Log; import android.view.ActionMode; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; 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.AbsListView.MultiChoiceModeListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.Spinner; import android.widget.TextView; public class ItemFragmentEdit extends ItemFragment { private static final String TAG = "ItemFragmentEdit"; private class AmountSpinnerListener implements OnItemSelectedListener { private Item mItem; public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { Log.i(TAG, "amount " + mItem.getId()); mItem.setCount(pos); } @Override public void onNothingSelected(AdapterView<?> arg0) { } public void setItem(Item item){ mItem = item; } } public class ItemAdapter extends ArrayAdapter<Item>{ public ItemAdapter(ArrayList<Item> items){ super(getActivity(), 0, items); } @Override public View getView(int position, View convertView, ViewGroup parent){ if (convertView == null){ convertView = getActivity().getLayoutInflater().inflate(R.layout.item_fragment_list_item, null); } Item i = getItem(position); Log.i(TAG, "name: " + i.getName()); // Configure text. ((TextView)convertView.findViewById(R.id.item_list_item)).setText(i.getName()); // Configure spinner. Spinner sp = (Spinner)convertView.findViewById(R.id.amount_spinner); sp.setSelection(i.getCount()); AmountSpinnerListener al = new AmountSpinnerListener(); al.setItem(i); sp.setOnItemSelectedListener(al); return convertView; } } private void editItem (Item i){ Log.i(TAG, "startEditDialog"); FragmentManager fm = getActivity().getSupportFragmentManager(); GenericEditorDialogFragment dialog = GenericEditorDialogFragment.newInstance(i.getId(), i.getName()); dialog.setTargetFragment(ItemFragmentEdit.this, GenericEditorDialogFragment.REQUEST_EDIT); dialog.show(fm, TAG); } public static ItemFragmentEdit newInstance(UUID categoryId){ ItemFragmentEdit fragment = new ItemFragmentEdit(); fragment.setArguments(setBundle(categoryId)); return fragment; } @Override public void onActivityResult(int requestCode, int resultCode, Intent data){ Log.d(TAG, "onActivityResult"); if (resultCode != Activity.RESULT_OK) return; if (requestCode == GenericEditorDialogFragment.REQUEST_NEW){ Item i = new Item(data.getStringExtra(GenericEditorDialogFragment.EXTRA_GENERIC_NAME), getCategory()); CategoryItemManager.get(getActivity()).addItem(i); } else{ Item i = CategoryItemManager.get(getActivity()).getItem(UUID.fromString(data.getStringExtra(GenericEditorDialogFragment.EXTRA_GENERIC_ID))); i.setName(data.getStringExtra(GenericEditorDialogFragment.EXTRA_GENERIC_NAME)); } ((ItemAdapter)getListAdapter()).notifyDataSetChanged(); } @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); Log.i(TAG, "onCreate"); ItemAdapter adapter = new ItemAdapter(CategoryItemManager.get(getActivity()).getItems(getCategory())); setListAdapter(adapter); } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){ getActivity().getMenuInflater().inflate(R.menu.item_fragment_context_menu, menu); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater){ super.onCreateOptionsMenu(menu, inflater); Log.i(TAG, "onCreateOptionsMenu"); inflater.inflate(R.menu.item_fragment_menu, menu); } @TargetApi(11) @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){ Log.i(TAG, "onCreateView"); View v = super.onCreateView(inflater, parent, savedInstanceState); ListView lv = (ListView)v.findViewById(android.R.id.list); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB){ registerForContextMenu(lv); } else { lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); lv.setMultiChoiceModeListener(new MultiChoiceModeListener(){ public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked){ } public boolean onCreateActionMode(ActionMode mode, Menu menu){ MenuInflater inflater = mode.getMenuInflater(); inflater.inflate(R.menu.item_fragment_context_menu, menu); return true; } public boolean onPrepareActionMode(ActionMode mode, Menu menu){ return false; } public boolean onActionItemClicked(ActionMode mode, MenuItem item){ ItemAdapter adapter = (ItemAdapter)getListAdapter(); CategoryItemManager categoryItemManager = CategoryItemManager.get(getActivity()); switch (item.getItemId()){ case R.id.delete_item: for (int i = adapter.getCount() - 1; i >= 0; i--){ if (getListView().isItemChecked(i)){ Log.i(TAG, "deleted " + adapter.getItem(i).getName()); categoryItemManager.deleteItem(adapter.getItem(i)); } } mode.finish(); adapter.notifyDataSetChanged(); Log.i(TAG, "count " + adapter.getCount()); return true; case R.id.edit_item: for (int i = 0; i < adapter.getCount(); i++){ if (getListView().isItemChecked(i)){ editItem(adapter.getItem(i)); } } mode.finish(); return true; default: return false; } } public void onDestroyActionMode(ActionMode mode){ } }); } return v; } @Override public boolean onOptionsItemSelected(MenuItem item){ switch (item.getItemId()){ case R.id.menu_item_new_item: Log.i(TAG, "startAddDialog"); FragmentManager fm = getActivity().getSupportFragmentManager(); Item i = new Item("", getCategory()); GenericEditorDialogFragment dialog = GenericEditorDialogFragment.newInstance(i.getId(), i.getName()); dialog.setTargetFragment(ItemFragmentEdit.this, GenericEditorDialogFragment.REQUEST_NEW); dialog.show(fm, TAG); return true; default: return super.onOptionsItemSelected(item); } } @Override public void setUserVisibleHint(boolean isVisibleToUser) { super.setUserVisibleHint(isVisibleToUser); if (isVisibleToUser) { ItemAdapter adapter = (ItemAdapter)getListAdapter(); adapter.notifyDataSetChanged(); } } }