· 6 years ago · Sep 30, 2019, 03:47 AM
1DELETEACTIVITY - DO NOT COPY THESE - //////////////////// - SPLITS SECTIONS
2
3package com.jblearning.candystorev2;
4
5import android.os.Bundle;
6import android.support.v7.app.AppCompatActivity;
7import android.view.View;
8import android.widget.EditText;
9import android.widget.RelativeLayout;
10import android.widget.Toast;
11import android.widget.Button;
12import android.widget.RadioButton;
13import android.widget.RadioGroup;
14import android.widget.ScrollView;
15import android.widget.Toast;
16import java.util.ArrayList;
17
18
19public class DeleteActivity extends AppCompatActivity{
20 private DatabaseManager dbManager;
21
22 public void onCreate(Bundle savedInstanceState){
23 super.onCreate(savedInstanceState);
24 dbManager = new DatabaseManager(this);
25 updateView();
26 }
27
28 //Build a View dynamically with all the candies
29 public void updateView() {
30 ArrayList<Candy> candies = dbManager.selectAll();
31 RelativeLayout layout = new RelativeLayout(this);
32 ScrollView scrollView = new ScrollView(this);
33 RadioGroup group = new RadioGroup(this);
34 for (Candy candy : candies) {
35 RadioButton rb = new RadioButton(this);
36 rb.setId(candy.getId());
37 rb.setText(candy.toString());
38 group.addView(rb);
39 }
40
41 //set up event handling
42 RadioButtonHandler rbh = new RadioButtonHandler();
43 group.setOnCheckedChangeListener(rbh);
44
45 //create a back button
46 Button backButton = new Button(this);
47 backButton.setText(R.string.button_back);
48 backButton.setOnClickListener(new View.OnClickListener() {
49 @Override
50 public void onClick(View v) {
51 DeleteActivity.this.finish();
52 }
53 });
54
55 scrollView.addView(group);
56 layout.addView(scrollView);
57
58 //add back button at bottom
59 RelativeLayout.LayoutParams params
60 = new RelativeLayout.LayoutParams(
61 RelativeLayout.LayoutParams.WRAP_CONTENT,
62 RelativeLayout.LayoutParams.WRAP_CONTENT);
63 params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
64 params.addRule(RelativeLayout.CENTER_HORIZONTAL);
65 params.setMargins(0, 0, 0, 50);
66 layout.addView(layout);
67 setContentView(layout);
68 }
69
70
71 private class RadioButtonHandler
72 implements RadioGroup.OnCheckedChangeListener{
73 public void onCheckedChanged(RadioGroup group, int checkedId) {
74 //delete candy from database
75 dbManager.deleteById(checkedId);
76 Toast.makeText(DeleteActivity.this, "Candy deleted", Toast.LENGTH_SHORT).show();
77
78 updateView();
79
80 }
81
82
83 }
84}
85
86////////////////////
87
88MAINACTIVITY
89
90package com.jblearning.candystorev2;
91
92import android.content.Intent;
93import android.os.Bundle;
94import android.support.v7.app.AppCompatActivity;
95import android.support.v7.widget.Toolbar;
96import android.util.Log;
97import android.view.Menu;
98import android.view.MenuItem;
99
100public class MainActivity extends AppCompatActivity {
101
102 @Override
103 protected void onCreate( Bundle savedInstanceState ) {
104 super.onCreate( savedInstanceState );
105 setContentView( R.layout.activity_main );
106 Toolbar toolbar = (Toolbar) findViewById( R.id.toolbar );
107 setSupportActionBar( toolbar );
108 }
109
110 @Override
111 public boolean onCreateOptionsMenu( Menu menu ) {
112 getMenuInflater( ).inflate( R.menu.menu_main, menu );
113 return true;
114 }
115
116 @Override
117 public boolean onOptionsItemSelected(MenuItem item) {
118 int id = item.getItemId( );
119 switch ( id ) {
120 case R.id.action_add:
121 Log.w( "MainActivity", "Add selected" );
122 Intent insertIntent = new Intent( this, InsertActivity.class );
123 this.startActivity( insertIntent );
124 return true;
125 case R.id.action_delete:
126 Log.w( "MainActivity", "Delete selected" );
127 Intent deleteIntent = new Intent(this, DeleteActivity.class);
128 this.startActivity(deleteIntent);
129 return true;
130 case R.id.action_update:
131 Log.w( "MainActivity", "Update selected" );
132 Intent updateIntent = new Intent(this, UpdateActivity.class);
133 this.startActivity(updateIntent);
134 return true;
135 default:
136 return super.onOptionsItemSelected( item );
137 }
138 }
139}
140
141////////////////////////////
142
143DATABASEMANAGER
144
145package com.jblearning.candystorev2;
146
147import android.content.Context;
148import android.database.Cursor;
149import android.database.sqlite.SQLiteDatabase;
150import android.database.sqlite.SQLiteOpenHelper;
151import java.util.ArrayList;
152
153public class DatabaseManager extends SQLiteOpenHelper {
154 private static final String DATABASE_NAME = "candyDB";
155 private static final int DATABASE_VERSION = 1;
156 private static final String TABLE_CANDY = "candy";
157 private static final String ID = "id";
158 private static final String NAME = "name";
159 private static final String PRICE = "price";
160
161 public DatabaseManager( Context context ) {
162 super( context, DATABASE_NAME, null, DATABASE_VERSION );
163 }
164
165 public void onCreate( SQLiteDatabase db ) {
166 // build sql create statement
167 String sqlCreate = "create table " + TABLE_CANDY + "( " + ID;
168 sqlCreate += " integer primary key autoincrement, " + NAME;
169 sqlCreate += " text, " + PRICE + " real )" ;
170
171 db.execSQL( sqlCreate );
172 }
173
174 public void onUpgrade( SQLiteDatabase db,
175 int oldVersion, int newVersion ) {
176 // Drop old table if it exists
177 db.execSQL( "drop table if exists " + TABLE_CANDY );
178 // Re-create tables
179 onCreate( db );
180 }
181
182 public void insert( Candy candy ) {
183 SQLiteDatabase db = this.getWritableDatabase( );
184 String sqlInsert = "insert into " + TABLE_CANDY;
185 sqlInsert += " values( null, '" + candy.getName( );
186 sqlInsert += "', '" + candy.getPrice( ) + "' )";
187
188 db.execSQL( sqlInsert );
189 db.close( );
190 }
191
192 public void deleteById( int id ) {
193 SQLiteDatabase db = this.getWritableDatabase( );
194 String sqlDelete = "delete from " + TABLE_CANDY;
195 sqlDelete += " where " + ID + " = " + id;
196
197 db.execSQL( sqlDelete );
198 db.close( );
199 }
200
201 public void updateById( int id, String name, double price ) {
202 SQLiteDatabase db = this.getWritableDatabase();
203
204 String sqlUpdate = "update " + TABLE_CANDY;
205 sqlUpdate += " set " + NAME + " = '" + name + "', ";
206 sqlUpdate += PRICE + " = '" + price + "'";
207 sqlUpdate += " where " + ID + " = " + id;
208
209 db.execSQL( sqlUpdate );
210 db.close( );
211 }
212
213 public ArrayList<Candy> selectAll( ) {
214 String sqlQuery = "select * from " + TABLE_CANDY;
215
216 SQLiteDatabase db = this.getWritableDatabase( );
217 Cursor cursor = db.rawQuery( sqlQuery, null );
218
219 ArrayList<Candy> candies = new ArrayList<Candy>( );
220 while( cursor.moveToNext( ) ) {
221 Candy currentCandy
222 = new Candy( Integer.parseInt( cursor.getString( 0 ) ),
223 cursor.getString( 1 ), cursor.getDouble( 2 ) );
224 candies.add( currentCandy );
225 }
226 db.close( );
227 return candies;
228 }
229
230 public Candy selectById( int id ) {
231 String sqlQuery = "select * from " + TABLE_CANDY;
232 sqlQuery += " where " + ID + " = " + id;
233
234 SQLiteDatabase db = this.getWritableDatabase( );
235 Cursor cursor = db.rawQuery( sqlQuery, null );
236
237 Candy candy = null;
238 if( cursor.moveToFirst( ) )
239 candy = new Candy( Integer.
240parseInt( cursor.getString( 0 ) ),
241 cursor.getString( 1 ), cursor.getDouble( 2 ) );
242 return candy;
243 }
244}
245
246
247////////////////////////////
248
249UPDATEACTIVITY - DO NOT COPY AND PASTE THESE ALL CAPATALIZE THINGIES
250
251package com.jblearning.candystorev2;
252
253
254import android.graphics.Point;
255import android.os.Bundle;
256import android.support.v7.app.AppCompatActivity;
257import android.text.InputType;
258import android.view.Gravity;
259import android.view.View;
260import android.view.ViewGroup;
261import android.widget.EditText;
262import android.widget.GridLayout;
263import android.widget.RelativeLayout;
264import android.widget.TextView;
265import android.widget.Toast;
266import android.widget.Button;
267import android.widget.RadioButton;
268import android.widget.RadioGroup;
269import android.widget.ScrollView;
270import android.widget.Toast;
271import java.util.ArrayList;
272
273public class UpdateActivity extends AppCompatActivity{
274 DatabaseManager dbManager;
275
276 public void onCreate(Bundle savedInstanceState){
277 super.onCreate(savedInstanceState);
278 dbManager = new DatabaseManager(this);
279 updateView();
280 }
281
282 //Build a View dynamically with all the candies
283 public void updateView() {
284 ArrayList<Candy> candies = dbManager.selectAll();
285 if (candies.size() > 0) {
286 //create ScrollView and GridLayout
287 ScrollView scrollView = new ScrollView(this);
288 GridLayout grid = new GridLayout(this);
289 grid.setRowCount(candies.size());
290 grid.setColumnCount(4);
291
292 //create arrays of components
293 TextView[] ids = new TextView[candies.size()];
294 EditText[][] namesAndPrices = new EditText[candies.size()][2];
295 Button[] buttons = new Button[candies.size()];
296 ButtonHandler bh = new ButtonHandler();
297
298 //retrieve width of screen
299 Point size = new Point();
300 getWindowManager().getDefaultDisplay().getSize(size);
301 int width = size.x;
302 int i = 0;
303
304 for (Candy candy : candies) {
305 //creates the TextView for the candy's id
306 ids[i] = new TextView(this);
307 ids[i].setGravity(Gravity.CENTER);
308 ids[i].setText("" + candy.getId());
309
310 namesAndPrices[i][0] = new EditText(this);
311 namesAndPrices[i][1] = new EditText(this);
312 namesAndPrices[i][0].setText(candy.getName());
313 namesAndPrices[i][1].setText("" + candy.getPrice());
314 namesAndPrices[i][1].setInputType(InputType.TYPE_CLASS_NUMBER);
315 namesAndPrices[i][0].setId(10 * candy.getId());
316 namesAndPrices[i][1].setId(10 * candy.getId() + 1);
317
318 //creates the button
319 buttons[i] = new Button(this);
320 buttons[i].setText("Update");
321 buttons[i].setId(candy.getId());
322
323 //set up event handling
324 buttons[i].setOnClickListener(bh);
325
326 // add the elements to grid
327 grid.addView(ids[i], width / 10,
328 ViewGroup.LayoutParams.WRAP_CONTENT);
329 grid.addView(namesAndPrices[i][0], (int) (width * .4),
330 ViewGroup.LayoutParams.WRAP_CONTENT);
331 grid.addView(namesAndPrices[i][1], (int) (width * .15),
332 ViewGroup.LayoutParams.WRAP_CONTENT);
333 grid.addView(buttons[i], (int) (width * .35),
334 ViewGroup.LayoutParams.WRAP_CONTENT);
335
336 i++;
337 }
338 scrollView.addView(grid);
339 setContentView(scrollView);
340 }
341 }
342 private class ButtonHandler implements View.OnClickListener{
343 public void onClick(View v){
344 //retrieve name and price
345 int candyId = v.getId();
346 EditText nameET = (EditText) findViewById(10 * candyId);
347 EditText priceET = (EditText) findViewById(10 * candyId + 1);
348 String name = nameET.getText().toString();
349 String priceString = priceET.getText().toString();
350
351 try {
352 double price = Double.parseDouble(priceString);
353 dbManager.updateById(candyId, name, price);
354 Toast.makeText(UpdateActivity.this, "Candy updated",
355 Toast.LENGTH_SHORT).show();
356
357 //update
358 updateView();
359 }catch (NumberFormatException nfe){
360 Toast.makeText(UpdateActivity.this,
361 "Price error", Toast.LENGTH_LONG).show();
362 }
363 }
364 }
365}
366
367
368//////////////////////////////////////////
369
370MANIFEST
371
372<?xml version="1.0" encoding="utf-8"?>
373<manifest package="com.jblearning.candystorev2"
374 xmlns:android="http://schemas.android.com/apk/res/android">
375
376 <application
377 android:allowBackup="true"
378 android:icon="@mipmap/ic_launcher"
379 android:label="@string/app_name"
380 android:supportsRtl="true"
381 android:theme="@style/AppTheme">
382 <activity
383 android:name=".MainActivity"
384 android:label="@string/app_name"
385 android:theme="@style/AppTheme.NoActionBar"
386 android:screenOrientation="portrait">
387 <intent-filter>
388 <action android:name="android.intent.action.MAIN"/>
389
390 <category android:name="android.intent.category.LAUNCHER"/>
391 </intent-filter>
392 </activity>
393 <activity
394 android:name=".InsertActivity"
395 android:screenOrientation="portrait">
396 </activity>
397 <activity
398 android:name=".DeleteActivity"
399 android:screenOrientation="portrait">
400 </activity>
401 <activity
402 android:name=".UpdateActivity"
403 android:screenOrientation="portrait">
404 </activity>
405 </application>
406
407</manifest>