0

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);


            }
        });

    }
}
1
  • can you show the log when app force closes? Commented Aug 27, 2020 at 16:06

2 Answers 2

2

The problem was I was not notifying the list notifyDataSetChanged() that the item I selected had changed here is the code which now populates the list.

// setOnItemClickListener
        orderList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            Order order;
            @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();
                orderItemList.add((Item) adapterView.getAdapter().getItem(pos));
                itemArrayAdaptor.notifyDataSetChanged();
                Toast.makeText(OrderActivity.this, orderItemList.toString(), Toast.LENGTH_SHORT).show();

            }
        });
Sign up to request clarification or add additional context in comments.

Comments

0
orderList.setOnItemClickListener(
   new AdapterView.OnItemClickListener() { 
          @Override public void onItemClick(AdapterView<?> adapterView, View 
             view, int pos, long id) { 
                Toast.makeText(OrderActivity.this, "Added" 
                   ,Toast.LENGTH_SHORT).show();
                   itemList.add(adapterView.getItemAtPosition(pos)); 
              }
           });

2 Comments

Thanks but this did not resolve the problem, I have edited the code and I am now able to get the selected item. But when trying to add this to the itemList the program crashes. I have edited the question with the new code and the error message
Hi, @Dave can you please provide an Error log? so that I could have a better overview of the error!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.