From d7842955bf86c1d8ea63b2139ea8e057e628684f Mon Sep 17 00:00:00 2001 From: Michal Date: Sun, 9 Dec 2018 22:44:08 +0100 Subject: [PATCH] =?UTF-8?q?Poprawione=20wy=C5=9Bwietlanie=20listy=20Popraw?= =?UTF-8?q?ione=20dodawanie=20produkt=C3=B3w?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/AndroidManifest.xml | 6 +- .../shoplist/DatabaseHelper.java | 32 ++++--- .../shoplist/EditFragment.java | 39 ++++++++ .../shoplist/FloatingAction.java | 45 ++++++++-- .../shoplist/MainActivity.java | 90 ++++++++++++++----- .../shoplist/MenuFragment.java | 31 +++---- app/src/main/res/layout/popup_dialog.xml | 3 +- app/src/main/res/layout/product_view.xml | 9 +- app/src/main/res/menu/list_menu.xml | 2 +- app/src/main/res/values/strings.xml | 1 + 10 files changed, 190 insertions(+), 68 deletions(-) create mode 100644 app/src/main/java/michalpawlaczyk/shoplist/EditFragment.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index b606529..082895c 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -10,18 +10,18 @@ android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme" - tools:ignore="GoogleAppIndexingWarning"> + tools:ignore="GoogleAppIndexingWarning" + android:fullBackupContent="true"> - + - \ No newline at end of file diff --git a/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java b/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java index 5b5fb8b..b17d38e 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/DatabaseHelper.java @@ -18,7 +18,7 @@ public class DatabaseHelper extends SQLiteOpenHelper { @Override public void onCreate(SQLiteDatabase db) { - db.execSQL("create table " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT) "); + db.execSQL("create table " + TABLE_NAME + "(ID INTEGER, NAME TEXT) "); } @Override @@ -27,34 +27,44 @@ public class DatabaseHelper extends SQLiteOpenHelper { onCreate(db); } - boolean insertData(String name){ + boolean insertData(String tableName, String name){ SQLiteDatabase db = this.getWritableDatabase(); ContentValues contentValues = new ContentValues(); contentValues.put(COL_2, name); - long result = db.insert(TABLE_NAME, null, contentValues); + long result = db.insert(tableName, null, contentValues); return result != -1; } - Cursor getAlldata(){ + Cursor getChecekData(String tableName){ SQLiteDatabase db = this.getWritableDatabase(); - return db.rawQuery("select * from " + TABLE_NAME, null); + return db.rawQuery("select * from " + tableName, 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){ + boolean updateData (String name, String newName){ 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 }); + contentValues.put(COL_2, newName); + db.update(TABLE_NAME, contentValues, "NAME = ?", new String[]{ name }); return true; } - Integer deleteData (String name) { + Integer deleteData (String tableName, String name) { SQLiteDatabase db = this.getWritableDatabase(); - return db.delete(TABLE_NAME, "NAME = ?", new String[] { name }); + return db.delete(tableName, "NAME = ?", new String[] { name }); + } + void userCreateTable(String tableName){ + SQLiteDatabase db = this.getWritableDatabase();/* + db.rawQuery("CREATE TABLE IF NOT EXISTS " + tableName + + " ( ID INTEGER, " + + "NAME TEXT )", null);*/ + String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS " + tableName + + " ( ID INTEGER, " + + "NAME TEXT )"; + db.execSQL(CREATE_TABLE); + db.close(); } } diff --git a/app/src/main/java/michalpawlaczyk/shoplist/EditFragment.java b/app/src/main/java/michalpawlaczyk/shoplist/EditFragment.java new file mode 100644 index 0000000..c79d943 --- /dev/null +++ b/app/src/main/java/michalpawlaczyk/shoplist/EditFragment.java @@ -0,0 +1,39 @@ +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; +import android.widget.Button; +import android.widget.EditText; + +public class EditFragment extends Fragment { + private Button addListBtn; + EditText listNameText; + String text; + + + @Override + public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { + View view = inflater.inflate(R.layout.popup_dialog, container, false); + addListBtn = view.findViewById(R.id.addListBtn); + listNameText = view.findViewById(R.id.listNameText); + btnClick(); + return view; + + } + public void btnClick(){ + addListBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + text = listNameText.getText().toString(); + } + }); + } + public String getText(){ + return text; + } +} \ No newline at end of file diff --git a/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java b/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java index 3eb31e7..92fb51a 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/FloatingAction.java @@ -1,17 +1,27 @@ package michalpawlaczyk.shoplist; +import android.database.Cursor; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; +import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; +import android.widget.ListView; import android.widget.Toast; +import java.util.ArrayList; + public class FloatingAction extends AppCompatActivity { private Button addListBtn; EditText listNameText; DatabaseHelper myDb; + String chosenListID; + ArrayList productItem; + ArrayAdapter adapter2; + ListView listProductView; + @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -20,22 +30,43 @@ public class FloatingAction extends AppCompatActivity { addListBtn = findViewById(R.id.addListBtn); listNameText = findViewById(R.id.listNameText); myDb = new DatabaseHelper(this); + listProductView = findViewById(R.id.productsListView); + productItem = new ArrayList<>(); 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(); + Bundle extras = getIntent().getExtras(); + if(extras != null) { + chosenListID = extras.getString("chosenListID"); + boolean isInserted = myDb.insertData(chosenListID, 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(); + } + //reloadData(); } - else{ - Toast.makeText(FloatingAction.this, "Something goes wrong!", Toast.LENGTH_LONG).show(); - } - } }); } + + public void reloadData(){ + Cursor res = myDb.getChecekData(chosenListID); + 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); + } + } } diff --git a/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java b/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java index 5c462ab..91c1b6d 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/MainActivity.java @@ -15,9 +15,9 @@ import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckedTextView; +import android.widget.FrameLayout; import android.widget.ListView; import android.widget.Toast; - import java.util.ArrayList; import java.util.Objects; @@ -36,6 +36,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On Button editItem; FloatingActionButton fab; String listProductID; + String chosenListID; @@ -54,14 +55,18 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On listItem = new ArrayList<>(); listNameView = findViewById(R.id.listView); listProductView = findViewById(R.id.productsListView); + ctvProduct = findViewById(R.id.productCheck); productItem = new ArrayList<>(); - viewData(); viewTableName(); - fab = (FloatingActionButton) findViewById(R.id.fab); + fab = findViewById(R.id.fab); + listProductView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); + fab.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - startActivity(new Intent(MainActivity.this, FloatingAction.class)); + Intent intent = new Intent(MainActivity.this, FloatingAction.class); + intent.putExtra("chosenListID", chosenListID); + startActivity(intent); } }); @@ -69,17 +74,27 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On @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(); +/* + if(listProductView.isItemChecked(position)) { + ctvProduct.setPaintFlags(ctvProduct.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG); + } + else { + ctvProduct.setPaintFlags(ctvProduct.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); + }*/ + } + }); + listNameView.setOnItemClickListener(new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + chosenListID = parent.getItemAtPosition(position).toString(); + viewCheckData(chosenListID); } }); - - - ctvProduct = findViewById(R.id.productCheck); registerForContextMenu(listProductView); //registerForContextMenu(listNameView); - /* +/* ctvProduct.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { @@ -104,7 +119,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public boolean onContextItemSelected(MenuItem item) { switch(item.getItemId()) { case R.id.deleteItem: - Integer result = myDb.deleteData(listProductID); + Integer result = myDb.deleteData(chosenListID, listProductID); System.out.println(listProductID); if(result > 0){ reloadData(); @@ -117,7 +132,21 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On return true; case R.id.editItem: - System.out.println("DziaƂa 2"); + /* + getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, + new EditFragment()).commit(); + EditFragment editFragment = new EditFragment(); + String tmp = editFragment.getText(); + boolean result2 = myDb.updateData(chosenListID, tmp); + if(result2){ + reloadData(); + } + else { + reloadData(); + Toast.makeText(MainActivity.this, "Fail", Toast.LENGTH_SHORT).show(); + + }*/ + Toast.makeText(MainActivity.this, "Not supported yet", Toast.LENGTH_SHORT).show(); return true; default: @@ -138,7 +167,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { switch (menuItem.getItemId()){ case R.id.AddList: - getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, + ((FrameLayout)findViewById(R.id.content_frame)).removeAllViews(); + getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, new MenuFragment()).commit(); break; } @@ -146,22 +176,22 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On return true; } - public void viewData(){ - Cursor res = myDb.getAlldata(); - if(res.getCount() == 0){ - Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show(); + public void viewCheckData(String tableName){ + Cursor res = myDb.getChecekData(tableName); + adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem); + listProductView.setAdapter(adapter2); + if(adapter2.getCount() > 0){ + adapter2.clear(); } - else { - while (res.moveToNext()) { - productItem.add(res.getString(1)); + while (res.moveToNext()) { + productItem.add(res.getString(1)); - } - adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem); - listProductView.setAdapter(adapter2); } + adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem); + listProductView.setAdapter(adapter2); } public void reloadData(){ - Cursor res = myDb.getAlldata(); + Cursor res = myDb.getChecekData(chosenListID); adapter2.clear(); if(res.getCount() == 0){ Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show(); @@ -178,6 +208,19 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On public void viewTableName(){ Cursor res = myDb.getAllTableName(); + if (res.moveToFirst()) { + while ( !res.isAfterLast() ) { + listItem.add(res.getString(res.getColumnIndex("name"))); + System.out.println(res.getString( res.getColumnIndex("name"))); + res.moveToNext(); + } + } + adapter = new ArrayAdapter<>(this, R.layout.list_view, listItem); + listNameView.setAdapter(adapter); + } + public void reloadTableName(){ + Cursor res = myDb.getAllTableName(); + adapter.clear(); if (res.moveToFirst()) { while ( !res.isAfterLast() ) { listItem.add( res.getString( res.getColumnIndex("name")) ); @@ -187,4 +230,5 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On adapter = new ArrayAdapter<>(this, R.layout.list_view, listItem); listNameView.setAdapter(adapter); } + } diff --git a/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java b/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java index 0ea9719..9d0fc9e 100644 --- a/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java +++ b/app/src/main/java/michalpawlaczyk/shoplist/MenuFragment.java @@ -10,14 +10,14 @@ import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; -import android.widget.Toast; -public class MenuFragment extends Fragment { +import java.util.Objects; + +public class MenuFragment extends Fragment{ private Button addListBtn; EditText listNameText; DatabaseHelper myDb; - //Context context; @Override @@ -26,24 +26,17 @@ public class MenuFragment extends Fragment { addListBtn = view.findViewById(R.id.addListBtn); listNameText = view.findViewById(R.id.listNameText); myDb = new DatabaseHelper(getActivity()); - AddData(); + AddTable(); return view; } - - 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(getActivity(), "Done", Toast.LENGTH_LONG).show(); - } - else{ - Toast.makeText(getActivity(), "Something goes wrong!", Toast.LENGTH_LONG).show(); - } - - } - }); + public void AddTable(){ + addListBtn.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + myDb.userCreateTable(listNameText.getText().toString()); + ((MainActivity)Objects.requireNonNull(getActivity())).reloadTableName(); + } + }); } } diff --git a/app/src/main/res/layout/popup_dialog.xml b/app/src/main/res/layout/popup_dialog.xml index 13680d5..544fa33 100644 --- a/app/src/main/res/layout/popup_dialog.xml +++ b/app/src/main/res/layout/popup_dialog.xml @@ -3,7 +3,8 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - android:orientation="vertical"> + android:orientation="vertical" + android:id="@+id/popup_dailog"> + android:checked="false" + android:gravity="center" /> diff --git a/app/src/main/res/menu/list_menu.xml b/app/src/main/res/menu/list_menu.xml index c22834e..aecd4a9 100644 --- a/app/src/main/res/menu/list_menu.xml +++ b/app/src/main/res/menu/list_menu.xml @@ -3,7 +3,7 @@ xmlns:app="http://schemas.android.com/apk/res-auto"> + android:title="@string/AddListButton" /> example@example.com Twoje Listy Dodaj + Nowa lista Open Close Ustawienia