0
作爲一個缺乏經驗的Android程序員,我需要一些幫助。我正在設計這個應用程序的教堂,需要一個活動,可以記錄祈禱請求(類似於做一個待辦事項)。此時,我可以將項目輸入到列表中,但是在使用下面的共享首選項摘要關閉後,它們似乎並未保存並回來,還查看了此站點,看起來我可以在沒有按下物理按鈕的情況下執行此操作保存。列表共享首選項
SharedPreferences prayerRequests = getPreferences(Activity.MODE_PRIVATE);
ListView myListViewa = (ListView)findViewById(R.id.myListView);
SharedPreferences.Editor editor = prayerRequests.edit();
editor.putString("prayers", myListViewa.toString());
editor.apply();
整個類是在這裏:
package com.direction.investor.prayerjournal;
import java.util.ArrayList;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class prayerRequests extends Activity {
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.prayerrequest);
ListView myListView = (ListView)findViewById(R.id.myListView);
final EditText myEditText = (EditText)findViewById(R.id.myEditText);
final ArrayList<String> prayers = new ArrayList<String>();
final ArrayAdapter<String> aa;
aa = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, prayers);
myListView.setAdapter(aa);
SharedPreferences prayerRequests = getPreferences(Activity.MODE_PRIVATE);
ListView myListViewa = (ListView)findViewById(R.id.myListView);
SharedPreferences.Editor editor = prayerRequests.edit();
editor.putString("prayers", myListViewa.toString());
editor.apply();
myEditText.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN)
if ((keyCode == KeyEvent.KEYCODE_DPAD_CENTER) ||
(keyCode == KeyEvent.KEYCODE_ENTER)) {
prayers.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
return true;
}
return false;
}
});
}
}