1

I have my app set so that when a user clicks on an item in the listview it disappears. However when I close the activity or app the list refreshes dully with all items included. Is there a way for me to delete the item permanently after the click it?

This is my code:

    public class Movies extends AppCompatActivity {

    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ListView moviesListView = (ListView)findViewById(R.id.moviesListView);

        final ArrayList<String> topMovies = new ArrayList<String>(asList("1. The Shawshank Redemption", "2. The Godfather", "3. The Godfather: Part 2",
                "4. The Dark Knight", "5. Pulp Fiction","6. Schlinder's List","7. 12 Angry Men","8. The Lord of the Rings: The Return of the King",
                "9. The Good, the Bad and the Ugly","10. Fight Club","11. The Lord of the Rings: The Fellowship of the Ring","12. Star Wars: Episode V - The Empire Strikes Back",
                "13. Forrest Gump","14. Inception","15. One Flew Over the Cuckoo's Nest","16. The Lord of the Rings: The Two Towers","17. Goodfellas",
                "18. The Matrix","19. Seven Samurai","20. Star Wars: Episode IV - A New Hope","21. City of God","22. Se7en","23. The Silence of the Lambs",
                "24. The Usual Suspects","25. It's a Wonderful Life","26. Life is Beautiful","27. Leon: The Professional","28. Once Upon a Time in the West",
                "29. Spirited Away","30. Interstellar","31. Saving Private Ryan","32. Casablanca","33. American History X","34. Psycho","35. City Lights","36. Raiders of the Lost Ark",
                "37. Rear Window","38. The Intouchables","39. Modern Times","40. The Green Mile","41. Terminator 2: Judgement Day","42. The Pianist","43. The Departed",
                "44. Whiplash","45. Back to the Future","46. Memento","47. Gladiator","48. Apocolypse Now","49. Dr. Strangelove","50. The Prestige"));

        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topMovies);

        moviesListView.setAdapter(arrayAdapter);

        final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.pindrop);



        moviesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String toRemove = arrayAdapter.getItem(position);
                arrayAdapter.remove(toRemove);

            }
        });
    }

}
Amarjit
  • 4,327
  • 2
  • 34
  • 51
  • option 1: store your list in a file and delete the selected item from the file. Option 2 : use sqlite db to store your list and delete the selected item from the db. – Gareth Price Feb 11 '16 at 13:28
  • Thanks, could you tell me the easiest way to do option 1 please? I am new to this and quite lost. – BelovedAunt Feb 11 '16 at 14:14
  • Try this: http://stackoverflow.com/questions/8152125/how-to-create-text-file-and-insert-data-to-that-file-on-android. Then to remove items from that list you would use string handling to match the movie clicked in the list. you can use something along the lines of this: http://stackoverflow.com/questions/3634772/read-a-text-file-and-search-a-string-in-android. to delete you can do something along the lines of this: http://stackoverflow.com/questions/14290474/how-to-delete-line-in-textfile-android. Once you have done this I would recommend learning to use sqlite for managing your data. – Gareth Price Feb 12 '16 at 13:14

4 Answers4

1

You should also remove it from your array list. Because your listview loads from your array so if that one stays unchanged, the listview on next load will also be unchanged.

mheonyae
  • 581
  • 2
  • 8
  • 24
1

You are hard-coding the values of topMovies in your Activity, so whenever you open the activity the values inside this ArrayList gets reset and in turn you see same item over and over again even after deleting it.

In this scenario the process of deletion has effect until you close the activity or app.

You have two simple solutions here.

Solution 1

Save the data you need in the ArrayList to sqlite database in the device. While launching the app/activity you fetch the data from DB and assign to your ArrayList. When you delete the item from your ListView, you do two things. One, delete it from the ArrayList (also call notifyDataSetChanged() on adapter) and two, delete it from the sqlite database. This way when you launch the activity/app you will have the update data every time.

Solution 2 Just a solution or more like a work-around

Store the values you need in the ArrayList in ShardPreference and do same thing as Solution 1.

Note: You should follow the first solution. I have added the second one just for the sake of information that this could also be done.

Rohit5k2
  • 17,948
  • 8
  • 45
  • 57
  • Correct. When using SQLite Database I would reccomend using a CursorAdapter instead of ArrayAdapter. – Desdroid Feb 11 '16 at 13:42
  • Thanks. I would prefer to use sharedpreference as time is running out for me. Is there a way to use sharedpreference in the Movies class or do I have to create a new java class? – BelovedAunt Feb 11 '16 at 14:15
  • No you can use shared pref from this class only. – Rohit5k2 Feb 11 '16 at 14:17
  • See this how to use shared preference http://stackoverflow.com/questions/23024831/android-shared-preferences-example – Rohit5k2 Feb 11 '16 at 14:20
0

Try this way :

Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);

OR

modify the ArrayList and call notifyDataSetChanged() on the ArrayAdapter.

arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
Amit Vaghela
  • 22,772
  • 22
  • 86
  • 142
0

Try to remove the items which is being clicked/Removed from the array which you have populated in the listview which in turn will ensure you that your listview will not be loaded with data even when come back to your activity and getting out of it.

Naresh Narsing
  • 765
  • 6
  • 9
  • How does this solve the question. He just wants the list view with the latest data. Not with no data at all ? – Desdroid Feb 11 '16 at 13:37