Android Studio - New Item in List is not shown












0















In my App i have a ListView, if i add some new Items, the ListView does not shown the Items. The items will be shown if i close the app and go back in it.



So my new Items are saved in my Database, but the list will not be synchronized.
i followed a Tutorial on youtube, if he tries it, it works fine, but not in my app. i hope someone can help me to find my problem.



In my newItem_Activity i have an Add-Button with following Code:



btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(todo.getName() == null){
Toast.makeText(ToDoCreateNew.this, "Please insert some value.", Toast.LENGTH_LONG).show();
return;
}
ToDoDatabaseHelper.getInstance(ToDoCreateNew.this).createTodo(todo);
finish();
}
});


My Database looks like this:



public ToDo createTodo(final ToDo todo) {
SQLiteDatabase database = this.getWritableDatabase();

ContentValues contentValues = new ContentValues();
contentValues.put(NAME_COLUMN, todo.getName());
contentValues.put(DUEDATE_COLUMN, todo.getDueDate() == null ? null : todo.getDueDate().getTimeInMillis() / 1000);
contentValues.put(FAVORITE_COLUMN, todo.isFavorite() ? 1 : 0);
contentValues.put(DESCRIPTION_COLUMN, todo.getDescription());
contentValues.put(DUETIME_COLUMN, String.valueOf(todo.getDueTime() == null ? null : todo.getDueTime().getTime()));

long newID = database.insert(TABLE_NAME, null, contentValues);
database.close();
return readToDo(newID);
}

public List<ToDo> readAllToDos(){
List<ToDo> todos = new ArrayList<>();
SQLiteDatabase database = this.getReadableDatabase();

Cursor c = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
if (c.moveToFirst()){
do {
ToDo todo = readToDo(c.getLong(c.getColumnIndex(ID_COLUMN)));
if (todo != null){
todos.add(todo);
}
} while (c.moveToNext());

}
database.close();
return todos;
}


and this Code is on my Activity with my ListView:



List<ToDo> dataSource;
ToDoOverviewListAdapter adapter;

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_to_do_overview);

lv = (ListView) findViewById(R.id.listToDo);
dataSource = ToDoDatabaseHelper.getInstance(this).readAllToDos();

adapter = new ToDoOverviewListAdapter(this, dataSource);
lv.setAdapter(new ToDoOverviewListAdapter(this, dataSource));
}

// Go to Activity to Add new Item - Add Button is in newItem_Activity
public void createToDo(){
startActivity(new Intent(ToDoOverview.this, ToDoCreateNew.class));
refreshListView();
}

private void refreshListView(){
dataSource.clear();
dataSource.addAll(ToDoDatabaseHelper.getInstance(this).readAllToDos());
adapter.notifyDataSetChanged();
}









share|improve this question



























    0















    In my App i have a ListView, if i add some new Items, the ListView does not shown the Items. The items will be shown if i close the app and go back in it.



    So my new Items are saved in my Database, but the list will not be synchronized.
    i followed a Tutorial on youtube, if he tries it, it works fine, but not in my app. i hope someone can help me to find my problem.



    In my newItem_Activity i have an Add-Button with following Code:



    btnAdd.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    if(todo.getName() == null){
    Toast.makeText(ToDoCreateNew.this, "Please insert some value.", Toast.LENGTH_LONG).show();
    return;
    }
    ToDoDatabaseHelper.getInstance(ToDoCreateNew.this).createTodo(todo);
    finish();
    }
    });


    My Database looks like this:



    public ToDo createTodo(final ToDo todo) {
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues contentValues = new ContentValues();
    contentValues.put(NAME_COLUMN, todo.getName());
    contentValues.put(DUEDATE_COLUMN, todo.getDueDate() == null ? null : todo.getDueDate().getTimeInMillis() / 1000);
    contentValues.put(FAVORITE_COLUMN, todo.isFavorite() ? 1 : 0);
    contentValues.put(DESCRIPTION_COLUMN, todo.getDescription());
    contentValues.put(DUETIME_COLUMN, String.valueOf(todo.getDueTime() == null ? null : todo.getDueTime().getTime()));

    long newID = database.insert(TABLE_NAME, null, contentValues);
    database.close();
    return readToDo(newID);
    }

    public List<ToDo> readAllToDos(){
    List<ToDo> todos = new ArrayList<>();
    SQLiteDatabase database = this.getReadableDatabase();

    Cursor c = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
    if (c.moveToFirst()){
    do {
    ToDo todo = readToDo(c.getLong(c.getColumnIndex(ID_COLUMN)));
    if (todo != null){
    todos.add(todo);
    }
    } while (c.moveToNext());

    }
    database.close();
    return todos;
    }


    and this Code is on my Activity with my ListView:



    List<ToDo> dataSource;
    ToDoOverviewListAdapter adapter;

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_to_do_overview);

    lv = (ListView) findViewById(R.id.listToDo);
    dataSource = ToDoDatabaseHelper.getInstance(this).readAllToDos();

    adapter = new ToDoOverviewListAdapter(this, dataSource);
    lv.setAdapter(new ToDoOverviewListAdapter(this, dataSource));
    }

    // Go to Activity to Add new Item - Add Button is in newItem_Activity
    public void createToDo(){
    startActivity(new Intent(ToDoOverview.this, ToDoCreateNew.class));
    refreshListView();
    }

    private void refreshListView(){
    dataSource.clear();
    dataSource.addAll(ToDoDatabaseHelper.getInstance(this).readAllToDos());
    adapter.notifyDataSetChanged();
    }









    share|improve this question

























      0












      0








      0








      In my App i have a ListView, if i add some new Items, the ListView does not shown the Items. The items will be shown if i close the app and go back in it.



      So my new Items are saved in my Database, but the list will not be synchronized.
      i followed a Tutorial on youtube, if he tries it, it works fine, but not in my app. i hope someone can help me to find my problem.



      In my newItem_Activity i have an Add-Button with following Code:



      btnAdd.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      if(todo.getName() == null){
      Toast.makeText(ToDoCreateNew.this, "Please insert some value.", Toast.LENGTH_LONG).show();
      return;
      }
      ToDoDatabaseHelper.getInstance(ToDoCreateNew.this).createTodo(todo);
      finish();
      }
      });


      My Database looks like this:



      public ToDo createTodo(final ToDo todo) {
      SQLiteDatabase database = this.getWritableDatabase();

      ContentValues contentValues = new ContentValues();
      contentValues.put(NAME_COLUMN, todo.getName());
      contentValues.put(DUEDATE_COLUMN, todo.getDueDate() == null ? null : todo.getDueDate().getTimeInMillis() / 1000);
      contentValues.put(FAVORITE_COLUMN, todo.isFavorite() ? 1 : 0);
      contentValues.put(DESCRIPTION_COLUMN, todo.getDescription());
      contentValues.put(DUETIME_COLUMN, String.valueOf(todo.getDueTime() == null ? null : todo.getDueTime().getTime()));

      long newID = database.insert(TABLE_NAME, null, contentValues);
      database.close();
      return readToDo(newID);
      }

      public List<ToDo> readAllToDos(){
      List<ToDo> todos = new ArrayList<>();
      SQLiteDatabase database = this.getReadableDatabase();

      Cursor c = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
      if (c.moveToFirst()){
      do {
      ToDo todo = readToDo(c.getLong(c.getColumnIndex(ID_COLUMN)));
      if (todo != null){
      todos.add(todo);
      }
      } while (c.moveToNext());

      }
      database.close();
      return todos;
      }


      and this Code is on my Activity with my ListView:



      List<ToDo> dataSource;
      ToDoOverviewListAdapter adapter;

      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_to_do_overview);

      lv = (ListView) findViewById(R.id.listToDo);
      dataSource = ToDoDatabaseHelper.getInstance(this).readAllToDos();

      adapter = new ToDoOverviewListAdapter(this, dataSource);
      lv.setAdapter(new ToDoOverviewListAdapter(this, dataSource));
      }

      // Go to Activity to Add new Item - Add Button is in newItem_Activity
      public void createToDo(){
      startActivity(new Intent(ToDoOverview.this, ToDoCreateNew.class));
      refreshListView();
      }

      private void refreshListView(){
      dataSource.clear();
      dataSource.addAll(ToDoDatabaseHelper.getInstance(this).readAllToDos());
      adapter.notifyDataSetChanged();
      }









      share|improve this question














      In my App i have a ListView, if i add some new Items, the ListView does not shown the Items. The items will be shown if i close the app and go back in it.



      So my new Items are saved in my Database, but the list will not be synchronized.
      i followed a Tutorial on youtube, if he tries it, it works fine, but not in my app. i hope someone can help me to find my problem.



      In my newItem_Activity i have an Add-Button with following Code:



      btnAdd.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
      if(todo.getName() == null){
      Toast.makeText(ToDoCreateNew.this, "Please insert some value.", Toast.LENGTH_LONG).show();
      return;
      }
      ToDoDatabaseHelper.getInstance(ToDoCreateNew.this).createTodo(todo);
      finish();
      }
      });


      My Database looks like this:



      public ToDo createTodo(final ToDo todo) {
      SQLiteDatabase database = this.getWritableDatabase();

      ContentValues contentValues = new ContentValues();
      contentValues.put(NAME_COLUMN, todo.getName());
      contentValues.put(DUEDATE_COLUMN, todo.getDueDate() == null ? null : todo.getDueDate().getTimeInMillis() / 1000);
      contentValues.put(FAVORITE_COLUMN, todo.isFavorite() ? 1 : 0);
      contentValues.put(DESCRIPTION_COLUMN, todo.getDescription());
      contentValues.put(DUETIME_COLUMN, String.valueOf(todo.getDueTime() == null ? null : todo.getDueTime().getTime()));

      long newID = database.insert(TABLE_NAME, null, contentValues);
      database.close();
      return readToDo(newID);
      }

      public List<ToDo> readAllToDos(){
      List<ToDo> todos = new ArrayList<>();
      SQLiteDatabase database = this.getReadableDatabase();

      Cursor c = database.rawQuery("SELECT * FROM " + TABLE_NAME, null);
      if (c.moveToFirst()){
      do {
      ToDo todo = readToDo(c.getLong(c.getColumnIndex(ID_COLUMN)));
      if (todo != null){
      todos.add(todo);
      }
      } while (c.moveToNext());

      }
      database.close();
      return todos;
      }


      and this Code is on my Activity with my ListView:



      List<ToDo> dataSource;
      ToDoOverviewListAdapter adapter;

      protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_to_do_overview);

      lv = (ListView) findViewById(R.id.listToDo);
      dataSource = ToDoDatabaseHelper.getInstance(this).readAllToDos();

      adapter = new ToDoOverviewListAdapter(this, dataSource);
      lv.setAdapter(new ToDoOverviewListAdapter(this, dataSource));
      }

      // Go to Activity to Add new Item - Add Button is in newItem_Activity
      public void createToDo(){
      startActivity(new Intent(ToDoOverview.this, ToDoCreateNew.class));
      refreshListView();
      }

      private void refreshListView(){
      dataSource.clear();
      dataSource.addAll(ToDoDatabaseHelper.getInstance(this).readAllToDos());
      adapter.notifyDataSetChanged();
      }






      java sql android-studio






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Dec 30 '18 at 14:05









      diem_Ldiem_L

      11111




      11111
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Do one thing when you add lists in adapter , wrap it in a method and call that method in both onCreate and OnStart Method . So basically you have to add list in adapter two times On OnCreate method and OnStart method. So When you update the data and move back to your original activity it will trigger OnStart method and it will show updated data.






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978262%2fandroid-studio-new-item-in-list-is-not-shown%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            Do one thing when you add lists in adapter , wrap it in a method and call that method in both onCreate and OnStart Method . So basically you have to add list in adapter two times On OnCreate method and OnStart method. So When you update the data and move back to your original activity it will trigger OnStart method and it will show updated data.






            share|improve this answer




























              0














              Do one thing when you add lists in adapter , wrap it in a method and call that method in both onCreate and OnStart Method . So basically you have to add list in adapter two times On OnCreate method and OnStart method. So When you update the data and move back to your original activity it will trigger OnStart method and it will show updated data.






              share|improve this answer


























                0












                0








                0







                Do one thing when you add lists in adapter , wrap it in a method and call that method in both onCreate and OnStart Method . So basically you have to add list in adapter two times On OnCreate method and OnStart method. So When you update the data and move back to your original activity it will trigger OnStart method and it will show updated data.






                share|improve this answer













                Do one thing when you add lists in adapter , wrap it in a method and call that method in both onCreate and OnStart Method . So basically you have to add list in adapter two times On OnCreate method and OnStart method. So When you update the data and move back to your original activity it will trigger OnStart method and it will show updated data.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Dec 30 '18 at 14:15









                RANAJEET BARIKRANAJEET BARIK

                2216




                2216






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53978262%2fandroid-studio-new-item-in-list-is-not-shown%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Mossoró

                    Error while reading .h5 file using the rhdf5 package in R

                    Pushsharp Apns notification error: 'InvalidToken'