Usuwanie danych
This commit is contained in:
Michal 2018-12-07 17:51:20 +01:00
parent 49e4fd94ce
commit 6346a71b7f
14 changed files with 269 additions and 46 deletions

View File

@ -29,7 +29,7 @@
</value>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/build/classes" />
</component>
<component name="ProjectType">

View File

@ -18,6 +18,8 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".FloatingAction"/>
</application>

View File

@ -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);
}
}

View File

@ -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;
return result != -1;
}
else {
Cursor getAlldata(){
SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("select * from " + TABLE_NAME, null);
}
Cursor getAllTableName(){
SQLiteDatabase db = this.getWritableDatabase();
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;
}
}
public Cursor getAlldata(){
Integer deleteData (String name) {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from " + TABLE_NAME, null);
return res;
}
public 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.delete(TABLE_NAME, "NAME = ?", new String[] { name });
}
}

View File

@ -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();
}
}
});
}
}

View File

@ -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<String> listItem;
ArrayList<String> productItem;
ArrayAdapter<String> adapter;
ArrayAdapter<String> 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();

View File

@ -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

View File

@ -0,0 +1,36 @@
<?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="wrap_content"
android:orientation="vertical">
<EditText
android:id="@+id/editRow"
android:layout_width="306dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="28dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="26dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:gravity=""
tools:ignore="Autofill" />
<Button
android:id="@+id/editBtn"
android:layout_below="@id/editRow"
android:layout_marginTop="60dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/editRow"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="@string/AddButton" />
</RelativeLayout>

View File

@ -1,16 +1,10 @@
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_height="wrap_content"
android:weightSum="100"
tools:ignore="ExtraText">
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:baselineAligned="false"
android:textSize="16sp"
android:textSize="17sp"
android:gravity="center_vertical"
android:text="@string/Product_name"
android:textColor="#000"
</TextView>
android:textColor="#000" />

View File

@ -12,7 +12,8 @@
android:layout_marginEnd="12dp"
android:layout_marginStart="12dp"
android:ems="10"
android:inputType="textAutoComplete" />
android:inputType="textAutoComplete"
tools:ignore="Autofill,LabelFor" />
<Button
android:layout_below="@id/listNameText"

View File

@ -0,0 +1,32 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/addProductText"
android:layout_width="306dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="28dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="26dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
tools:layout_editor_absoluteX="72dp"
tools:layout_editor_absoluteY="16dp" />
<Button
android:id="@+id/addProductBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="@+id/addProductText"
android:layout_alignParentTop="true"
android:layout_marginTop="66dp"
android:text="@string/AddButton" />
</RelativeLayout>

View File

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/productCheck"
android:text=""
android:textSize="20sp"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:checked="false"/>

View File

@ -1,7 +1,9 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/deleteItem"
android:title="@string/Delete"/>
<item android:id="@+id/editItem"
android:title="@string/Edit"/>
</menu>

View File

@ -3,7 +3,7 @@
<string name="AccountText">Użytkownik</string>
<string name="EmailText">example@example.com</string>
<string name="ProductList">Twoje Listy</string>
<string name="AddButton">Dodaj listę</string>
<string name="AddButton">Dodaj</string>
<string name="Open">Open</string>
<string name="Close">Close</string>
<string name="Settings">Ustawienia</string>
@ -13,4 +13,5 @@
<string name="Popup_btn">Potwierdź</string>
<string name="Delete">Usuń</string>
<string name="Edit">Edytuj</string>
<string name="AddProduct">Wprowadź nazwę produktu</string>
</resources>