From 6346a71b7f6dca0ad289b81624c0e3db7c784d4e Mon Sep 17 00:00:00 2001 From: Michal Date: Fri, 7 Dec 2018 17:51:20 +0100 Subject: [PATCH] Fab Usuwanie danych --- .idea/misc.xml | 2 +- app/src/main/AndroidManifest.xml | 2 + .../shoplist/AddProductFragment.java | 17 +++ .../shoplist/DatabaseHelper.java | 41 +++---- .../shoplist/FloatingAction.java | 41 +++++++ .../shoplist/MainActivity.java | 110 ++++++++++++++++-- .../shoplist/MenuFragment.java | 4 +- app/src/main/res/layout/edit_popup.xml | 36 ++++++ app/src/main/res/layout/list_view.xml | 12 +- app/src/main/res/layout/popup_dialog.xml | 3 +- app/src/main/res/layout/product_add.xml | 32 +++++ app/src/main/res/layout/product_view.xml | 10 ++ app/src/main/res/menu/context_menu.xml | 2 + app/src/main/res/values/strings.xml | 3 +- 14 files changed, 269 insertions(+), 46 deletions(-) create mode 100644 app/src/main/java/michalpawlaczyk/shoplist/AddProductFragment.java create mode 100644 app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java create mode 100644 app/src/main/res/layout/edit_popup.xml create mode 100644 app/src/main/res/layout/product_add.xml create mode 100644 app/src/main/res/layout/product_view.xml diff --git a/.idea/misc.xml b/.idea/misc.xml index b0c7b20..e0d5b93 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -29,7 +29,7 @@ - + diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 2da24c6..b606529 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -18,6 +18,8 @@ + + diff --git a/app/src/main/java/michalpawlaczyk/shoplist/AddProductFragment.java b/app/src/main/java/michalpawlaczyk/shoplist/AddProductFragment.java new file mode 100644 index 0000000..877f3be --- /dev/null +++ b/app/src/main/java/michalpawlaczyk/shoplist/AddProductFragment.java @@ -0,0 +1,17 @@ +package michalpawlaczyk.shoplist; + +import android.os.Bundle; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.support.v4.app.Fragment; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; + +public class AddProductFragment extends Fragment { + @Nullable + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + return inflater.inflate(R.layout.product_add, container, false); + } +} diff --git a/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java b/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java index a739963..5b5fb8b 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java @@ -9,10 +9,10 @@ import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { private static final String DATABASE_NAME = "Products.db"; private static final String TABLE_NAME = "products_list"; - public static final String COL_1 = "ID"; + private static final String COL_1 = "ID"; private static final String COL_2 = "NAME"; - public DatabaseHelper(Context context) { + DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); } @@ -27,33 +27,34 @@ public class DatabaseHelper extends SQLiteOpenHelper { onCreate(db); } - public boolean insertData(String name){ + boolean insertData(String name){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_2, name); long result = db.insert(TABLE_NAME, null, contentValues); - if (result == -1) { - return false; - } - else { - return true; - } + return result != -1; } - public Cursor getAlldata(){ + Cursor getAlldata(){ SQLiteDatabase db = this.getWritableDatabase(); - Cursor res = db.rawQuery("select * from " + TABLE_NAME, null); - return res; + return db.rawQuery("select * from " + TABLE_NAME, null); } - public Cursor getAllTableName(){ + Cursor getAllTableName(){ SQLiteDatabase db = this.getWritableDatabase(); - Cursor res = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); - /* - if (res.moveToFirst()) { - while ( !res.isAfterLast() ) { - res.moveToNext(); - }*/ - return res; + return db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); + } + boolean updateData (String id, String name){ + SQLiteDatabase db = this.getWritableDatabase(); + ContentValues contentValues = new ContentValues(); + contentValues.put(COL_1, id); + contentValues.put(COL_2, name); + db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{ id }); + return true; + } + + Integer deleteData (String name) { + SQLiteDatabase db = this.getWritableDatabase(); + return db.delete(TABLE_NAME, "NAME = ?", new String[] { name }); } } diff --git a/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java b/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java new file mode 100644 index 0000000..3eb31e7 --- /dev/null +++ b/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java @@ -0,0 +1,41 @@ +package michalpawlaczyk.shoplist; + +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; +import android.widget.EditText; +import android.widget.Toast; + +public class FloatingAction extends AppCompatActivity { + private Button addListBtn; + EditText listNameText; + DatabaseHelper myDb; + + @Override + protected void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.popup_dialog); + addListBtn = findViewById(R.id.addListBtn); + listNameText = findViewById(R.id.listNameText); + myDb = new DatabaseHelper(this); + AddData(); + } + public void AddData(){ + addListBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + boolean isInserted = myDb.insertData(listNameText.getText().toString()); + if(isInserted){ + Toast.makeText(FloatingAction.this, "Done", Toast.LENGTH_LONG).show(); + + } + else{ + Toast.makeText(FloatingAction.this, "Something goes wrong!", Toast.LENGTH_LONG).show(); + } + + } + }); + } +} diff --git a/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java b/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java index 7b974c0..5c462ab 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java @@ -1,7 +1,9 @@ package michalpawlaczyk.shoplist; +import android.content.Intent; import android.database.Cursor; import android.support.annotation.NonNull; +import android.support.design.widget.FloatingActionButton; import android.support.design.widget.NavigationView; import android.support.v4.view.GravityCompat; import android.support.v4.widget.DrawerLayout; @@ -11,6 +13,8 @@ import android.os.Bundle; import android.view.*; import android.widget.AdapterView; import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.CheckedTextView; import android.widget.ListView; import android.widget.Toast; @@ -23,8 +27,16 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On private ActionBarDrawerToggle mToggle; DatabaseHelper myDb; ArrayList listItem; + ArrayList productItem; ArrayAdapter adapter; + ArrayAdapter adapter2; ListView listNameView; + ListView listProductView; + CheckedTextView ctvProduct; + Button editItem; + FloatingActionButton fab; + String listProductID; + @Override @@ -41,16 +53,76 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On myDb = new DatabaseHelper(this); listItem = new ArrayList<>(); listNameView = findViewById(R.id.listView); - //viewData(); + listProductView = findViewById(R.id.productsListView); + productItem = new ArrayList<>(); + viewData(); viewTableName(); - - listNameView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + fab = (FloatingActionButton) findViewById(R.id.fab); + fab.setOnClickListener(new View.OnClickListener() { @Override - public void onItemClick(AdapterView parent, View view, int position, long id) { - String text = listNameView.getItemAtPosition(position).toString(); - Toast.makeText(MainActivity.this, " "+text, Toast.LENGTH_SHORT).show(); + public void onClick(View v) { + startActivity(new Intent(MainActivity.this, FloatingAction.class)); } }); + + listProductView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + listProductID = parent.getItemAtPosition(position).toString(); + Toast.makeText(MainActivity.this, " "+listProductID, Toast.LENGTH_SHORT).show(); + } + }); + + + ctvProduct = findViewById(R.id.productCheck); + + registerForContextMenu(listProductView); + //registerForContextMenu(listNameView); + + /* + ctvProduct.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + if(ctvProduct.isChecked()){ + ctvProduct.setChecked(false); + } + else + ctvProduct.setChecked(true); + } + } + );*/ + } + @Override + public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) { + super.onCreateContextMenu(menu, v, menuInfo); + getMenuInflater().inflate(R.menu.context_menu, menu); + + + } + + @Override + public boolean onContextItemSelected(MenuItem item) { + switch(item.getItemId()) { + case R.id.deleteItem: + Integer result = myDb.deleteData(listProductID); + System.out.println(listProductID); + if(result > 0){ + reloadData(); + } + else { + reloadData(); + Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show(); + + } + + return true; + case R.id.editItem: + System.out.println("Działa 2"); + return true; + + default: + return super.onContextItemSelected(item); + } } @Override @@ -74,21 +146,35 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On return true; } - /*public void viewData(){ + public void viewData(){ Cursor res = myDb.getAlldata(); if(res.getCount() == 0){ Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show(); } else { while (res.moveToNext()) { - listItem.add(res.getString(1)); + productItem.add(res.getString(1)); } - adapter = new ArrayAdapter<>(this, R.layout.list_view, listItem); - listNameView.setAdapter(adapter); - + adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem); + listProductView.setAdapter(adapter2); } - }*/ + } + public void reloadData(){ + Cursor res = myDb.getAlldata(); + adapter2.clear(); + if(res.getCount() == 0){ + Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show(); + } + else { + while (res.moveToNext()) { + productItem.add(res.getString(1)); + + } + adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem); + listProductView.setAdapter(adapter2); + } + } public void viewTableName(){ Cursor res = myDb.getAllTableName(); diff --git a/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java b/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java index 8dc5071..0ea9719 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java @@ -1,6 +1,6 @@ package michalpawlaczyk.shoplist; -import android.content.Context; +//import android.content.Context; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -17,7 +17,7 @@ public class MenuFragment extends Fragment { private Button addListBtn; EditText listNameText; DatabaseHelper myDb; - Context context; + //Context context; @Override diff --git a/app/src/main/res/layout/edit_popup.xml b/app/src/main/res/layout/edit_popup.xml new file mode 100644 index 0000000..fbbd3d9 --- /dev/null +++ b/app/src/main/res/layout/edit_popup.xml @@ -0,0 +1,36 @@ + + + + + +