Android Open Source - ShoppingList Shopping Lists Fragment






From Project

Back to project page ShoppingList.

License

The source code is released under:

GNU General Public License

If you think the Android project ShoppingList listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package net.sledzdev.shoppinglist;
// w w  w.j  a  va  2 s. com
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;

import net.sledzdev.shoppinglist.adapter.DataModel;
import net.sledzdev.shoppinglist.adapter.ShoppingListsAdapter;
import net.sledzdev.shoppinglist.event.EventBusFactory;
import net.sledzdev.shoppinglist.event.ListSelectedEvent;
import net.sledzdev.shoppinglist.event.NewListRequestEvent;
import net.sledzdev.shoppinglist.manager.ContentManager;
import net.sledzdev.shoppinglist.manager.OnUiThreadFutureCallback;
import net.sledzdev.shoppinglist.model.ShoppingItem;
import net.sledzdev.shoppinglist.model.ShoppingItemBuilder;
import net.sledzdev.shoppinglist.model.ShoppingList;


public class ShoppingListsFragment extends ListFragment {
    private static final String STATE_ACTIVATED_POSITION = "activated_position";

    private int mActivatedPosition = ListView.INVALID_POSITION;
    private ContentManager contentManager;
    private Button newListBtn;

    public ShoppingListsFragment() {
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.shopping_lists, container);
        newListBtn = (Button) rootView.findViewById(R.id.new_list);
        setEvents();
        return rootView;
    }

    private void setEvents() {
        newListBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBusFactory.getEventBus().post(new NewListRequestEvent());
            }
        });
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        // Restore the previously serialized activated item position.
        if (savedInstanceState != null
                && savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
            setActivatedPosition(savedInstanceState.getInt(STATE_ACTIVATED_POSITION));
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        initContentManagerAndLoadLists();
    }

    private void initContentManagerAndLoadLists() {
        contentManager = ContentManager.createContentManager(this.getActivity());

        ListenableFuture<DataModel<ShoppingList>> future = contentManager.loadShoppingListsModel();
        Futures.addCallback(future, new OnUiThreadFutureCallback<DataModel<ShoppingList>>(getActivity()) {
            @Override
            protected void onSuccessUiThread(DataModel<ShoppingList> shoppingLists) {
                ShoppingListsAdapter shoppingListsAdapter = new ShoppingListsAdapter(getActivity(), shoppingLists);
                setListAdapter(shoppingListsAdapter);
            }

            @Override
            public void onFailure(Throwable throwable) {
                Log.d("shopping app error", "couldn't load shopping lists, error: " + throwable);
            }
        });
    }

    @Override
    public void onListItemClick(ListView listView, View view, int position, long id) {
        super.onListItemClick(listView, view, position, id);
        EventBusFactory.getEventBus().post(new ListSelectedEvent(id));
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        if (mActivatedPosition != ListView.INVALID_POSITION) {
            // Serialize and persist the activated item position.
            outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
        }
    }

    public void setActivateOnItemClick(boolean activateOnItemClick) {
        // When setting CHOICE_MODE_SINGLE, ListView will automatically
        // give items the 'activated' state when touched.
        getListView().setChoiceMode(activateOnItemClick
                ? ListView.CHOICE_MODE_SINGLE
                : ListView.CHOICE_MODE_NONE);
    }

    private void setActivatedPosition(int position) {
        if (position == ListView.INVALID_POSITION) {
            getListView().setItemChecked(mActivatedPosition, false);
        } else {
            getListView().setItemChecked(position, true);
        }

        mActivatedPosition = position;
    }

}




Java Source Code List

net.sledzdev.shoppinglist.DetailFragmentHandlersRegister.java
net.sledzdev.shoppinglist.ItemDialogActivity.java
net.sledzdev.shoppinglist.ShoppingListDetailActivity.java
net.sledzdev.shoppinglist.ShoppingListDetailFragment.java
net.sledzdev.shoppinglist.ShoppingListsActivity.java
net.sledzdev.shoppinglist.ShoppingListsFragment.java
net.sledzdev.shoppinglist.adapter.DataModelAdapter.java
net.sledzdev.shoppinglist.adapter.DataModel.java
net.sledzdev.shoppinglist.adapter.ElementWithId.java
net.sledzdev.shoppinglist.adapter.ItemAdapterEventController.java
net.sledzdev.shoppinglist.adapter.ItemAdapter.java
net.sledzdev.shoppinglist.adapter.ListMapDataModelTest.java
net.sledzdev.shoppinglist.adapter.ListMapDataModel.java
net.sledzdev.shoppinglist.adapter.ShoppingListAdapterEventsHandler.java
net.sledzdev.shoppinglist.adapter.ShoppingListsAdapter.java
net.sledzdev.shoppinglist.content.DatabaseHelper.java
net.sledzdev.shoppinglist.content.DatabaseUriContentProvider.java
net.sledzdev.shoppinglist.content.ItemRowProvider.java
net.sledzdev.shoppinglist.content.ItemsProvider.java
net.sledzdev.shoppinglist.content.ItemsTable.java
net.sledzdev.shoppinglist.content.ListRowProvider.java
net.sledzdev.shoppinglist.content.ListsProvider.java
net.sledzdev.shoppinglist.content.ListsTable.java
net.sledzdev.shoppinglist.content.RowUriProvider.java
net.sledzdev.shoppinglist.content.ShoppingListProviderTest.java
net.sledzdev.shoppinglist.content.ShoppingListProvider.java
net.sledzdev.shoppinglist.content.ShoppingProviderContract.java
net.sledzdev.shoppinglist.content.TableUriProvider.java
net.sledzdev.shoppinglist.content.UriContentProvider.java
net.sledzdev.shoppinglist.event.ClearListEvent.java
net.sledzdev.shoppinglist.event.EventBusFactory.java
net.sledzdev.shoppinglist.event.ItemChangedEvent.java
net.sledzdev.shoppinglist.event.ItemCheckedChangedEvent.java
net.sledzdev.shoppinglist.event.ItemDeleteEvent.java
net.sledzdev.shoppinglist.event.ItemDialogCloseEvent.java
net.sledzdev.shoppinglist.event.ItemDialogOpenRequestEvent.java
net.sledzdev.shoppinglist.event.ListDeleteEvent.java
net.sledzdev.shoppinglist.event.ListSelectedEvent.java
net.sledzdev.shoppinglist.event.ListTitleChangedEvent.java
net.sledzdev.shoppinglist.event.NewItemEvent.java
net.sledzdev.shoppinglist.event.NewListEvent.java
net.sledzdev.shoppinglist.event.NewListRequestEvent.java
net.sledzdev.shoppinglist.event.TextWatcherAdapter.java
net.sledzdev.shoppinglist.event.UpdateListAdapterEvent.java
net.sledzdev.shoppinglist.handlers.ClearListEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemChangedEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemCheckedChangeEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemDeleteEventHandler.java
net.sledzdev.shoppinglist.handlers.ItemDialogOpenRequestEventHandler.java
net.sledzdev.shoppinglist.handlers.ListDeleteEventHandler.java
net.sledzdev.shoppinglist.handlers.ListSelectedEventHandler.java
net.sledzdev.shoppinglist.handlers.ListTitleChangedEventHandler.java
net.sledzdev.shoppinglist.handlers.NewItemEventHandler.java
net.sledzdev.shoppinglist.handlers.NewListEventHandler.java
net.sledzdev.shoppinglist.manager.ContentManagerTests.java
net.sledzdev.shoppinglist.manager.ContentManager.java
net.sledzdev.shoppinglist.manager.ContentTransformer.java
net.sledzdev.shoppinglist.manager.ItemContentTransformer.java
net.sledzdev.shoppinglist.manager.ListsContentTransformer.java
net.sledzdev.shoppinglist.manager.MockContentManager.java
net.sledzdev.shoppinglist.manager.OnUiThreadFutureCallback.java
net.sledzdev.shoppinglist.model.ShoppingItemBuilderTest.java
net.sledzdev.shoppinglist.model.ShoppingItemBuilder.java
net.sledzdev.shoppinglist.model.ShoppingItem.java
net.sledzdev.shoppinglist.model.ShoppingListFactoryTest.java
net.sledzdev.shoppinglist.model.ShoppingListFactory.java
net.sledzdev.shoppinglist.model.ShoppingList.java