增量设置<string>

时间:2016-02-10 19:54:03

标签: android

我正在制作一个有6种不同挑战的游戏;我把它放在不同的课堂上。每次我玩游戏时,出现的挑战都不会相同,因为我将课程随机化了,我不希望同样的挑战再次出现(冗余)。任何人都可以帮助我吗? 这是MainActivity.java

public class MainActiv extends Activity{
private static final String msg = null;
private Random random = new Random();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    SharedPreferences pref2 = getApplicationContext().getSharedPreferences("set", MODE_PRIVATE);
    SharedPreferences.Editor editor2 = pref2.edit();
    Set<String> set = new HashSet<String>();
    set.add(dragsandal.class.getName());
    set.add(Terimakasih.class.getName());
    set.add(Ludah.class.getName());
    set.add(Permisi.class.getName());
    set.add(Tolong.class.getName());
    set.add(Maaf.class.getName());
    editor2.putStringSet("set", set);
    editor2.apply();
    set = pref2.getStringSet("set", set);
    Set<Class> classes = new HashSet<Class>();
    for (String s : set) {
        try{
            classes.add(Class.forName(s));
        }
        catch(ClassNotFoundException ignore){
        }
    }
    List<Class> list = new ArrayList<Class>(classes);
    Class item = list.get(random.nextInt(list.size()));
    Log.d(msg, "remove:" + item);
    set.remove(item.getName());
    Log.d(msg, "set " + set.size());
    editor2.putStringSet("st", set);
    editor2.apply();

    Intent m = new Intent(MainActiv.this,item);
    startActivity(m);
}

}

下面:这是想要访问集合更改的类。 Skor.java

public class Skor extends Activity {
private static final String msg = null;
ArrayList<Class> nama = new ArrayList<Class>();
private Random random = new Random();

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.skor);

    ImageButton next = (ImageButton) findViewById(R.id.lanjutkan);
    next.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Set<String> set = new HashSet<String>();
            SharedPreferences pref2 = getApplicationContext().getSharedPreferences("set", MODE_PRIVATE);
            SharedPreferences.Editor editor2 = pref2.edit();
            set = pref2.getStringSet("set", set);
            Log.d(msg, "set " + set.size());
            Set<Class> classes = new HashSet<Class>();
            for (String s : set) {
                try {
                    classes.add(Class.forName(s));
                } catch (ClassNotFoundException ignore) {
                }
            }
            List<Class> list = new ArrayList<Class>(classes);
            Class item = list.get(random.nextInt(list.size()));
            Log.d(msg, "class:" + item);
            //no redundan
            set.remove(item.getName());
            editor2.putStringSet("set", set);
            editor2.apply();
            Log.d(msg, "set " + set.size());
            Intent main = new Intent(Skor.this, item);
            startActivity(main);
        }
    });
}}

在MainActivity.java中设置已成功删除随机项。但是在Skor.java中,该集合没有删除并返回到原始集合

0 个答案:

没有答案