I am working on integrating a database to an Android application for a course.
Currently, I have all the database code done, but getting all of the EditText fields to enter data into the database has me stuck.
Assignment objectives here, so we can be on the same page:
My code and some screenshots:
Data Entry Java Code:
package com.healthylife; import android.app.ListActivity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.provider.BaseColumns; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.CursorAdapter; import android.widget.EditText; import android.widget.ListView; import android.widget.Spinner; public class Data extends AppCompatActivity { private Button add; private Button delete; private EditText name; private EditText ingredients; private EditText instructions; private Spinner category; public class DataHandlingActivity extends ListActivity{ private SQLiteDatabase Recipes; String fields[] = { "Recipe Name", "Category","Ingredients", "Instructions", BaseColumns._ID }; private CursorAdapter dataSource; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_data); databasehandler helper = new databasehandler(this); Recipes = helper.getReadableDatabase(); Cursor data = Recipes.query("Recipes", fields, null, null, null, null, null); dataSource = new CursorAdapter(this, R.layout.row, data, fields, new int[] { R.id.recipename, R.id.category, R.id.ingredients, R.id.instructions }); ListView view = getListView(); view.setHeaderDividersEnabled(true); view.addHeaderView(getLayoutInflater().inflate(R.layout.row, null)); setListAdapter(dataSource); }}}
____________
Database Handler Code:
package com.healthylife; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.provider.BaseColumns; import android.util.Log; public class databasehandler extends SQLiteOpenHelper { public databasehandler(Context context){ super(context, "DBhandler", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("CREATE TABLE IF NOT EXISTS Recipes(" + BaseColumns._ID + "INTEGER PRIMARY KEY AUTOINCREMENT, recipeName STRING, category STRING, ingredients STRING, instructions STRING)"); db.execSQL("INSERT INTO Recipes (recipeName, category, ingredients, instructions) VALUES('')"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
This screenshot has the text entry fields, and one spinner entry field to enter data to be submitted to the database. I have been using my resources and understand how most everything else works now, just getting the buttons and text entry fields to work has me puzzled now.
Any help would be appreciated.
MainActivity.java
import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.RadioGroup; import android.widget.Spinner; import android.content.ContentValues; import android.content.Intent; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Toast;
public class MainActivity extends AppCompatActivity { String selectcat; Button add,del; ImageView img; RadioGroup rg; //RadioButton rbtn; EditText ing,ins,recipe; Spinner cat; DataBaseHelper helper=null; Context context; SQLiteDatabase db=null; Cursor cursor; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setTitle("Helthy Life"); String TABLE_CREATE = "CREATE TABLE IF NOT EXISTS Recipes(" + "recipe VARCHAR(100),cat VARCHAR(100),ing VARCHAR(100),ins VARCHAR(100))"; helper = new DataBaseHelper(this); db = helper.getWritableDatabase(); db.execSQL(TABLE_CREATE); recipe= (EditText) findViewById(R.id.etname); ing= (EditText) findViewById(R.id.eting); ins= (EditText) findViewById(R.id.etins); String items[] = new String[]{"bread", "xxx"}; cat = (Spinner) findViewById(R.id.drpcat); add = (Button) findViewById(R.id.btnadd); del=(Button)findViewById(R.id.btndel); ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items); cat.setAdapter(adapter1); cat.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { selectcat = (String) adapterView.getItemAtPosition(i); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); del.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { db.execSQL("delete from Recipes"); Toast.makeText(MainActivity.this,"Reocrd Deleted Successfully",Toast.LENGTH_LONG).show(); } }); add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { submitForm(); Intent i=new Intent(MainActivity.this,Add_success.class); startActivity(i); } }); } private void submitForm() { //first validate the form then move ahead //if this becomes true that means validation is successfull final String recipe1=recipe.getText().toString(); final String ins1=ins.getText().toString(); final String ing1=ing.getText().toString(); ContentValues cv=new ContentValues(); cv.put("Recipe",recipe1); cv.put("category",selectcat); cv.put("ingrediants",ing1); cv.put("instruction",ins1); long result=db.insert("Recipes",null,cv); //process the data further } }
Add_success.java
import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.widget.Button; public class Add_success extends AppCompatActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_success); } }
add_success.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/txtsuccess" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" android:layout_marginTop="71dp" android:gravity="center_horizontal" android:text="Added Successfully......" android:textSize="20dp" /> </RelativeLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.ruchi.myapp.MainActivity"> <TextView android:id="@+id/txtname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignEnd="@+id/etname" android:layout_alignParentTop="true" android:layout_alignRight="@+id/etname" android:layout_marginEnd="11dp" android:layout_marginRight="11dp" android:layout_marginTop="10dp" android:text="Please give the recipe a name below" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="164dp" /> <EditText android:id="@+id/etname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/txtcat" android:layout_centerHorizontal="true" android:layout_marginBottom="25dp" android:ems="10" android:inputType="textPersonName" tools:layout_editor_absoluteX="151dp" tools:layout_editor_absoluteY="138dp" /> <TextView android:id="@+id/txtcat" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/etname" android:layout_alignStart="@+id/etname" android:layout_below="@+id/txtname" android:layout_marginLeft="69dp" android:layout_marginStart="69dp" android:layout_marginTop="86dp" android:text="Category" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="164dp" /> <Spinner android:id="@+id/drpcat" android:layout_width="210dp" android:layout_height="40dp" android:layout_below="@+id/txtcat" android:layout_marginLeft="70dp" android:layout_marginStart="70dp" android:layout_marginTop="12dp" android:background="@android:drawable/btn_dropdown" tools:layout_editor_absoluteX="152dp" tools:layout_editor_absoluteY="352dp"></Spinner> <TextView android:id="@+id/txting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="90dp" android:layout_below="@+id/drpcat" android:text="ingredient" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="164dp" android:layout_marginLeft="90dp" /> <EditText android:id="@+id/eting" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txting" android:layout_centerHorizontal="true" android:layout_marginBottom="25dp" android:ems="10" android:inputType="textPersonName" tools:layout_editor_absoluteX="151dp" tools:layout_editor_absoluteY="138dp" /> <TextView android:id="@+id/txtins" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="90dp" android:layout_below="@+id/eting" android:text="instruction" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="164dp" android:layout_marginLeft="90dp" /> <EditText android:id="@+id/etins" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txtins" android:layout_centerHorizontal="true" android:layout_marginBottom="25dp" android:ems="10" android:inputType="textPersonName" tools:layout_editor_absoluteX="151dp" tools:layout_editor_absoluteY="138dp" /> <Button android:id="@+id/btnadd" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/etins" android:layout_centerHorizontal="true" android:layout_marginLeft="20dp" android:layout_marginTop="8dp" android:text="Add" tools:layout_editor_absoluteX="148dp" tools:layout_editor_absoluteY="429dp" /> <Button android:id="@+id/btndel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/btnadd" android:layout_centerHorizontal="true" android:layout_marginLeft="20dp" android:layout_marginTop="8dp" android:text="Delete" tools:layout_editor_absoluteX="148dp" tools:layout_editor_absoluteY="429dp" /> </RelativeLayout>
in AndroidManifest.xml add below given line
<activity android:name=".Add_success"></activity>
when you entered details and click on add button it shows Added successfully
and if you click on delete button it shows Record Deleted successfully
I am working on integrating a database to an Android application for a course. Currently, I have all the database code done, but getting all of the EditText fields to enter data into the database has...
Edit question this login java code allows to proceed to next activity even if no data is enetered. add a constraint which displays a message to fill all fields. package com.example.Divahalls; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class LoginActivity extends AppCompatActivity { Button btnMoveToDashboard; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); btnMoveToDashboard=findViewById(R.id.buttonMoveToDashboard); btnMoveToDashboard.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(LoginActivity.this,CustomerActivity.class); startActivity(intent); } }); } }
Can someone help me with my code.. I cant get an output. It says I do not have a main method. HELP PLEASE. The instruction are in bold on the bottom of the code. package SteppingStones; //Denisse.Carbo import java.util.Scanner; import java.util.ArrayList; import java.util.List; public class SteppingStone5_Recipe { private String recipeName; private int servings; private List<String> recipeIngredients; private double totalRecipeCalories; public String getRecipeName() { return recipeName; } public void setRecipeName (string recipeName){ this.recipeName = recipeName; } public int getServings() { return...
this customer java code allows to proceed to next activity even if no data is enetered. add a constraint which displays a message to fill all fields. import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.SpinnerAdapter; import java.util.ArrayList; import java.util.List; public class CustomerActivity extends AppCompatActivity { EditText editTextName, editTextEmail,editTextPhone, editTextMenu; Button btnBook; Spinner s; String packageName; SpinnerAdapter spinnerAdapter; List<String> packages=new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_customer);...
this java code allows to proceed to next activity even if no data is enetered. add a constraint which displays a message to fill all fields. import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class SignupActivity extends AppCompatActivity { EditText editTextName,editTextEmail,editTextPhone,editTextPswd,editTextCPswd,editTextAge,editTextFatherName; Button btnMoveToLogin; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signup); editTextName=findViewById(R.id.editTextName); editTextFatherName=findViewById(R.id.editTextFatherName); editTextAge=findViewById(R.id.editTextAge); editTextPhone=findViewById(R.id.editTextPhoneNumber); editTextPswd=findViewById(R.id.editTextPswd); editTextCPswd=findViewById(R.id.editTextCPswd); editTextEmail=findViewById(R.id.editTextEmail); btnMoveToLogin=findViewById(R.id.buttonMoveToLogIn); btnMoveToLogin.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Log.i("Name",editTextEmail.getText().toString()); if(editTextEmail.getText().toString()=="" &&...
Prompt: In this stepping stone lab assignment, you will build a Recipe class, getting user input to collect the recipe name and serving size, using the ingredient entry code from Stepping Stone Lab Four to add ingredients to the recipe, and calculating the calories per serving. Additionally, you will build your first custom method to print the recipe to the screen. Specifically, you will need to create the following: The instance variables for the class (recipeName, serving size, and...
Hi. Could you help me with the code below? I need to validate the code below, using expressions that can carry out the actions or that make appropriate changes to the program’s state, using conditional and iterative control structures that repeat actions as needed. The unit measurement is missing from the final output and I need it to offer options to lbs, oz, grams, tbsp, tsp, qt, pt, and gal. & fl. oz. Can this be added? The final output...
In Android Studio, I just can't find the errors to this problem, I have the following code written down, what I need is just how to make a direct route/directions between 2 specific coordinates, specifically I need the coordinates (34.782, -86.569) to have a direct route to (34.781, -86.571) thank you Here is Code: package com.example ------ import android.support.v4.app.FragmentActivity; import android.os.Bundle; import com.google.android.gms.maps.CameraUpdateFactory; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.OnMapReadyCallback; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MapsActivity extends FragmentActivity implements OnMapReadyCallback...
Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. Here is my activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...
Need help to edit the code below. Am using it play a lottery game in Android Studio what i need help with is if play one game u need to clear it up before u could another game. Here is my activity_main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="simpleapps.my.lotteryapplication.MainActivity" android:orientation="vertical" android:layout_margin="10dp" android:weightSum="100"> <TextView android:layout_width="match_parent" android:layout_height="0dp" android:text="Enter White Balls : " android:textColor="#000000" android:textSize="22sp" android:layout_weight="15" /> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:orientation="horizontal" android:weightSum="100" android:layout_margin="10dp" android:layout_weight="15"> <EditText android:id="@+id/wBall1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="20" android:inputType="number"/>...
I am getting this Error can you please fix my Java code. import java.awt.Dialog; import java.awt.Label; import java.awt.TextArea; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map.Entry; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JPanel; public class Fall_2017 { public TextArea courseInput; public Label textAreaLabel; public JButton addData; public Dialog confirmDialog; HashMap<Integer, ArrayList<String>> students; public Fall_2017(){ courseInput = new TextArea(20, 40); textAreaLabel = new Label("Student's data:"); addData = new JButton("Add...