从列表视图中提取数据

时间:2011-10-31 16:48:34

标签: android android-listview sharedpreferences

我目前有一个自定义listview,每个项目中有两行文本,一行用于当前时间,另一行用于用户输入的文本。我这样做是通过创建新的hashmap并添加到<ArrayList<HashMap<String,String>>使用的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();



    }

1 个答案:

答案 0 :(得分: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); 

尝试此操作,以便在共享首选项中编辑字符串集,

putStringSet(String key, Set<String> values) 

在首选项编辑器中设置一组字符串值,一旦调用commit()就会被写回。

了解更多信息,请查看Android-SharedPreferences