简体   繁体   English

阅读java.util.Map <String,?> 来自文件

[英]read java.util.Map<String,?> from a file

I'm having a problem with getting values from a map that I save into a Preferences file. 我从保存到首选项文件的地图中获取值时遇到问题。 I can print out the keys from the map, but not any values. 我可以从地图中打印出键,但是没有任何值。 I think it has something to do with the typecasting that I'm doing, but I've tried everything I know of and can't figure it out. 我认为这与我正在进行的类型转换有关,但是我已经尝试了所有我所知道的并且无法弄清楚的东西。

I tested the values from the map that I saved to the preferences file, and they output just fine. 我测试了保存到首选项文件中的映射中的值,它们的输出效果很好。

I tried the suggestions below but they did not help 我尝试了以下建议,但没有帮助

Heres my code 这是我的代码

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;
    }
}

} }

You should be saving/loading from shared preferences like the example given 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
}

You need to look at the return value of the methods you're using in Coords, and use variables typed appropriately to store those return values. 您需要查看您在Coords中使用的方法的返回值,并使用适当键入的变量来存储这些返回值。

Something like this should work no problem: 这样的事情应该没有问题:

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

Should work fine with a 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;
      }
}

What you have to do is: 您要做的是:

float t = Float.parseFloat(theString);

You should also catch a "NumberFormatException" 您还应该捕获一个"NumberFormatException"

Your problem at here: 您的问题在这里:

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

You can't just cast Map<String, ?> to HashMap<String, Coords> ! 您不能只将Map<String, ?>HashMap<String, Coords> It's not TypeSafe! 不是TypeSafe!

Try some like this: 试试这样:

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
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM