Poprawione wyświetlanie listy

Poprawione dodawanie produktów
This commit is contained in:
Michal 2018-12-09 22:44:08 +01:00
parent 6346a71b7f
commit d7842955bf
10 changed files with 190 additions and 68 deletions

View File

@ -10,7 +10,8 @@
android:roundIcon="@mipmap/ic_launcher_round" android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true" android:supportsRtl="true"
android:theme="@style/AppTheme" android:theme="@style/AppTheme"
tools:ignore="GoogleAppIndexingWarning"> tools:ignore="GoogleAppIndexingWarning"
android:fullBackupContent="true">
<activity android:name=".MainActivity"> <activity android:name=".MainActivity">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
@ -21,7 +22,6 @@
<activity android:name=".FloatingAction"/> <activity android:name=".FloatingAction"/>
</application> </application>
</manifest> </manifest>

View File

@ -18,7 +18,7 @@ public class DatabaseHelper extends SQLiteOpenHelper {
@Override @Override
public void onCreate(SQLiteDatabase db) { 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 @Override
@ -27,34 +27,44 @@ public class DatabaseHelper extends SQLiteOpenHelper {
onCreate(db); onCreate(db);
} }
boolean insertData(String name){ boolean insertData(String tableName, String name){
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(COL_2, name); contentValues.put(COL_2, name);
long result = db.insert(TABLE_NAME, null, contentValues); long result = db.insert(tableName, null, contentValues);
return result != -1; return result != -1;
} }
Cursor getAlldata(){ Cursor getChecekData(String tableName){
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("select * from " + TABLE_NAME, null); return db.rawQuery("select * from " + tableName, null);
} }
Cursor getAllTableName(){ Cursor getAllTableName(){
SQLiteDatabase db = this.getWritableDatabase(); SQLiteDatabase db = this.getWritableDatabase();
return db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null); 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(); SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues(); ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id); contentValues.put(COL_2, newName);
contentValues.put(COL_2, name); db.update(TABLE_NAME, contentValues, "NAME = ?", new String[]{ name });
db.update(TABLE_NAME, contentValues, "ID = ?", new String[]{ id });
return true; return true;
} }
Integer deleteData (String name) { Integer deleteData (String tableName, String name) {
SQLiteDatabase db = this.getWritableDatabase(); 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();
} }
} }

View 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;
}
}

View File

@ -1,17 +1,27 @@
package michalpawlaczyk.shoplist; package michalpawlaczyk.shoplist;
import android.database.Cursor;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity; import android.support.v7.app.AppCompatActivity;
import android.view.View; import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import java.util.ArrayList;
public class FloatingAction extends AppCompatActivity { public class FloatingAction extends AppCompatActivity {
private Button addListBtn; private Button addListBtn;
EditText listNameText; EditText listNameText;
DatabaseHelper myDb; DatabaseHelper myDb;
String chosenListID;
ArrayList<String> productItem;
ArrayAdapter<String> adapter2;
ListView listProductView;
@Override @Override
protected void onCreate(@Nullable Bundle savedInstanceState) { protected void onCreate(@Nullable Bundle savedInstanceState) {
@ -20,22 +30,43 @@ public class FloatingAction extends AppCompatActivity {
addListBtn = findViewById(R.id.addListBtn); addListBtn = findViewById(R.id.addListBtn);
listNameText = findViewById(R.id.listNameText); listNameText = findViewById(R.id.listNameText);
myDb = new DatabaseHelper(this); myDb = new DatabaseHelper(this);
listProductView = findViewById(R.id.productsListView);
productItem = new ArrayList<>();
AddData(); AddData();
} }
public void AddData(){ public void AddData(){
addListBtn.setOnClickListener(new View.OnClickListener() { addListBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
boolean isInserted = myDb.insertData(listNameText.getText().toString()); Bundle extras = getIntent().getExtras();
if(extras != null) {
chosenListID = extras.getString("chosenListID");
boolean isInserted = myDb.insertData(chosenListID, listNameText.getText().toString());
if (isInserted) { if (isInserted) {
Toast.makeText(FloatingAction.this, "Done", Toast.LENGTH_LONG).show(); Toast.makeText(FloatingAction.this, "Done", Toast.LENGTH_LONG).show();
} } else {
else{
Toast.makeText(FloatingAction.this, "Something goes wrong!", Toast.LENGTH_LONG).show(); Toast.makeText(FloatingAction.this, "Something goes wrong!", Toast.LENGTH_LONG).show();
} }
//reloadData();
} }
}
}); });
} }
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);
}
}
} }

View File

@ -15,9 +15,9 @@ import android.widget.AdapterView;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
import android.widget.Button; import android.widget.Button;
import android.widget.CheckedTextView; import android.widget.CheckedTextView;
import android.widget.FrameLayout;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast; import android.widget.Toast;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Objects; import java.util.Objects;
@ -36,6 +36,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
Button editItem; Button editItem;
FloatingActionButton fab; FloatingActionButton fab;
String listProductID; String listProductID;
String chosenListID;
@ -54,14 +55,18 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
listItem = new ArrayList<>(); listItem = new ArrayList<>();
listNameView = findViewById(R.id.listView); listNameView = findViewById(R.id.listView);
listProductView = findViewById(R.id.productsListView); listProductView = findViewById(R.id.productsListView);
ctvProduct = findViewById(R.id.productCheck);
productItem = new ArrayList<>(); productItem = new ArrayList<>();
viewData();
viewTableName(); viewTableName();
fab = (FloatingActionButton) findViewById(R.id.fab); fab = findViewById(R.id.fab);
listProductView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
fab.setOnClickListener(new View.OnClickListener() { fab.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { 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,12 +74,22 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listProductID = parent.getItemAtPosition(position).toString(); 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(listProductView);
//registerForContextMenu(listNameView); //registerForContextMenu(listNameView);
@ -104,7 +119,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public boolean onContextItemSelected(MenuItem item) { public boolean onContextItemSelected(MenuItem item) {
switch(item.getItemId()) { switch(item.getItemId()) {
case R.id.deleteItem: case R.id.deleteItem:
Integer result = myDb.deleteData(listProductID); Integer result = myDb.deleteData(chosenListID, listProductID);
System.out.println(listProductID); System.out.println(listProductID);
if(result > 0){ if(result > 0){
reloadData(); reloadData();
@ -117,7 +132,21 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
return true; return true;
case R.id.editItem: 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; return true;
default: default:
@ -138,6 +167,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){ switch (menuItem.getItemId()){
case R.id.AddList: case R.id.AddList:
((FrameLayout)findViewById(R.id.content_frame)).removeAllViews();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,
new MenuFragment()).commit(); new MenuFragment()).commit();
break; break;
@ -146,12 +176,13 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
return true; return true;
} }
public void viewData(){ public void viewCheckData(String tableName){
Cursor res = myDb.getAlldata(); Cursor res = myDb.getChecekData(tableName);
if(res.getCount() == 0){ adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem);
Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show(); listProductView.setAdapter(adapter2);
if(adapter2.getCount() > 0){
adapter2.clear();
} }
else {
while (res.moveToNext()) { while (res.moveToNext()) {
productItem.add(res.getString(1)); productItem.add(res.getString(1));
@ -159,9 +190,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem); adapter2 = new ArrayAdapter<>(this, R.layout.product_view, productItem);
listProductView.setAdapter(adapter2); listProductView.setAdapter(adapter2);
} }
}
public void reloadData(){ public void reloadData(){
Cursor res = myDb.getAlldata(); Cursor res = myDb.getChecekData(chosenListID);
adapter2.clear(); adapter2.clear();
if(res.getCount() == 0){ if(res.getCount() == 0){
Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show(); Toast.makeText(this, "No data to show", Toast.LENGTH_SHORT).show();
@ -181,10 +211,24 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
if (res.moveToFirst()) { if (res.moveToFirst()) {
while ( !res.isAfterLast() ) { while ( !res.isAfterLast() ) {
listItem.add(res.getString(res.getColumnIndex("name"))); listItem.add(res.getString(res.getColumnIndex("name")));
System.out.println(res.getString( res.getColumnIndex("name")));
res.moveToNext(); res.moveToNext();
} }
} }
adapter = new ArrayAdapter<>(this, R.layout.list_view, listItem); adapter = new ArrayAdapter<>(this, R.layout.list_view, listItem);
listNameView.setAdapter(adapter); 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")) );
res.moveToNext();
}
}
adapter = new ArrayAdapter<>(this, R.layout.list_view, listItem);
listNameView.setAdapter(adapter);
}
} }

View File

@ -10,14 +10,14 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.Toast;
import java.util.Objects;
public class MenuFragment extends Fragment{ public class MenuFragment extends Fragment{
private Button addListBtn; private Button addListBtn;
EditText listNameText; EditText listNameText;
DatabaseHelper myDb; DatabaseHelper myDb;
//Context context;
@Override @Override
@ -26,23 +26,16 @@ public class MenuFragment extends Fragment {
addListBtn = view.findViewById(R.id.addListBtn); addListBtn = view.findViewById(R.id.addListBtn);
listNameText = view.findViewById(R.id.listNameText); listNameText = view.findViewById(R.id.listNameText);
myDb = new DatabaseHelper(getActivity()); myDb = new DatabaseHelper(getActivity());
AddData(); AddTable();
return view; return view;
} }
public void AddTable(){
public void AddData(){
addListBtn.setOnClickListener(new View.OnClickListener() { addListBtn.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
boolean isInserted = myDb.insertData(listNameText.getText().toString()); myDb.userCreateTable(listNameText.getText().toString());
if(isInserted){ ((MainActivity)Objects.requireNonNull(getActivity())).reloadTableName();
Toast.makeText(getActivity(), "Done", Toast.LENGTH_LONG).show();
}
else{
Toast.makeText(getActivity(), "Something goes wrong!", Toast.LENGTH_LONG).show();
}
} }
}); });
} }

View File

@ -3,7 +3,8 @@
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_height="match_parent"
android:orientation="vertical"> android:orientation="vertical"
android:id="@+id/popup_dailog">
<EditText <EditText
android:id="@+id/listNameText" android:id="@+id/listNameText"

View File

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

View File

@ -3,7 +3,7 @@
xmlns:app="http://schemas.android.com/apk/res-auto"> xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/AddList" <item android:id="@+id/AddList"
android:title="@string/AddButton" /> android:title="@string/AddListButton" />
<item android:id="@+id/Reminder" <item android:id="@+id/Reminder"
android:title="@string/Reminder"/> android:title="@string/Reminder"/>
<item android:id="@+id/ProductList" <item android:id="@+id/ProductList"

View File

@ -4,6 +4,7 @@
<string name="EmailText">example@example.com</string> <string name="EmailText">example@example.com</string>
<string name="ProductList">Twoje Listy</string> <string name="ProductList">Twoje Listy</string>
<string name="AddButton">Dodaj</string> <string name="AddButton">Dodaj</string>
<string name="AddListButton">Nowa lista</string>
<string name="Open">Open</string> <string name="Open">Open</string>
<string name="Close">Close</string> <string name="Close">Close</string>
<string name="Settings">Ustawienia</string> <string name="Settings">Ustawienia</string>