0

I have created a list of items using RecyclerView. I have basically followed this tutorial, which matches my initial requirement. My final activity is:

public class PlacesActivity extends AppCompatActivity {
    private GeoDataClient mGeoDataClient;

    //a list to store all the products
    List<PlaceSaved> placesList;

    //the recyclerview
    RecyclerView recyclerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.places_layout);

        //getting the recyclerview from xml
        recyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));

        //initializing the productlist
        placesList = new ArrayList<>();

        //adding some items to our list
        placesList.add(
            new PlaceSaved(
                1,
                "Victoria Memorial, Kolkata",
                "22.5448, 88.3426",
                R.drawable.property_image_3
        ));

        placesList.add(
            new PlaceSaved(
                1,
                "Tower of London, London",
                "51.5081, 0.0759",
                R.drawable.property_image_3
        ));

        placesList.add(
            new PlaceSaved(
                1,
                "Uppsala Castle, Sweden",
                "59.8533, 17.6356",
                R.drawable.property_image_3
        ));

        //creating recyclerview adapter
        PlacesAdapter adapter = new PlacesAdapter(this, placesList);
        //setting adapter to recyclerview
        recyclerView.setAdapter(adapter);
        RecyclerView.ItemAnimator itemAnimator = new DefaultItemAnimator();
        itemAnimator.setAddDuration(1000);
        itemAnimator.setRemoveDuration(1000);
        recyclerView.setItemAnimator(itemAnimator);
    }
}

This works fine for initial learning (as this is a part of my very first project in java), but the problem is the list is hard coded. I want this list to be editable and manageable by the end-user(he should be able to edit/save/add/delete etc).

I have searched google to get 2 option:

  1. SharedPreference
  2. SQlite database through Gson

I don't expect the user to create more than 20 such places, though there is no hardcoded array size limit.

So, I have 2 questions to ask:

  1. which is better among SQLite and sharedpreference for this small number of list
  2. I am yet to find any tutorial/documentation on creation/storage and retrieve the database. Can you kindly show me some way to do that?

Regards,

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
BaRud
  • 3,055
  • 7
  • 41
  • 89
  • do you want to add item using some kind of dialog ? – Umar Hussain Mar 05 '18 at 09:57
  • read this answer regarding SQLite and SharedPreference :) https://stackoverflow.com/a/15698559/6914992. – jace Mar 05 '18 at 10:02
  • @umar: no... These data will be shown in the activity layout, as defined in places_layout. – BaRud Mar 05 '18 at 10:08
  • please share the xml layout then as well – Umar Hussain Mar 05 '18 at 10:09
  • I will point you to a different direction. Try Realm. Realm is the world's first (if not only) mobile first database. It is fairly simple to use (definitely simpler than SQLite), and the developers claim it is faster than SQLite. For details: https://realm.io/docs/java/latest/ – Anuraag Baishya Mar 05 '18 at 10:13

3 Answers3

2

Shared preferences are usually used for storing app configurations and user preferences in app setting. For larger data you should use SQLite.

For accessing SQLite use the support library Room by Google which is basically an abstraction over SQLite for developers ease.

Please see this article on using Room library, hopefully it will help in your case : https://medium.com/@ajaysaini.official/building-database-with-room-persistence-library-ecf7d0b8f3e9

Umar Hussain
  • 3,461
  • 1
  • 16
  • 38
1

SQLite is the solution... if you want to store some content like a list. I think The following link contains all the content you need to create your project

https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/

Aniket
  • 391
  • 5
  • 13
  • @Aniket: thanks for the reference. The tutorial does not include any layout file. Does that mean that I don't need a recyclerview while working with the database? – BaRud Mar 05 '18 at 10:55
  • its totally up to your requirement @BaRud – Aniket Mar 05 '18 at 13:16
  • for working on adapters and perform curd operation you can refer to this: https://www.simplifiedcoding.net/android-sqlite-database-example/ IF there is an issue to understand that code please inform me ... I will create some basic code which can help you to understand the working. – Aniket Mar 05 '18 at 13:28