0

当应用程序启动时,我从服务器获得了一些静态数据(数组),我需要将这些数据用于微调器,用于不同的活动。因此,这些数据应该可以在不同时间从不同活动中获得,以创建微调器。

我怎样才能做到这一点?如何在代码中生成微调器(微调器数据是键 = 值)?

4

1 回答 1

0

什么类型的数据包含该数组?也许你可以用SharedPreferences它来保存它。

您可以在此处阅读有关生成微调器的信息。 CountryAdapter类是制作微调器适配器的示例,该适配器在微调器下拉列表中显示项目。

编辑:将您的数据放入 Hashtable 并创建 2 种方法来保存和读取 SharedPreferences 中的数据:

public boolean saveData( Hashtable<String, String> myData) {

    Editor editor = getApplicationContext().getSharedPreferences("myData", Context.MODE_PRIVATE).edit();
    for (String key : myData.values()) {
        editor.putString(key, myData.get(key));
    }
    return editor.commit();
}

public Hashtable<String, String>  restoreData() {
    Hashtable<String, String> myData = new Hashtable<String, String>();

    SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences("myData", Context.MODE_PRIVATE);
    int size = sharedPreferences.getInt("size", 0);

    for(int i =0; i < size; i++){
        myData.put(sharedPreferences.getString("key"+i, "key+1"), sharedPreferences.getString("value"+i, "value+1"));
    }

    return myData;
}
于 2013-04-15T19:37:27.160 回答