I am trying to create a waiter app which allows the waiter to take orders.
I am trying to achieve this using a ListView and setOnItemClickListener.
My idea is that every time a Item is clicked on the ListView it will be added to a ArrayList<Item>. Which later I can pass this List to a method created in DataBaseHelper which then iterates through the List and adds each Item from the order to the SQLite database.
I have not created the method in the DataBaseHelper class yet but I would like to know how to populate the ArrayList<Item> first and then move on to the next step.
The table I will be adding the list to has a manny to many relationship and is called order_item. Which also brings a question to mind as I do not need the full details about the Item and only the IDto save in the table do I need to get all the information of the Item?
Right now what is happening is the app allows me to click and add one Item and then the apps is forced to close.
I am also not sure if I should make the ArrayList a global variable or have it in the Order Object as a variable?
The log says:
java.lang.IllegalStateException: The content of the adapter has changed but ListView did not receive a notification. Make sure the content of your adapter is not modified from a background thread, but only from the UI thread. Make sure your adapter calls notifyDataSetChanged() when its content changes. [in ListView(2131165292, class android.widget.ListView) with Adapter(class android.widget.ArrayAdapter)]
OrderActivity
public class OrderActivity extends AppCompatActivity {
EditText waiter_id;
Button order;
ListView orderList;
List<Item> itemList = new ArrayList<Item>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_order);
waiter_id = (EditText) findViewById(R.id.waiter_id);
order = (Button) findViewById(R.id.order_button);
orderList = (ListView) findViewById(R.id.item_list);
//Showing Items on the ListView
DataBaseHelper dbHelp = new DataBaseHelper(OrderActivity.this);
final List<Item> itemList = dbHelp.getMenu();
ArrayAdapter itemArrayAdaptor = new ArrayAdapter<Item>(OrderActivity.this, android.R.layout.simple_list_item_1, itemList);
orderList.setAdapter(itemArrayAdaptor);
// setOnItemClickListener
orderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
Item item;
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int pos, long id) {
//Selecting the Item and storing it in a reference variable to add to itemList
Toast.makeText(OrderActivity.this, "Order" + pos , Toast.LENGTH_SHORT).show();
Item item = (Item) adapterView.getAdapter().getItem(pos);
Toast.makeText(OrderActivity.this, item.toString() , Toast.LENGTH_SHORT).show();
itemList.add(item);
}
});
}
}