How to update the ListView after adding new items?
<p>I am working on a grocery list app and having trouble refreshing the list view after adding a new item. After I add a new item in the database the ListView is not refreshed. If I go in the item details page and then come back to main activity the onCreate is called again and it refreshes it correctly.</p>

<p>If I call the refresh method in the addItemToDb() (on button clicked) method it duplicates my items but does not add them to the database.
Has anyone had this problem before???
Here is the code:</p>

<p>The list view adapter</p>

<pre><code>    public class ItemListViewAdapter extends ArrayAdapter&lt;ItemModel&gt; {
    Activity activity;
    int layoutResource;
    ArrayList&lt;ItemModel&gt; itemModelArrayList = new ArrayList&lt;&gt;();

    public ItemListViewAdapter(Activity act, int resource, ArrayList&lt;ItemModel&gt; data) {
        super(act, resource, data);

        activity = act;
        layoutResource = resource;
        itemModelArrayList = data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return itemModelArrayList.size();
    }

    @Override
    public ItemModel getItem(int position) {
        return itemModelArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return super.getItemId(position);
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        View row = convertView;
        final ViewHolder holder;

        if (row == null || (row.getTag()) == null) {

            LayoutInflater inflater = LayoutInflater.from(activity);
            row = inflater.inflate(layoutResource, null);

            holder = new ViewHolder();

            holder.hItemName = (TextView) row.findViewById(R.id.custom_row_productName);
            holder.hItemPrice = (TextView) row.findViewById(R.id.custom_row_productPrice);
            holder.hItemType = (TextView) row.findViewById(R.id.custom_row_productType);
            holder.hCheckBox = (CheckBox) row.findViewById(R.id.custom_row_checkBox);
            holder.hItemEdit = (ImageView) row.findViewById(R.id.custom_row_edit);

            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }

        holder.hModel = getItem(position);

        holder.hItemName.setText(holder.hModel.getItemName());
        holder.hItemPrice.setText(String.valueOf(holder.hModel.getItemPrice()));
        holder.hItemType.setText(holder.hModel.getItemType());

        holder.hItemEdit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int itemID = holder.hModel.getItemId();
                String itemName = holder.hModel.getItemName();
                String itemPrice = String.valueOf(holder.hModel.getItemPrice());
                String itemType = holder.hModel.getItemType();
                String itemDate = holder.hModel.getItemDate();

                Intent intent = new Intent(activity, ItemDetail.class);

                intent.putExtra("id", itemID);
                intent.putExtra("product", itemName);
                intent.putExtra("price", itemPrice);
                intent.putExtra("type", itemType);
                intent.putExtra("date", itemDate);

                startActivity(activity, intent, null);
            }
        });

        return row;
    }

    public class ViewHolder {
        ItemModel hModel;
        TextView hItemName;
        TextView hItemPrice;
        TextView hItemType;
        TextView hItemDate;
        CheckBox hCheckBox;
        ImageView hItemEdit;
    }
}
</code></pre>

<p>And main activity</p>

<pre><code> @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    dbHandler = new DatabaseHandler(getApplicationContext());

    itemNameText = (EditText) findViewById(R.id.activity_main_productName);
    itemPriceText = (EditText) findViewById(R.id.activity_main_productPrice);
    itemTypeSpinner = (Spinner) findViewById(R.id.activity_main_spinner);
    addButton = (Button) findViewById(R.id.activity_main_addButton);
    saveListButton = (FloatingActionButton) findViewById(R.id.activity_main_fab);


    ArrayAdapter&lt;CharSequence&gt; spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.productTypes, R.layout.spinner_item);
    itemTypeSpinner.setAdapter(spinnerAdapter);


    //USE BUTTONS
    addButton.setOnClickListener(this);
    saveListButton.setOnClickListener(this);

    //LIST_VIEW
    listView = (ListView) findViewById(R.id.activity_main_listView);

    //calling methods
    refreshData();
}

    public void refreshData() {
    modelArrayListContainer.clear();

    //GET ITEMS FROM DB
    ArrayList&lt;ItemModel&gt; modelArrayListFromDB = dbHandler.getAllItems();

    for (int i = 0; i &lt; modelArrayListFromDB.size(); i++) {

        int ditemID = modelArrayListFromDB.get(i).getItemId();
        String dItemName = modelArrayListFromDB.get(i).getItemName();
        double dItemPrice = modelArrayListFromDB.get(i).getItemPrice();
        String dItemType = modelArrayListFromDB.get(i).getItemType();
        String dItemDate = modelArrayListFromDB.get(i).getItemDate();

        ItemModel newModel = new ItemModel();

        newModel.setItemId(ditemID);
        newModel.setItemName(dItemName);
        newModel.setItemPrice((int) dItemPrice);
        newModel.setItemType(dItemType);
        newModel.setItemDate(dItemDate);

        modelArrayListContainer.add(newModel);
    }

    //setup Adapter
    itemListViewAdapter = new ItemListViewAdapter(MainActivity.this, R.layout.custom_product_layout_activity_main, modelArrayListContainer);
    listView.setAdapter(itemListViewAdapter);
    itemListViewAdapter.notifyDataSetChanged();
}

        public void addItemToDb() {
        ItemModel model = new ItemModel();

        String spinnerValue = itemTypeSpinner.getSelectedItem().toString();
            model.setItemName(itemNameText.getText().toString().trim());      model.setItemPrice(Double.parseDouble((itemPriceText.getText().toString().trim())));
model.setItemType(spinnerValue);

            dbHandler.addItem(model);
            dbHandler.close();

            Log.v(TAG, "::addItemToDb - itemAdded");

        }
    }
</code></pre>