2011-10-31 164 views
0

我目前有一個自定義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(); 



    } 
+0

看看我的回答,希望這會幫助你。 – user370305

+0

您能告訴我爲什麼要將數據存儲在共享首選項中嗎?是安全原因還是你不想與其他人分享? – user370305

+0

嗯,我只是希望將用戶在「listview」中輸入的項目存儲起來,以便當它們返回到當前活動時,listview將被重新填充。我認爲sharedPreferences將是要走的路,但也許不是?我應該保存這個包還是你推薦的? – Splitusa

回答

0

我覺得你的問題是,這個for循環,

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及以上版本。任何方法來解決這個問題?謝謝。 – Splitusa