我目前有一個自定義listview
,每個項目中有兩行文本,一個用於當前時間,另一個用於用戶輸入的文本。我通過創建一個新的hashmap
並將<ArrayList<HashMap<String,String>>
添加到listview
所使用的位置來執行此操作。我想將我的數據保存到sharedPreferences
,但我似乎只是從用戶那裏得到最後一個輸入。我的問題是:是否有從列表視圖中提取數據,然後將其添加到sharedpreferences?或者將arraylist中的數據添加到sharedpreferences中?從列表視圖中提取數據
這裏是我下面的代碼:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_feed);
//create button and implement on click listener
sendButton = this.findViewById(R.id.sendPostButton);
sendButton.setOnClickListener(this);
//create text field and add text change listener
postTextField = (EditText)findViewById(R.id.postTextField);
postTextField.addTextChangedListener(TextEditorWatcher);
//create text views for seeing the posts and character count
currentTimeTextView = (TextView)findViewById(R.id.postTimeTextView);
mainPostTextView = (TextView)findViewById(R.id.postTextView);
characterCountView = (TextView)findViewById(R.id.charsleft);
characterCountView.setText("150 chars left");
//text view for event name and set the text
feedName = (TextView)findViewById(R.id.nameOfFeed);
currentFeedName = CreateFeedActivity.eventFeedName;
feedName.setText(currentFeedName);
list = new ArrayList<HashMap<String,String>>();
//create the adapter for the list view
adapter = new SimpleAdapter(
this,
list,
R.layout.post_layout,
new String[]{"time", "post"},
new int[]{R.id.postTimeTextView, R.id.postTextView});
//set list adapter
setListAdapter(adapter);
//place the current feed number into a variable here
currentFeedCount = CreateFeedActivity.feedCount;
//create the hashmap for the list view
feedPostMap = new HashMap<String, String>();
//place the stored data into the view again if activity has already been created
if (LiveFeedrHomeActivity.feedOccurs == 1){
Log.d(TAG, "in feed occurs is 1");
//get the shared pref
sharedPref = getSharedPreferences(MY_FEED, 0);
Map<String, ?> map = new HashMap<String, String>();
map = sharedPref.getAll();
//convert from the map to the hashmap
feedPostMap = (HashMap<String, String>) map;
//add to the list
list.add(feedPostMap);
//refresh the adapter
adapter.notifyDataSetChanged();
Log.d(TAG, "feedmap get all");
}
//make variable feed = 1 so that you can't create another feed
LiveFeedrHomeActivity.feedOccurs = 1;
}
@Override
public void onClick(View button) {
switch(button.getId()){
case R.id.sendPostButton:
//create date
date = new Date();
//get current time
currentTime = new SimpleDateFormat("h:mm aaa").format(date);
SendToDatabase();
DisplayUserInput();
break;
}
@Override
public void onStop() {
super.onStop();
Log.d(TAG, "on stopp'd");
//get shared pref settings
sharedPref = getSharedPreferences(MY_FEED, 0);
sharedPrefEditor = sharedPref.edit();
//get the items in the hash map and add it to the
//shared preferences
for (String string : feedPostMap.keySet()){
sharedPrefEditor.putString(string, feedPostMap.get(string));
// }
sharedPrefEditor.commit();
}
看看我的回答,希望這會幫助你。 – user370305
您能告訴我爲什麼要將數據存儲在共享首選項中嗎?是安全原因還是你不想與其他人分享? – user370305
嗯,我只是希望將用戶在「listview」中輸入的項目存儲起來,以便當它們返回到當前活動時,listview將被重新填充。我認爲sharedPreferences將是要走的路,但也許不是?我應該保存這個包還是你推薦的? – Splitusa