共享首选项:检索并显示存储的数据

时间:2015-10-09 11:33:24

标签: android sharedpreferences

我已经使用共享首选项将我的数据存储到MyUserChoice.xml中:

<link href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.css" rel="stylesheet"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/4.0.1/dropzone.js"></script>

<form action="/file-upload" class="dropzone" id="my-awesome-dropzone"></form>

我试图检索上面的3个字符串。

<string name="MyUserChoice">ApplicationInfo{1ebad2cb com.example.user.example},
    ApplicationInfo{15c7caa8 com.android.gallery},
    ApplicationInfo{bc0a9c1 com.android.quicksearchbox}</string>

我想要实现的是,当我重新启动我的应用程序时,它应该一个接一个地显示每个字符串。

for (int i = 0; i < count; i++) {
                String currentItem = (String) myList.getAdapter()
                        .getItem(i);
                if (selectedItems.contains(currentItem)) {
                    myList.setItemChecked(i, true);
                    Toast.makeText(getApplicationContext(),
                            "Current Item: " + currentItem,
                            Toast.LENGTH_LONG).show();
                } else {
                    myList.setItemChecked(i, false);
                }
        } 

相反,它什么也没有返回,我在logcat中看到了这个:

Current Item:  ApplicationInfo{1ebad2cb com.example.user.example},
Current Item:  ApplicationInfo{15c7caa8 com.android.gallery},
Current Item:  ApplicationInfo{bc0a9c1 com.android.quicksearchbox}

任何人都知道我的编码有什么问题?特此附上的是我的MainActivity.java:

10-09 01:19:18.756    1311-1311/com.android.systemui W/ResourceType﹕ No package identifier when getting value for resource number 0x00000000
10-09 01:19:18.756    1311-1311/com.android.systemui W/PackageManager﹕ Failure retrieving resources for com.example.checkboxsharedpreferences: Resource ID #0x0
10-09 01:19:18.781    1311-1327/com.android.systemui I/art﹕ Background sticky concurrent mark sweep GC freed 10643(410KB) AllocSpace objects, 0(0B) LOS objects, 0% free, 22MB/22MB, paused 5.699ms total 16.577ms
10-09 01:19:19.371      931-931/? W/SurfaceFlinger﹕ couldn't log to binary event log: overflow.

2 个答案:

答案 0 :(得分:1)

将MyPREFERENCES.toString()更改为MyPREFERENCES。它已经是String.No需要使用.toString();

SharedPreferences.Editor prefEditor = sharedpreferences.edit();
String savedItems = getSavedItems();
prefEditor.putString(MyPREFERENCES, savedItems);
prefEditor.commit();

从SharedPreferences检索数据:

SharedPreferences sharedpreferences= getSharedPreferences(PREF_NAME, MODE_PRIVATE);
String channel = sharedpreferences.getString(MyPREFERENCES, "null");

答案 1 :(得分:1)

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 */

 var React = require('react-native');
var SideMenu = require('react-native-side-menu');
var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Navigator,
} = React;

var ContentView = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to React Native!
        </Text>
      </View>
    );
  }
});

var TestView = React.createClass({
  render: function() {
    return (
      <View style={styles.container}>
        <Text style={styles.welcome}>
          Welcome to another page.
        </Text>
        <Text style={styles.instructions}>
          Testing react native side menu with navigator.
        </Text>
      </View>
    );
  }
});

var Menu = React.createClass({
  about: function() {
    this.props.menuActions.close();
    this.props.navigator.push({
      component: TestView,
      title: 'Test View',
    });
  },

  render: function() {
    return (
      <View style={styles.sidemenu}>
        <Text style={styles.paddingMenuItem}>Menu</Text>
        <Text onPress={this.about} style={styles.paddingMenuItem}>About</Text>
      </View>
    );
  }
});

var FindWisely = React.createClass({
  render: function() {
    return (
      <Navigator
       initialRoute={{
         component: Something,
         title: 'Something',
       }}
       configureScene={() => {
         return Navigator.SceneConfigs.FadeAndroid;
       }}
       renderScene={(route, navigator) => {
         if(route.component) {
           return React.createElement(route.component, { navigator });
         }
       }}/>
    );
  }
});

var Something = React.createClass({
  render: function() {
    var menu = <Menu navigator={this.props.navigator}/>;
    return (
      <SideMenu menu={menu}>
        <ContentView/>
      </SideMenu>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#F5FCFF',
  },
  welcome: {
    fontSize: 20,
    textAlign: 'center',
    margin: 10,
  },
  sidemenu: {
    paddingTop: 50,
  },
  paddingMenuItem: {
    padding: 10,
  },
});

module.exports = FindWisely;

没有回复有用的东西。

将其替换为

sharedpreferences.contains(MyPREFERENCES.toString())