我有一个问题,所以我不知道为什么如何从给定对象丢失的内存数据中抛出一个应用程序,但是当我退出应用程序这些数据时。
此类将数据保存到SharedPrefernce
public class MySharedPreference {
public static final String PREFS_NAME = "LIST_CARD";
public static final String CARD = "CARD";
public MySharedPreference() {
super();
}
public void saveCardToSharedPreference(Context context, ArrayList<Card> cardList) {
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
editor = settings.edit();
Gson gson = new Gson();
String jsonString = gson.toJson(cardList);
editor.putString(CARD, jsonString);
editor.commit();
}
public ArrayList getCardList(Context context) {
SharedPreferences sharedPreferences;
List<Card> card;
sharedPreferences = context.getSharedPreferences(PREFS_NAME,
Context.MODE_PRIVATE);
if (sharedPreferences.contains(CARD)) {
String jsonCard = sharedPreferences.getString(CARD, null);
Gson gson = new Gson();
Card[] cardItem = gson.fromJson(jsonCard, Card[].class);
card = Arrays.asList(cardItem);
card = new ArrayList(card);
} else
return null;
return (ArrayList) card;
}
}
MainActivity
此课程显示我的名单
public class MainActivity extends AppCompatActivity {
public ArrayList<Card> cardsList = new ArrayList<Card>();
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(newBase);
}
private TextView textCard;
private CardAdapter cardAdapter;
private RecyclerView cardRecyclerView;
private LinearLayout cardLayout;
private MySharedPreference mySharedPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
Typeface custom_fonts2 = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Bold.ttf");
mySharedPreference = new MySharedPreference();
cardLayout = (LinearLayout) findViewById(R.id.layoutEmptyCard);
cardLayout.setVisibility(View.VISIBLE);
Default app = (Default) getApplicationContext();
cardsList = app.getListCard();
if (cardsList.size() > 0) {
cardsList = new ArrayList<Card>();
cardsList = app.getListCard();
cardLayout.setVisibility(View.GONE);
mySharedPreference.getCardList(MainActivity.this);
cardRecyclerView = (RecyclerView) findViewById(R.id.recycleViewCard);
cardAdapter = new CardAdapter(this, cardsList);
cardRecyclerView.setLayoutManager(new LinearLayoutManager(MainActivity.this));
cardRecyclerView.setAdapter(cardAdapter);
}
if (cardsList.size() == 0) {
cardLayout.setVisibility(View.VISIBLE);
}
TextView title_app = (TextView) findViewById(R.id.toolbar_title);
title_app.setTypeface(custom_fonts);
textCard = (TextView) findViewById(R.id.textCard);
textCard.setTypeface(custom_fonts);
Button addCardButton = (Button) findViewById(R.id.addCardButton);
addCardButton.setTypeface(custom_fonts2);
addCardButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addCard();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.addCard) {
addCard();
}
return super.onOptionsItemSelected(item);
}
private void addCard() {
Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
startActivity(intent);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
try {
this.finishAffinity();
} catch (Exception e) {
}
return true;
}
return super.onKeyDown(keyCode, event);
}
}