0

I have a custom ArrayAdapter which is set to a listview with data from database:

CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, database.getAll());
listview.setAdapter(adapter);

Now, I want when the user deletes a database item the listview should refresh. Until now, I can successfully delete the item from the database, but it remains in the listview. In the ArrayAdapter class I call refresh() method from a new class same to adapter's parent class

    public void refresh(){
    ParentClass class_par = new ParentClass();
    class_par.refreshfromParent();
    }

And in the ParentClass I have a new method: refreshfromParent()

    public void refreshfromAlarm(){
    DatabaseHandler database = new DatabaseHandler(myview.getContext());
    listview.setAdapter(new CustomAdapter(myview.getContext(), -1, database.getAll())); // here I change listview adapter, and populate it with new database data, but I get NullPointerException
    }

So, how can I refresh listview items? Please, DO NOT mention notifyDataSetChanged(), it DOES NOT work.

6
  • just use adapter.notifyDatasetchanged() to refresh listview Commented Dec 31, 2016 at 11:26
  • yes, when you remove or change listitem data, just use above line. it make changes done by you Commented Dec 31, 2016 at 11:28
  • I have used but it crashes, with NullPointerException Commented Dec 31, 2016 at 11:29
  • do not use any ArrayAdapter when your data model is Cursor related, use SimpleCursorAdapter instead Commented Dec 31, 2016 at 11:29
  • @pskink Yes, when I retrieve all my data from database I use cursor. So, should I use SimpleCursorAdapter ? If so, how will the super() be in public CustomAdapter? Commented Dec 31, 2016 at 11:33

2 Answers 2

0

here you making en error

CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, database.getAll());
        listview.setAdapter(adapter);

    //so first you should make an arrayList and define it globally like
    // ArrayList <yourType>list = new ArrayList<>();

    //then initialize your adapter like this 

    list.addAll(databse.getAll());
    CustomAdapter adapter = new CustomAdapter(myview.getContext(), -1, list);
        listview.setAdapter(adapter);

    // now when you want to delete an item
    // delete it from your list like list.remove(location)
    // and also delete same item from your datebase
    // after that just call this
    notifyDataSetChanged();
Sign up to request clarification or add additional context in comments.

2 Comments

It throws OutOfBoundsException. I finally decided to close and reload my fragment which contains my listview, so it has the same result.
there should maybe some problem when you are deleting item from arraylist through specific location, that's way you are getting OutOfBoundsException error. reload fragment is not good approach
0

I realized that notifyDataSetChanged is a big trouble, so I decided to close and reload my fragment which contains my listview. Thus, the result looks the same with DataSetChanged and there is no difference in the users' eyes. I suggest everyone who reaches in this question do the same.

Comments

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.