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:
SharedPreference
SQlite
database throughGson
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:
- which is better among
SQLite
andsharedpreference
for this small number of list - 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,