I have created a Shopping list app using an Expandable ListView. The Parent has a checkbox which isn't holding its check when scrolling or leaving the list and returning to it. I have researched this for days and cannot get it to work. "Help us Obiwan, you're our only hope" package palmer.markj.markpalmer.myshoppinglist; package palmer.markj.markpalmer.myshoppinglist;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import java.util.List;
import static android.support.v4.content.ContextCompat.startActivity;
/**
* Created by Mark Palmer on 18/11/2017.
*/
public class ExpandableAdapter extends BaseExpandableListAdapter
{
Context context;
List<String> heading_items;
List<String> size;
List<String> description;
List<Double> quantity;
ExpandableAdapter(Context context, List<String> heading_items,
List<String> size, List<String> description, List<Double> quantity)
{
this.context = context;
this.heading_items = heading_items;
this.size = size;
this.description = description;
this.quantity = quantity;
}
@Override
public int getGroupCount()
{
return heading_items.size();
}
@Override
public int getChildrenCount(int i)
{
return 1;
}
@Override
public Object getGroup(int i)
{
return heading_items.get(i);
}
@Override
public Object getChild(int i, int i1)
{
return null;
}
@Override
public long getGroupId(int i)
{
return i;
}
@Override
public long getChildId(int i, int i1)
{
return i1;
}
@Override
public boolean hasStableIds()
{
return false;
}
@Override
public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup)
{
RecyclerView.ViewHolder viewHolder = null;
final String item = (String)this.getGroup(i);
if (view == null) {
LayoutInflater layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.parent_layout, null);
}
final TextView itemTV = view.findViewById(R.id.parent_shopping_item);
itemTV.setText(item);
final CheckBox checkBox = view.findViewById(R.id.checkBox);
Button deleteItemBtn = view.findViewById(R.id.button);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked)
{
ShoppingItem shoppingItem = new ShoppingItem();
itemTV.setPaintFlags(itemTV.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
if (! isChecked)
{
itemTV.setPaintFlags(itemTV.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
}
}
});
deleteItemBtn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
DBManager dbManager = new DBManager(context, null, null, 1);
dbManager.deleteItem(item);
Intent intent = new Intent(context, MainActivity.class);
Bundle bundle = new Bundle();
startActivity(context, intent, bundle);
}
});
return view;
}
@Override
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup)
{
String sizeofItem = this.size.get(i);
String descriptionOfItem = this.description.get(i);
Double quantity = this.quantity.get(i);
String qtyText = String.valueOf(quantity);
if (view == null)
{
LayoutInflater layoutInflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.child_layout, null);
}
TextView sizeTV = view.findViewById(R.id.text_size);
sizeTV.setText(sizeofItem);
TextView descriptionTV = view.findViewById(R.id.text_description);
descriptionTV.setText(descriptionOfItem);
TextView quantityTV = view.findViewById(R.id.text_qty);
quantityTV.setText(qtyText);
return view;
}
@Override
public boolean isChildSelectable(int i, int i1)
{
return true;
}
}
package palmer.markj.markpalmer.myshoppinglist;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import java.util.ArrayList;
public class AddItem extends AppCompatActivity
{
String itemName;
String itemSize;
String itemDescription;
String qtyText;
double itemQty;
ShoppingItem shoppingItem;
EditText addItemET;
EditText addSizeET;
EditText addDescET;
EditText addQtyET;
boolean itemOK = false;
boolean qtyOK = false;
boolean delete = false;
// Set up the arrays
ArrayList<ShoppingItem> items;
DBManager dbManager = new DBManager(this, null, null, 1);
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
addItemET = (EditText)findViewById(R.id.edittext_item);
addSizeET = (EditText)findViewById(R.id.editText_size);
addDescET = (EditText)findViewById(R.id.editText_description);
addQtyET = (EditText)findViewById(R.id.editText_quantity);
}
@Override
protected void onPostResume()
{
super.onPostResume();
addItemET.setText(null);
addSizeET.setText(null);
addDescET.setText(null);
addQtyET.setText(null);
addItemET.requestFocus();
}
public void addItem(View view)
{
itemName = addItemET.getText().toString();
if(itemName.equals(""))
{
Toast.makeText(this, "Please add an Item", Toast.LENGTH_SHORT).show();
addItemET.requestFocus();
return;
}
else
{
itemOK = true;
}
itemSize = addSizeET.getText().toString();
itemDescription = addDescET.getText().toString();
qtyText = addQtyET.getText().toString();
if(qtyText.equals(""))
{
Toast.makeText(this, "Please add a Quantity", Toast.LENGTH_SHORT).show();
addQtyET.requestFocus();
return;
}
else
{
itemQty = Double.parseDouble(addQtyET.getText().toString());
qtyOK = true;
}
if(itemOK && qtyOK)
{
shoppingItem = new ShoppingItem(itemName, itemSize, itemDescription, itemQty);
if(dbManager.itemIsOnTheList(shoppingItem))
{
Toast.makeText(this, "Item " + itemName + " is already on the list", Toast.LENGTH_SHORT).show();
}
else
{
dbManager.addItem(shoppingItem);
Toast.makeText(this, "Item " + itemName + " has been added to list", Toast.LENGTH_SHORT).show();
}
addItemET.setText(null);
addSizeET.setText(null);
addDescET.setText(null);
addQtyET.setText(null);
addItemET.requestFocus();
}
}
public void viewListClick(View view)
{
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void deleteList(View view)
{
if (delete)
{
dbManager.deleteAllItems();
Toast.makeText(this, "Shopping List has been Deleted", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(this, "Press Again to Delete List", Toast.LENGTH_LONG).show();
delete = true;
new Handler().postDelayed(new Runnable()
{
@Override
public void run()
{
delete = false;
}
}, 3500);
}
}
}
package palmer.markj.markpalmer.myshoppinglist;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
DBManager dbmanager = new DBManager(this, null, null,1);
List<ShoppingItem> items;
ExpandableListView expandableListView;
// Set up the arrays
List<String> itemName = new ArrayList<>();
List<String> size= new ArrayList<>();
List<String> description= new ArrayList<>();
List<Double> qty = new ArrayList<>();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
items = dbmanager.getAllShoppingItems();
for (i = 0; i < items.size(); i++)
{
itemName.add(items.get(i).getItem());
size.add(items.get(i).getSize());
description.add(items.get(i).getDescription());
qty.add(items.get(i).getQuantity());
}
expandableListView = (ExpandableListView)findViewById(R.id.shopping_list);
ExpandableAdapter expandableAdapter = new ExpandableAdapter(this, itemName, size, description, qty);
expandableListView.setAdapter(expandableAdapter);
}
public void onCheckboxClicked(View view)
{
}
@Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(getApplicationContext(),AddItem.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id)
{
case palmer.markj.markpalmer.myshoppinglist.R.id.menu_add_item:
startActivity(new Intent(getApplicationContext(),AddItem.class));
return true;
case palmer.markj.markpalmer.myshoppinglist.R.id.menu_refresh:
startActivity(new Intent(getApplicationContext(),MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
package palmer.markj.markpalmer.myshoppinglist;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ExpandableListView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity
{
DBManager dbmanager = new DBManager(this, null, null,1);
List<ShoppingItem> items;
ExpandableListView expandableListView;
// Set up the arrays
List<String> itemName = new ArrayList<>();
List<String> size= new ArrayList<>();
List<String> description= new ArrayList<>();
List<Double> qty = new ArrayList<>();
int i = 0;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
items = dbmanager.getAllShoppingItems();
for (i = 0; i < items.size(); i++)
{
itemName.add(items.get(i).getItem());
size.add(items.get(i).getSize());
description.add(items.get(i).getDescription());
qty.add(items.get(i).getQuantity());
}
expandableListView = (ExpandableListView)findViewById(R.id.shopping_list);
ExpandableAdapter expandableAdapter = new ExpandableAdapter(this, itemName, size, description, qty);
expandableListView.setAdapter(expandableAdapter);
}
public void onCheckboxClicked(View view)
{
}
@Override
public void onBackPressed()
{
super.onBackPressed();
startActivity(new Intent(getApplicationContext(),AddItem.class));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id)
{
case palmer.markj.markpalmer.myshoppinglist.R.id.menu_add_item:
startActivity(new Intent(getApplicationContext(),AddItem.class));
return true;
case palmer.markj.markpalmer.myshoppinglist.R.id.menu_refresh:
startActivity(new Intent(getApplicationContext(),MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
package palmer.markj.markpalmer.myshoppinglist;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Mark Palmer on 19/11/2017.
*/
public class DBManager extends SQLiteOpenHelper
{
// Data fields
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "shopping.db";
public static final String TABLE_SHOPPING_ITEM = "shoppingItem";
public static final String COLUMN_SHOPPING_ITEM = "_item";
public static final String COLUMN_SIZE = "_size";
public static final String COLUMN_DESCRIPTION = "_description";
public static final String COLUMN_ITEM_IS_CHECKED = "_isChecked";
public static final String COLUMN_QUANTITY = "_qty";
public DBManager(Context context, String name, SQLiteDatabase.CursorFactory factory, int version)
{
super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}
@Override
public void onOpen(SQLiteDatabase db)
{
super.onOpen(db);
if (!db.isReadOnly())
{
// Enable foreign key constraints
db.execSQL("PRAGMA foreign_keys='ON';");
}
}
@Override
public void onCreate(SQLiteDatabase dbase)
{
// Create medication table
String createShoppingTable = "CREATE TABLE IF NOT EXISTS " + TABLE_SHOPPING_ITEM + "(" +
COLUMN_SHOPPING_ITEM + " TEXT PRIMARY KEY, " +
COLUMN_SIZE + " TEXT, " +
COLUMN_DESCRIPTION + " TEXT, " +
COLUMN_ITEM_IS_CHECKED + " INTEGER DEFAULT 0, " +
COLUMN_QUANTITY + " REAL" +
");";
dbase.execSQL(createShoppingTable);
}
@Override
public void onUpgrade(SQLiteDatabase dbase, int i, int i1)
{
}
public void addItem(ShoppingItem shoppingItem)
{
ContentValues values = new ContentValues();
SQLiteDatabase dbase = getWritableDatabase();
values.put(COLUMN_SHOPPING_ITEM, shoppingItem.getItem());
values.put(COLUMN_SIZE, shoppingItem.getSize());
values.put(COLUMN_DESCRIPTION, shoppingItem.getDescription());
values.put(COLUMN_QUANTITY, shoppingItem.getQuantity());
dbase.insert(TABLE_SHOPPING_ITEM, null, values);
dbase.close();
}
public boolean itemIsChecked(ShoppingItem item)
{
boolean isChecked = false;
if (item.getIsChecked() == 1)
{
isChecked = true;
}
return isChecked;
}
public List<ShoppingItem> getAllShoppingItems()
{
List<ShoppingItem> shoppingList = new ArrayList<>();
ShoppingItem shoppingItem;
String query = "SELECT * FROM " + TABLE_SHOPPING_ITEM + " WHERE 1";
SQLiteDatabase dbase = getReadableDatabase();
Cursor cursor = dbase.rawQuery(query, null);
// Loop thru all items
cursor.moveToFirst();
while (!cursor.isAfterLast())
{
shoppingItem = new ShoppingItem();
if(cursor.getString(cursor.getColumnIndex("_item")) != null)
{
shoppingItem.setItem(cursor.getString(cursor.getColumnIndex(COLUMN_SHOPPING_ITEM)));
shoppingItem.setSize(cursor.getString(cursor.getColumnIndex(COLUMN_SIZE)));
shoppingItem.setDescription(cursor.getString(cursor.getColumnIndex(COLUMN_DESCRIPTION)));
shoppingItem.setQuantity(cursor.getDouble(cursor.getColumnIndex(COLUMN_QUANTITY)));
// Add the object to the list
shoppingList.add(shoppingItem);
}
cursor.moveToNext();
}
cursor.close();
dbase.close();
return shoppingList;
}
public boolean itemIsOnTheList(ShoppingItem shoppingItem)
{
String item = shoppingItem.getItem();
boolean itemExists = false;
List<String> existingItems = new ArrayList<>();
SQLiteDatabase dbase = getReadableDatabase();
String query = "SELECT COUNT (*) FROM " + TABLE_SHOPPING_ITEM + " WHERE " + COLUMN_SHOPPING_ITEM + "=\"" + item + "\""; ;
Cursor cursor = dbase.rawQuery(query, null);
cursor.moveToFirst();
int count = cursor.getInt(0);
if (count > 0)
{
itemExists = true;
}
return itemExists;
}
public void deleteItem(String itemName)
{
SQLiteDatabase dbase = getWritableDatabase();
dbase.execSQL("DELETE FROM " + TABLE_SHOPPING_ITEM + " WHERE " + COLUMN_SHOPPING_ITEM +
"=\"" + itemName + "\";");
}
public void deleteAllItems()
{
SQLiteDatabase dbase = getWritableDatabase();
dbase.execSQL("DELETE FROM " + TABLE_SHOPPING_ITEM + " WHERE 1");
}
}
Aucun commentaire:
Enregistrer un commentaire