Android Open Source - TacoTool Create Activity






From Project

Back to project page TacoTool.

License

The source code is released under:

GNU General Public License

If you think the Android project TacoTool 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.example.tacotool;
/*  www  .ja  v a2 s. co  m*/
import java.util.ArrayList;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import database.DBManager;

@SuppressLint("NewApi")
public class CreateActivity extends Activity {
  private Taco taco;
  private DBManager dbm;
  private ListView listview;
  private ArrayAdapter adapter;
  @SuppressLint("NewApi")
  @Override
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_create);
    SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    taco = new Taco();
    dbm = new DBManager(getBaseContext());

    ActionBar ab = getActionBar();
    ab.setTitle(R.string.t_create);
    
    //till listvyn behvs en lyssnare med vilken man kan ta bort ingredienser frn nuvarande taco!
    listview = (ListView) findViewById(R.id.list_ingredients);

    adapter = new ArrayAdapter(getBaseContext(), android.R.layout.simple_list_item_1);

    listview.setAdapter(adapter);
    listview.setOnItemClickListener(new OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
        //        System.out.println("item: " + view.getI + " removed");
        String s = listview.getItemAtPosition(position).toString();
        adapter.remove(listview.getItemAtPosition(position));
        taco.removeIngredient(s);
        showToast(s);
      }
    });

  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.create, menu);
    return true;
  }

  public boolean onOptionsItemSelected(MenuItem item){

    switch (item.getItemId()) {

    case R.id.save_taco:
      if (taco.getName()!=null) {
        dbm.open();
        dbm.addRecipe(taco);        
        dbm.close();
      } else {
        showToast("Must set a name!");
      }        
      //MSTE SE TILL ATT TACON SPARAS MED NAMN!
      break;
    case R.id.open_taco:
      openSavedRecipe();
      break;
    }



    return false;
  }

  public void openSavedRecipe(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    dbm.open();
    ArrayList<String> list = dbm.getRecipes();
    dbm.close();
    final String[] arr = list.toArray(new String[list.size()]);
    builder.setTitle(R.string.title_recipes)
    .setItems(arr, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        String recipe = arr[which];
        dbm.open();
        taco = dbm.getRecipe(recipe);
        adapter.clear();

        for (String s : taco.getIngredients()) {
          adapter.add(s);
        }
        dbm.close();
      }
    });;

    AlertDialog dialog = builder.create();
    dialog.show();
  }


  public void showIngredientDialog(){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    dbm.open();
    ArrayList<String> list = dbm.getIngredients();
    dbm.close();
    final String[] arr = list.toArray(new String[list.size()]);
    builder.setTitle(R.string.t_ingredients)
    .setItems(arr, new DialogInterface.OnClickListener() {
      public void onClick(DialogInterface dialog, int which) {
        String ingredient = arr[which];

        if (!taco.addIngredient(arr[which])) {
          showToast("Recipe already contains " + ingredient);
        } else {
          adapter.add(ingredient);          
        }
      }
    });;

    AlertDialog dialog = builder.create();
    dialog.show();

  }

  public void setName(View view){
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View layout = inflater.inflate(R.layout.dialog_setname, null);
    builder.setView(layout)
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int id) {
        try {
          EditText mEdit = (EditText)layout.findViewById(R.id.taco_name);
          TextView txt = (TextView) findViewById(R.id.real_taco_name);
          txt.setText(mEdit.getText().toString());
          taco.setName(mEdit.getText().toString());
        } catch (NullPointerException e) {
          //no name was set!
        }
      }
    });
    AlertDialog dialog = builder.create();
    dialog.show();
  }

  public void addIngredient(View view){
    showIngredientDialog();
  }

  public void createIngredient(View view) {
    //db.addIngredient(ingredient);
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    LayoutInflater inflater = getLayoutInflater();
    final View layout = inflater.inflate(R.layout.dialog_ingredientname, null);
    builder.setView(layout)
    .setPositiveButton(R.string.save, new DialogInterface.OnClickListener() {
      @Override
      public void onClick(DialogInterface dialog, int id) {
        final EditText mEdit = (EditText) layout.findViewById(R.id.ingredient_name); 
        dbm.open();
        if (!dbm.addIngredient(mEdit.getText().toString())) {

          showToast("Ingredient " + mEdit.getText() + " already exists");
        }
        dbm.close();

      }
    })
    .setNeutralButton(R.string.discard, new DialogInterface.OnClickListener() {

      @Override
      public void onClick(DialogInterface dialog, int which) {
        // TODO Auto-generated method stub

      }
    });

    AlertDialog dialog = builder.create();
    dialog.show();
  }


  public void showToast(String message){
    Context context = getApplicationContext();
    int duration = Toast.LENGTH_SHORT;

    Toast toast = Toast.makeText(context, message, duration);
    toast.show();
  }

}




Java Source Code List

com.example.tacotool.CreateActivity.java
com.example.tacotool.MainActivity.java
com.example.tacotool.ManageActivity.java
com.example.tacotool.SettingsActivity.java
com.example.tacotool.Taco.java
database.DBManager.java
database.DBSQLiteHelper.java