현재 각 항목에 두 줄의 텍스트 (현재 시간 및 사용자 입력 텍스트)가있는 사용자 정의 listview
이 있습니다. 나는 새로운 hashmap
을 만들고 <ArrayList<HashMap<String,String>>
에 listview
을 사용하여 이것을 추가한다. 내 데이터를 sharedPreferences
에 저장하고 싶습니다만 사용자로부터 마지막 입력을받는 것처럼 보입니다. 내 질문은 : 어쨌든 listview에서 데이터를 추출하고 다음 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'에 입력 한 항목을 저장하여 현재 액티비티로 돌아갈 때리스트 뷰가 다시 채워지도록하려고합니다. 나는 sharedPreferences가 갈 수있는 방법이라고 생각했지만 아마도 그렇지 않을까요? 번들을 저장해야합니까? 아니면 무엇을 권하고 싶습니까? – Splitusa