【发布时间】:2018-03-17 13:25:55
【问题描述】:
我有一个简单的待办事项列表,我试图在应用程序退出和方向更改时保存待办事项(列表项)。我已经尝试了一些代码,但它似乎没有保存任何东西。这是我的 MainActivity。
package com.example.android.todolist;
import android.content.Context;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity
{
//Variables
Button addTodo;
ListView listView;
EditText itemInput;
ArrayAdapter<String> adapter;
TextView myTodoList;
String stringInput;
@Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Add custom font to title
myTodoList = (TextView)findViewById(R.id.tvMyTodoList);
Typeface textViewFont = Typeface.createFromAsset(getAssets(),"High Tide - Demo.ttf");
myTodoList.setTypeface(textViewFont);
//Find views by ID
listView = (ListView) findViewById(R.id.lvTodoView);
itemInput = (EditText) findViewById(R.id.etAddItem);
addTodo = (Button) findViewById(R.id.btnAddTodo);
//Set adapter onto ListView
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, new ArrayList<String>());
listView.setAdapter(adapter);
//On long click action
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
{
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id)
{
adapter.remove(adapter.getItem(position));
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Item Deleted", Toast.LENGTH_LONG).show();
return true;
}
});
//Add strikethrough to on click action, and remove strikethrough when clicked again
listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
TextView addStrikethroughToListItem = (TextView) view;
if ((addStrikethroughToListItem.getPaintFlags() & Paint.STRIKE_THRU_TEXT_FLAG) == 0)
{
addStrikethroughToListItem.setPaintFlags(addStrikethroughToListItem.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
}
else
{
addStrikethroughToListItem.setPaintFlags(0);
}
}
});
}
//Method to add item to ListView
public void addItemToList(View view)
{
Context appContext = getApplicationContext();
String emptyItem = "Please enter an item";
int duration = Toast.LENGTH_LONG;
if (itemInput.length() > 1)
{
adapter.add(itemInput.getText().toString().trim());
adapter.notifyDataSetChanged();
itemInput.setText(" ");
}
else
{
Toast.makeText(appContext, emptyItem, duration).show();
}
}
private static int ix = 0;
@Override
protected void onPause()
{
super.onPause();
submitPrefs();
Log.e(TAG,"OnPause executed");
}
@Override
protected void onResume()
{
super.onResume();
getStoredPrefs();
Log.e(TAG,"onResume executed");
}
private void submitPrefs()
{
SharedPreferences pref = getBaseContext().getSharedPreferences("MyPrefsX",MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString("InputX","My Iteration" + ix);
editor.commit();
ix++;
}
private void getStoredPrefs()
{
SharedPreferences pref = getBaseContext().getSharedPreferences("MyPrefsX",MODE_PRIVATE);
String s = pref.getString("InputX", "Not set yet");
Log.e(TAG,"My State == " + s);
}
}
我还没有在 onBackPressed 方法中添加任何代码,因为我还没有达到这一点。现在我对 onSaveInstanceState 方法和 onRestoreInstanceState 方法很感兴趣。
这是我的 XML。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.todolist.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp"
android:background="@drawable/sample_background3">
<TextView
android:id="@+id/tvMyTodoList"
android:layout_width="0dp"
android:layout_height="58dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:gravity="center"
android:text="@string/my_todo_s"
android:textSize="40sp"
app:layout_constraintHorizontal_bias="0.512"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<EditText
android:hint="@string/enter_todo_here"
android:id="@+id/etAddItem"
android:layout_height="49dp"
android:layout_width="261dp"
android:labelFor="@+id/etAddItem"
android:inputType="textCapCharacters"
android:layout_marginStart="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/lvTodoView"/>
<Button
android:id="@+id/btnAddTodo"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="addItemToList"
android:text="@string/button"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/etAddItem"
app:layout_constraintRight_toRightOf="@+id/lvTodoView"
app:layout_constraintTop_toBottomOf="@+id/lvTodoView"/>
<ListView
android:id="@+id/lvTodoView"
android:layout_width="0dp"
android:layout_height="373dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
app:layout_constraintHorizontal_bias="0.511"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="@+id/tvMyTodoList"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
关于我做错了什么有什么想法吗?该应用程序没有保存任何内容。唯一一次我发现我的待办事项仍在列表中是当我按下主页按钮并重新打开应用程序时。如果我以任何其他方式关闭应用程序,则不会保存任何内容。
我曾尝试使用 SharedPreferences,但运气不佳。我在论坛上询问 SharedPreferences 是否适合此操作,我被告知可以。我还询问了一个 SQLite 数据库,并被告知在这种情况下不需要它。我还在 YouTube 上尝试了各种教程,但没有任何效果。上面显示的代码是我尝试过的最后一种方法。我似乎找不到其他可以尝试的方法。
另外,由于这是我的第一个应用程序,如果有人能给我一些关于我所做工作的一般性反馈,无论是关于结构、代码还是任何人希望得到的任何建议,我将不胜感激摆脱我可能表现出的任何不良做法。
从主页按钮关闭重新打开应用程序
后退按钮按下
【问题讨论】:
-
你有没有追踪
onSaveInstanceState是否真的被解雇了?还是改用onPause? -
不,我不这么认为。我怎么会在上面留下痕迹?我应该添加一个日志吗?我也尝试过 onPause 但我可能犯了和现在一样的错误。没有任何效果。
-
我的意思是使用 Log.d() 向您的日志发送一些消息。或 Log.e()。不要忘记在完成的代码中删除它们!您可以在 android studio 中过滤登录。通过这些消息,您可以跟踪您的程序并查看哪些方法正在执行,哪些没有。
-
好的,谢谢。我会检查一下。
-
我刚试过这个,我已经添加了必要的日志。 onSaveInstanceState 和 onRestoreInstanceState 都会被调用。当我按下主页按钮时,会调用 OnSaveInstanceState,它可以工作。 onRestoreInstanceState 在我旋转屏幕时被调用,但不保存数据。代码已在相关方法中更新。有什么想法吗?
标签: java android listview android-studio-2.3 saving-data