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

View File

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

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;
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());
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{
} else {
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.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,12 +74,22 @@ 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);
@ -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,6 +167,7 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.AddList:
((FrameLayout)findViewById(R.id.content_frame)).removeAllViews();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame,
new MenuFragment()).commit();
break;
@ -146,12 +176,13 @@ 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));
@ -159,9 +190,8 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
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();
@ -181,10 +211,24 @@ public class MainActivity extends AppCompatActivity implements NavigationView.On
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")) );
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.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Objects;
public class MenuFragment extends Fragment{
private Button addListBtn;
EditText listNameText;
DatabaseHelper myDb;
//Context context;
@Override
@ -26,23 +26,16 @@ 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(){
public void AddTable(){
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();
}
myDb.userCreateTable(listNameText.getText().toString());
((MainActivity)Objects.requireNonNull(getActivity())).reloadTableName();
}
});
}

View File

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

View File

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

View File

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

View File

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