Poprawione wyświetlanie listy
Poprawione dodawanie produktów
This commit is contained in:
parent
6346a71b7f
commit
d7842955bf
@ -10,7 +10,8 @@
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/AppTheme"
|
||||
tools:ignore="GoogleAppIndexingWarning">
|
||||
tools:ignore="GoogleAppIndexingWarning"
|
||||
android:fullBackupContent="true">
|
||||
<activity android:name=".MainActivity">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
@ -21,7 +22,6 @@
|
||||
<activity android:name=".FloatingAction"/>
|
||||
|
||||
|
||||
|
||||
</application>
|
||||
|
||||
</manifest>
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
39
app/src/main/java/michalpawlaczyk/shoplist/EditFragment.java
Normal file
39
app/src/main/java/michalpawlaczyk/shoplist/EditFragment.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
@ -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<String> productItem;
|
||||
ArrayAdapter<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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();
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -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">
|
||||
|
||||
<EditText
|
||||
android:id="@+id/listNameText"
|
||||
|
@ -1,10 +1,13 @@
|
||||
<?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:layout_height="45dp"
|
||||
android:orientation="vertical"
|
||||
android:id="@+id/productCheck"
|
||||
android:text=""
|
||||
android:text="test"
|
||||
android:paddingStart="15dp"
|
||||
android:paddingEnd="2dp"
|
||||
android:textSize="20sp"
|
||||
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
|
||||
android:checked="false"/>
|
||||
android:checked="false"
|
||||
android:gravity="center" />
|
||||
|
@ -3,7 +3,7 @@
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto">
|
||||
|
||||
<item android:id="@+id/AddList"
|
||||
android:title="@string/AddButton" />
|
||||
android:title="@string/AddListButton" />
|
||||
<item android:id="@+id/Reminder"
|
||||
android:title="@string/Reminder"/>
|
||||
<item android:id="@+id/ProductList"
|
||||
|
@ -4,6 +4,7 @@
|
||||
<string name="EmailText">example@example.com</string>
|
||||
<string name="ProductList">Twoje Listy</string>
|
||||
<string name="AddButton">Dodaj</string>
|
||||
<string name="AddListButton">Nowa lista</string>
|
||||
<string name="Open">Open</string>
|
||||
<string name="Close">Close</string>
|
||||
<string name="Settings">Ustawienia</string>
|
||||
|
Loading…
Reference in New Issue
Block a user