I am having a lot of trouble with this problem. What I am trying to do is take value from a recipe page (activity 1) and pass them into a function that updates a contents table in the SQLite database. There is also a second activity that displays a list of all the rows in the contents table. What I want to do is click a button in activity 1 to deduct the ingredients and then move to activity 2 manually to see that they have been successfully deducted.
I have what I think is the working code but it doesnt seem to be working, I am getting no errors and cant see where I have gone wrong. Any help will be great!
I have even added a button in the second activity that refreshes the list when clicked. I can figure out if its the execution of the deductIngredients
function or the SQL that is causing the problem
SQL statement to update the rows
//deducting ingredients after cooking
public boolean deductIngredient(String ingredient, int measurement) {
db.execSQL( "update kitchen set kitchen.measurement = kitchen.measurement - ? where kitchen.ingredient_name = ?", new Object[] { Integer.valueOf( measurement ), ingredient } );
return true;
}
Activity 1 - display the recipe and ingredients used and button to deduct ingredients
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class SingleRecipeDisaply extends Activity {
DBMain adapter = new DBMain(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_single_recipe_disaply);
TextView txtProduct = (TextView) findViewById(R.id.recipeTitle);
Intent i = getIntent();
// getting attached intent data
String string = i.getStringExtra("recipe");
// displaying selected product name
txtProduct.setText(string);
adapter.open();
Cursor code = adapter.getCode(string);
code.moveToFirst();
int recipeCode = code.getInt(0);
final Cursor ingredients = adapter.getRecipesIngredients(recipeCode);
Cursor directions = adapter.getRecipesDirections(recipeCode);
//String[] columns = new String[] {db.KEY_NAME, db.KEY_CODE, db.KEY_ROWID};
String[] ingredient_columns = new String[] {adapter.KEY_INGREDIENT_NAME, adapter.KEY_INGREDIENT_MEASUREMENT, adapter.KEY_INGREDIENT_UNIT};
int[] to = new int[] {R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit};
SimpleCursorAdapter ingredientAdpater = new SimpleCursorAdapter(this,R.layout.row5, ingredients, ingredient_columns, to, 0);
ListView ingredientsRecquired = (ListView) findViewById(R.id.ingredientsRequired);
ingredientsRecquired.setAdapter(ingredientAdpater);
String[] direction_columns = new String[] {adapter.KEY_DIRECTION_DESCRIPTION};
int [] to2 = new int[] {R.id.directionDescription};
SimpleCursorAdapter directionAdapter = new SimpleCursorAdapter(this,R.layout.row6, directions, direction_columns, to2, 0);
ListView directionRequired = (ListView) findViewById(R.id.directionsRequired);
directionRequired.setAdapter(directionAdapter);
Button cookButton = (Button) findViewById(R.id.cookButton);
cookButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
while (ingredients.moveToNext())
{
String ingredient = ingredients.getString(ingredients.getColumnIndex("ingredient_name"));
int measurement = ingredients.getInt(ingredients.getColumnIndex("measurement"));
adapter.deductIngredient(ingredient, measurement);
}
ingredients.close();
}
});
}
}
Activity 2 - Display the contents table
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class showContents extends AppCompatActivity {
//DBAdapterIngredients db = new DBAdapterIngredients(this);
DBMain adapter = new DBMain(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_contents);
adapter.open();
//adapter.insertSamples();
//adapter.getAllIngredients();
/*adapter.insertContent("Melons", 50, "grams");
adapter.insertContent("Beef", 100, "grams");
adapter.insertContent("Red Pepper", 30, "grams");
adapter.insertContent("Onion", 2, "cloves");
adapter.insertContent("Five Spice", 100, "grams");
adapter.insertContent("Rice", 5, "cups");
adapter.insertContent("Green Beans", 200, "grams");
adapter.insertContent("Oil", 300, "ml");
adapter.insertContent("Chicken", 300, "grams");
adapter.insertContent("Bread", 20, "slices");
adapter.insertContent("Butter", 100, "grams");
adapter.insertContent("Mayo", 30, "grams");
adapter.insertContent("Salt", 10, "grams");
adapter.insertContent("Pepper", 10, "grams");
adapter.insertContent("Milk", 400, "ml");
adapter.insertContent("Cabbage", 200, "grams");
adapter.insertContent("Yogurt", 20, "grams");
adapter.insertContent("Broccoli", 200, "grams");
adapter.insertContent("Tomato", 300, "grams");
adapter.insertContent("Potato", 500, "grams");
adapter.insertContent("Lamb", 400, "grams");*/
Cursor cursor = adapter.getAllContents();
//String[] columns = new String[] {db.KEY_NAME, db.KEY_MEASUREMENT, db.KEY_ROWID};
String[] columns = new String[] {adapter.KEY_CONTENTS_NAME, adapter.KEY_CONTENTS_MEASUREMENT, adapter.KEY_CONTENTS_UNIT};
int[] to = new int[] {R.id.ingredientName, R.id.ingredientMeasurement, R.id.ingredientUnit};
final SimpleCursorAdapter myCursorAdapter = new SimpleCursorAdapter(this,R.layout.row2, cursor, columns, to, 0);
ListView ingredientList = (ListView) findViewById(R.id.ingredientList);
ingredientList.setAdapter(myCursorAdapter);
Button refresh = (Button) findViewById(R.id.refresh);
refresh.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
myCursorAdapter.notifyDataSetChanged();
}
});
}
}
Aucun commentaire:
Enregistrer un commentaire