从文件</string,?>中读取java.util.Map <string,?>

时间:2015-04-10 17:58:58

标签: java android dictionary libgdx

我在从保存到首选项文件的地图中获取值时遇到问题。我可以从地图上打印出键,但不能打印任何值。我认为它与我正在做的类型转换有关,但我已经尝试了我所知道的所有内容并且无法弄明白。

我测试了我保存到首选项文件的地图中的值,它们输出得很好。

我尝试了以下建议,但他们没有帮助

继承我的代码

public class SetSettings {
private Actor actor;
private Actor hit;
private Sprite sprite;
private Sprite sprite2;
private Rectangle rect;
private boolean customHit = false;
private ShapeRenderer render = new ShapeRenderer();
public float y;
public float x;
Array<Actor> actors = GameScreen.buttons.stage.getActors();

public SetSettings() {
    setOriginal();
    setCustom();
    rect = new Rectangle();
}

public void setOriginal() {
    learnGame.ass.settings.get().clear();
    float height = Gdx.graphics.getHeight();
    float width = Gdx.graphics.getWidth();
    // ui settings
    java.util.Map<String, Coords> map = new HashMap<String, Coords>();
    map.put("hpBar", new Coords(width - (learnGame.ass.hpBar.getWidth() * 1.02f), height - (height * .076f)));
    map.put("hpBase", new Coords(learnGame.ass.hpBar.getX(), learnGame.ass.hpBar.getY()));

    for (Entry<String, Coords> key : map.entrySet())
        System.out.println(key.getValue().x); // works fine here

}

public void setCustom() {
    java.util.Map<String, Coords> amap = (java.util.HashMap<String, Coords>) learnGame.ass.settings.get();
    // Float[] nums = amap.get("hpBar");
    for (Entry<String, Coords> key : amap.entrySet()) {
        // /float t = key.getValue().x; // <-----------error here -- java.lang.String cannot be cast
        System.out.println("6" + key.getKey());
        // String xz = key.getValue();
    }

}

public class Coords {
    float x;
    float y;

    public Coords(float x, float y) {
        this.x = x;
        this.y = y;
    }
}

}

3 个答案:

答案 0 :(得分:0)

您应该从共享偏好设置中保存/加载,例如给定here

的示例
//persist
HashMap<String, Integer> counters; //the hashmap you want to save
SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();    
for (String s : counters.keySet()) {
    editor.putInteger(s, counters.get(s));
}
editor.commit();


//load
SharedPreferences pref = getContext().getSharedPreferences("Your_Shared_Prefs", Context.MODE_PRIVATE);
HashMap<String, Integer> map= (HashMap<String, Integer>) pref.getAll();
for (String s : map.keySet()) {
        Integer value=map.get(s);
        //Use Value
}

您需要查看在Coords中使用的方法的返回值,并使用适当键入的变量来存储这些返回值。

这样的事情应该没问题:

 for (Entry<String, Coords> key : amap.entrySet()) {
       float x_value = key.getValue().getX(); 
       float y_value = key.getValue().getY(); 
    }

使用getter工作正常:

public class Coords {
    float x;
    float y;

    public Coords(float x, float y) {
        this.x = x;
        this.y = y;
    }
     public float getX(){
       return this.x;
      }
     public float getY(){
       return this.y;
      }
}

答案 1 :(得分:-1)

你要做的是:

float t = Float.parseFloat(theString);

您还应该抓住"NumberFormatException"

答案 2 :(得分:-1)

你的问题在这里:

java.util.Map<String, Coords> amap = (java.util.HashMap<String, Coords>) learnGame.ass.settings.get();

您无法将Map<String, ?>投射到HashMap<String, Coords>!它不是TypeSafe!

尝试这样的事情:

Map<String, ?> settings = learnGame.ass.settings.get();
for (Entry<String, ?> entry : settings.entrySet()) {
    if (entry.getValue() instanceof Coords) {
        Coords coords = (Coords) entry.getValue();
        // do some with coords
    }
}