title: ListView not refreshing contents from database
body: <p>I'm trying to make a simple list with the ability to add new items to it, delete and modify them. The contents of the list should be saved in SQLite. My problem is the following: When I add a new item, the list isn't refreshing, and the item appears only after I exited the app, and open it again.I found a workaround, but it's hacky and doesn't entirely work.</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
      SaveClass save = new SaveClass(this);
      private ListView mShoppingList;
      private EditText mItemEdit;
      private Button mAddButton;
      private ArrayAdapterElem mAdapter;
      List&lt;ItemList&gt; elemek;
  
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          elemek = save.getAllItems();
  
          mAdapter = new ArrayAdapterElem(this, elemek);
          mShoppingList = findViewById(R.id.shopping_listView);
          mItemEdit = (EditText) findViewById(R.id.item_editText);
          mAddButton = (Button) findViewById(R.id.add_button);
          mShoppingList.setAdapter(mAdapter);
  
  
          mAddButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  ItemList item = new ItemList(mItemEdit.getText().toString());
                  mItemEdit.setText("");
                  save.addElem(item);
                  mAdapter.add(item);
                  mAdapter.notifyDataSetChanged();
              }
          });
  </code></pre>
  
  <p>And this is my custom ArrayAdapter class:</p>
  
  <pre><code>public class ArrayAdapterElem extends ArrayAdapter&lt;ItemList&gt; {
  
      private int elem;
  
      public ArrayAdapterElem(Context context, List&lt;ItemList&gt; elemek) {
          super(context, 0, elemek);
      }
  
      @Override
      public View getView(int position, View convertView, ViewGroup parent) {
          ItemList elem = getItem(position);
          if (convertView == null) {
              convertView = LayoutInflater.from(getContext()).inflate(R.layout.lista_elem, parent, false);
          }
          TextView elemnev = (TextView) convertView.findViewById(R.id.Elem);
  
          if (elem.checked == true) {
              elemnev = (TextView) convertView.findViewById(R.id.Elem);
              elemnev.setTextColor(Color.GRAY);
              elemnev.setPaintFlags(elemnev.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
          }
          else
          {
              elemnev.setTextColor(Color.BLACK);
              elemnev.setPaintFlags(elemnev.getPaintFlags() &amp; (~ Paint.STRIKE_THRU_TEXT_FLAG));
          }
  
          elemnev.setText(elem.listaElem);
  
          return convertView;
      }
  
  }
  
  </code></pre>
  
  <p>Right now I save the item in SQLite and add the item to ListView manually. I know it means the new object in the database and the object in the list are not the same, hence my modifications doesn't work on the saved item, until I reload the app to get the object from the database. So I'd like to find a better solution for this.
  If I just simply save the item in the database, and call notifyDataSetChanged() method on the adapter, it doesn't refresh items in the list from the database. Should I implement notifyDataSetChanged in my custom ArrayAdapter class, or is there some much simpler solution I fail to see?</p>
  
  <p>Thank you for any help in adavance.</p>