• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to store added Items in Android

Bidhan1

Lurker
Jun 5, 2016
1
0
I recently wanted to develop a shopping list app and i followed this tutorial www.javacjava.com/AndroidTutorials.aspx but in this tutorial the tutor shows that his added items are stored but i followed the same process but when i close and reopen the app the added items are gone. what can be done to fix this?

here is my program code:

package com.dopenets.myapplication;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import android.app.AlertDialog;
import android.widget.EditText;
import android.content.DialogInterface;
import android.content.Context;
import android.content.SharedPreferences;
import android.app.Activity;

import android.widget.Toast;
import android.widget.AdapterView;
import android.widget.TextView;
import android.view.View;

public class MainActivity extends AppCompatActivity {
ArrayList<String> shoppingList = null;
ArrayAdapter<String> adapter = null;
ListView lv = null;

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

shoppingList = getArrayVal(getApplicationContext());

adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, shoppingList);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView parent, View view, final int position, long id) {
String selectedItem = ((TextView) view).getText().toString();
if (selectedItem.trim().equals(shoppingList.get(position).trim())) {
removeElement(selectedItem, position);
} else {
Toast.makeText(getApplicationContext(),"Error Removing Element", Toast.LENGTH_LONG).show();
}
}
});

}

@override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();


if (id == R.id.action_add){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Add Item");
final EditText input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialog, int which) {
shoppingList.add(preferredCase(input.getText().toString()));
Collections.sort(shoppingList);
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
return true;

}


return super.onOptionsItemSelected(item);
}
public static String preferredCase(String original)
{
if (original.isEmpty())
return original;

return original.substring(0, 1).toUpperCase() + original.substring(1).toLowerCase();
}
public static void storeArrayVal( ArrayList<String> inArrayList, Context context)
{
Set<String> WhatToWrite = new HashSet<String>(inArrayList);
SharedPreferences WordSearchPutPrefs = context.getSharedPreferences("dbArrayValues", Activity.MODE_PRIVATE);
SharedPreferences.Editor prefEditor = WordSearchPutPrefs.edit();
prefEditor.putStringSet("myArray", WhatToWrite);
prefEditor.commit();
}

public static ArrayList getArrayVal( Context dan)
{
SharedPreferences WordSearchGetPrefs = dan.getSharedPreferences("dbArrayValues",Activity.MODE_PRIVATE);
Set<String> tempSet = new HashSet<String>();
tempSet = WordSearchGetPrefs.getStringSet("myArray", tempSet);
return new ArrayList<String>(tempSet);
}

public void removeElement(String selectedItem, final int position){
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Remove " + selectedItem + "?");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialog, int which) {
shoppingList.remove(position);
Collections.sort(shoppingList);
storeArrayVal(shoppingList, getApplicationContext());
lv.setAdapter(adapter);
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
}
 

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones