2011-10-31 6 views
0

현재 각 항목에 두 줄의 텍스트 (현재 시간 및 사용자 입력 텍스트)가있는 사용자 정의 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(); 



    } 
+0

봐에, 이것은 당신을 도울 것입니다 희망을 본다. – user370305

+0

공유 환경 설정에 데이터를 저장하려는 이유를 알려주시겠습니까? 보안상의 이유로 다른 사람과 공유하고 싶지 않습니까? – user370305

+0

글쎄, 나는 단순히 사용자가'listview'에 입력 한 항목을 저장하여 현재 액티비티로 돌아갈 때리스트 뷰가 다시 채워지도록하려고합니다. 나는 sharedPreferences가 갈 수있는 방법이라고 생각했지만 아마도 그렇지 않을까요? 번들을 저장해야합니까? 아니면 무엇을 권하고 싶습니까? – Splitusa

답변

0

,이 루프, 그래서

for (String string : feedPostMap.keySet()){    
sharedPrefEditor.putString(string, feedPostMap.get(string));  
    } 

시도

Set<String> s = new HashSet<String>(); 
    for (int i=0;i<list.size();i++){  
     s.add(list.get(i).get(string));  
    } 

sharedPrefEditor.putStringSet(stringSet, s); 

이 시도 : 여기

은 아래에있는 내 코드입니다 sharedpreference에서 편집 할 문자열

putStringSet(String key, Set<String> values) 

환경 설정 편집기에서 문자열 값 세트를 설정하면 commit()이 호출 된 후에 다시 기록됩니다. 추가 정보를

내 대답에 Android-SharedPreferences

+0

나는 이것을 지금 시험해 보겠다. 'commit()'또는'apply()'를 포함해야합니까? 감사합니다. – Splitusa

+0

또한'sharedPrefEditor.putStringSet (stringSet, s);에 설정된 문자열은 무엇입니까? '? – Splitusa

+0

좋아요, 그래서'putStringSet'은 API 11 이상에서만 작동한다는 것을 알았습니다. API 8 이상을 코딩하고 있습니다. 이 문제를 해결할 수있는 방법은 없나요? 감사. API 8에 대해 – Splitusa