2011-07-02 11 views
0

私は問題なくランダムな文字列を作成するために、StackOverflowの上ここにこの質問を使用している:D (Show random stringランダムな文字列を非表示/削除しますか?

いくつかの回と同じ文字列が現れ、それは迷惑なんです。

は、だから、私はすでにクラスを殺す終了セッションのボタンがあり、文字列だけセッションごとに1回を示したいと思います。だから私は1-3の数字を持っていると言うことができます。 1つしか数字が残っていないので、2が最初に表示され、次に1が表示されます.3つしか表示されません。 「次ボタン」の

マイボタンのコード。現在、クラスを終了し、再びそれを開始します!新しい文字列を表示するように変更するにはどうすればよいですか?

private void onButtonClick(Button clickedButton) { 
    Intent startIntent = null; 
    if (clickedButton.getId() == R.id.quit) { 
     startIntent = new Intent(this, mainmenu.class); 
     finish(); 
    } 

    else if (clickedButton.getId() == R.id.next) { 
     startIntent = new Intent(this, play.class); 
     startActivity(startIntent); 
     finish(); 
    } 

    if (startIntent != null) { 
     startIntent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); 
     startActivityIfNeeded(startIntent, 0); 
    } 
} 
private void setupbutton() {   
    View.OnClickListener onClickHandler = new View.OnClickListener() {   

     public void onClick(View v) { 
      onButtonClick((Button)v);    
     } 
    }; 
    Button button = (Button)findViewById(R.id.quit); 
    button.setOnClickListener(onClickHandler); 

    button = (Button)findViewById(R.id.next); 
    button.setOnClickListener(onClickHandler); 

答えて

0

同じ文字列が表示されています!同じ番号を連続して複数回生成する可能性があります。

は2に名前を付けるために、多くの可能性があります。

  1. は、アレイ内のすべての文字列を入れて、それをシャッフルします。そして、インデックス1から始まる一つずつアウトの要素を取る:

    Random rgenerator = new Random(); 
    
        ArrayList<String> strings = new ArrayList<String>(getResources().getStringArray(R.array.myArray));     
        int index = rgenerator.nextInt(strings.size()); 
        String randomString = strings.get(index); 
        strings.remove(index); 
    

List strings = new ArrayList<String>(getResources().getStringArray(R.array.myArray)); 
Collections.shuffle(list); 
  • 配列内のすべての文字列を入れて、文字列をランダムなインデックスを持つ文字列を選択して削除EDIT: [OK]を、私はあなたがすでに使用されていない文字列を覚えている...

    を参照してください。リスト内の文字列を格納し、リストには、静的にするので、あなたは活動playの異なるインスタンスの作成の間で状態を保存することができます

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.play); 
        setupbutton(); 
    
        if (myString == null || myString.size() == 0) { 
         Resources res = getResources(); 
         myString = new ArrayList(Arrays.asList(res.getStringArray(R.array.myArray))); 
        } 
    
        // get a random string 
        int rand = rgenerator.nextInt(myString.size()); 
        String q = myString.get(rand); 
    
        // remove the last used string from the list 
        myString.remove(rand); 
    
        TextView tv = (TextView) findViewById(R.id.text1); 
        tv.setText(q); 
    } 
    
  • +0

    doesnの」:次に、あなたのコンストラクタビットを変更

    private static ArrayList<String> myString; 

    仕事!エラーが発生します。 「; strings.remove(インデックス);文字列randomString = strings.get(インデックス)」私はすでにランダム生成ものを持って、私が追加しました文字列をMyStringに変更しました。 – JeppeHP

    +0

    ああ、どうしたの?あなたのコードはどのように見えますか?あなた自身を助ける努力をしていますか?私は他の機能をしようとしているbecuase – thaussma

    +0

    http://karlsson1337x.com/prtscr.jpg私が追加されたコードを削除しました。そして、画像上のコードは私のクラスの一部です!ありがとう。 – JeppeHP

    0

    インデックスのランダムな順列を作成し、それを使用してシーケンス内の次の文字列を選択します。乱数生成器は、乱数を生成するので、

    関連する問題