Android Open Source - ActiveAndroidDemo Add Product Dialog Fragment






From Project

Back to project page ActiveAndroidDemo.

License

The source code is released under:

This is free and unencumbered software released into the public domain. Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a co...

If you think the Android project ActiveAndroidDemo 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 com.gannon.activeandroiddemo.fragments;
//w  w  w .jav a2  s .  com
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import com.gannon.activeandroiddemo.R;
import com.gannon.activeandroiddemo.activities.Main;
import com.gannon.activeandroiddemo.models.Category;
import com.gannon.activeandroiddemo.models.Product;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Add Product Dialog Fragment
 */
public class AddProductDialogFragment extends DialogFragment
{
    @InjectView(R.id.title_edit)
    protected EditText titleEdit;
    @InjectView(R.id.description_edit)
    protected EditText descriptionEdit;
    @InjectView(R.id.price_edit)
    protected EditText priceEdit;
    @InjectView(R.id.category_spinner)
    protected Spinner categorySpinner;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_add_product, container, false);
        ButterKnife.inject(this, view);

        getDialog().setTitle("Add Product");

        categorySpinner.setAdapter(
                new ArrayAdapter<Category>(
                        getActivity(), android.R.layout.simple_spinner_item, Category.getAll()
                )
        );

        return view;
    }

    @Override
    public void onDismiss(DialogInterface dialog)
    {
        super.onDismiss(dialog);

        ((Main)getActivity()).reloadCurrent();
    }

    @OnClick(R.id.create_button)
    protected void createdButtonClick()
    {
        saveProduct();

        getDialog().dismiss();
    }

    /**
     * Persist new product to database
     */
    private void saveProduct()
    {
        Product product = new Product((Category)categorySpinner.getSelectedItem());

        product.title       = titleEdit.getText().toString();
        product.description = descriptionEdit.getText().toString();
        product.price       = Double.parseDouble(priceEdit.getText().toString());

        product.save();

        Toast.makeText(
                getActivity(), "Product has been added to database", Toast.LENGTH_SHORT
        ).show();
    }
}




Java Source Code List

com.gannon.activeandroiddemo.ActiveAndroidDemo.java
com.gannon.activeandroiddemo.ApplicationTest.java
com.gannon.activeandroiddemo.activities.Main.java
com.gannon.activeandroiddemo.adapters.ProductArrayAdapter.java
com.gannon.activeandroiddemo.fragments.AddProductDialogFragment.java
com.gannon.activeandroiddemo.helpers.ModelHelper.java
com.gannon.activeandroiddemo.models.Category.java
com.gannon.activeandroiddemo.models.Product.java