2011-04-24 13 views
0

向所有幫助我解決其他問題的人表示感謝。我幾乎擁有它,但最後的兩個問題阻止它按我想要的方式工作。陣列列表正確傳輸併爲每個陣列列表成員設置分數

這2個班級應該做如下。第一類獲得想要玩遊戲的人的姓名。使用相同的EditText,當他們輸入他們的名字時,他們點擊提交。當提交所有的名字時,他們點擊完成/播放按鈕,將他們和他們的數據(有多少名球員和名字)發送到下一堂課。在第一課我相信錯誤在於提交按鈕。我試圖將所有名稱添加到數組列表中,我不相信它正確地做到了。當我運行這個應用程序時,從用戶的角度來看,它的名字很好。但是在下面的屏幕上,它應該顯示他們的名字:(它說空,所以它沒有正確地得到名字)和一個要做的事(它正確地)。

它需要做的最後一件事是在類2上它需要允許那些按鈕(失敗,冠軍和不壞)只需要點擊一次(然後它設置一個分數到人的名字把它變成)然後它需要開始下一個人和任務。 (它沒有atm)。我真的很感謝幫助讓這件事情發揮作用。感謝所有花時間回覆的人。如果你不願意看到我的求助請求,很抱歉。

1類

public class Class1 extends Activity 
{ 
    int players=0, i=0; 
    String names[]; 

    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.class1); 

     final EditText input = (EditText) findViewById(R.id.nameinput); 

     final ArrayList<String> names = new ArrayList<String>(); 
     //names = new String[players]; 

     Button submitButton = (Button) findViewById(R.id.submit_btn); 
     submitButton.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View submit1) 
      { 
       //for(i=i; i < players; i++) 
       //{ 
        players++; 
        names.add(input.getText().toString()); 
        //names[i] = input.getText().toString(); 
        input.setText(""); 
       //} 
      } 
     }); 

     Button doneButton = (Button) findViewById(R.id.done_btn); 
     doneButton.setOnClickListener(new View.OnClickListener() 
     { 
      public void onClick(View done1) 
      { 
       Intent done = new Intent(Class1.this, Game.class); 
       Bundle bundle = new Bundle(); 
       bundle.putStringArrayList("arrayKey", names); 

       done.putExtra("players", players); 
       //done.putExtra("names", names[players]); 
       startActivity(done); 
      } 
     }); 
    } 

遊戲類

public class Game extends Activity 
{ 
    int players, counter=0, score, ptasks,rindex; 
    String[] names; 
    String[] tasks; 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.game); 

     Bundle bundle = this.getIntent().getExtras(); 
     String[] names = bundle.getStringArray("arrayKey"); 

     Intent game = getIntent(); 
     players = game.getIntExtra("players", 1); 
     //names = game.getStringArrayExtra("names"); 

     Random generator = new Random(); 

     tasks = new String[10]; 
     tasks[0]= ""; 
     tasks[1]= ""; 
     tasks[2]= ""; 
     tasks[3]= ""; 
     tasks[4]= ""; 
     tasks[5]= ""; 
     tasks[6]= ""; 
     tasks[7]= ""; 
     tasks[8]= ""; 
     tasks[9]= ""; 

     names = new String[players]; 

     while (counter <5) 
     { 
      for (int i = 0; i < players; i++) 
      { 
       TextView name1 = (TextView) findViewById(R.id.pname); 
       name1.setText(names[i]+":"); 

       ptasks = 10; 
       rindex = generator.nextInt(ptasks); 

       TextView task = (TextView) findViewById(R.id.task); 
       task.setText(tasks[rindex]); 

       Button failButton = (Button) findViewById(R.id.fail_btn); 
       failButton.setOnClickListener(new View.OnClickListener() 
       { 
        public void onClick(View failed) 
        { 
         return; 
        } 
       }); 

       Button notButton = (Button) findViewById(R.id.notbad_btn); 
       notButton.setOnClickListener(new View.OnClickListener() 
       { 
        public void onClick(View notbad) 
        { 
         return; 
        } 
       }); 

       Button champButton = (Button) findViewById(R.id.champ_btn); 
       champButton.setOnClickListener(new View.OnClickListener() 
       { 
        public void onClick(View champp) 
        { 
         return; 
        } 
       }); 

      } 

      counter++; 
     } 
    } 
} 

作爲附帶說明。您在那些在他們旁邊有//註釋的部分中看到的內容我在那裏,因爲我正在測試這些內容和那些未被註釋並且都沒有工作的內容。如果你有任何關於修復這個問題的意見,我很感激。

+0

你也應該回到你剛纔的問題和在適當的地方選擇答案並對答案有用/有用的用戶投票。這是一個簡單的手勢和基本的禮貌,這將使人們繼續幫助你:) – 2011-04-25 04:36:40

回答

0

我看到兩個問題與您的代碼,爲什麼你在爲你的球員列表中null這或許可以解釋你的第二個Activity

  1. GameString[] names = bundle.getStringArray("arrayKey");

    ArrayList<String> names = bundle.getStringArrayList("arrayKey");` 
    
  2. Class1,您將ArrayList放入Bundlebundle.putStringArrayList("arrayKey", names);),這是從開始沒有意義的不在哪裏。你應該把它變成了Intent來代替:

    done.putStringListExtra("arrayKey", names); 
    

請注意,你的代碼是更加令人困惑,因爲你同時擁有String []名爲namesArrayList命名names在不同的範圍。決定一個(我建議List)並擺脫其他。

此外,在Game,這是unncessary:

Bundle bundle = this.getIntent().getExtras(); 
String[] names = bundle.getStringArray("arrayKey"); 

Intent game = getIntent(); 
players = game.getIntExtra("players", 1); 

你已經有bundle在此之前只是,這樣你就可以如做:

Bundle bundle = this.getIntent().getExtras(); 
String[] names = bundle.getStringArray("arrayKey"); 
players = bundle.getInt("players", 1); 

的基本概念是從呼叫活動,您使用各種putExtra()putExtraXXX()方法將信息放入Intent。在稱爲活動中,你得到的要麼

  • 讓你放Intent的信息Bundle *從*通過getExtras()Intent,然後把頭髮在使用上的各種get()方法的一切Bundle不是Intent)。
  • 直接在Intent上調用getExtraXXX()方法。

對於第二部分,因爲你的代碼目前維持,它只是要遍歷馬上所有的球員(5次的一切,我不明白的counter目的)。

只有當按下其中一個按鈕時,您應該執行的所有處理(計算當前播放器的分數,遞增播放器索引的值,設置下一個任務等)。如果這將是一項長期的任務,那麼您可以禁用按鈕直至完成,以強制每個玩家只允許按下一個按鈕。當下一個玩家準備就緒時重新啓用按鈕。

我沒有精力去生產出你所需要的一切,但在一個起點,開啓此:

public void onCreate(Bundle savedInstanceState) 
{ 
    //...other code here 
    while (counter <5) 
    { 
     for (int i = 0; i < players; i++) 
     { 
      TextView name1 = (TextView) findViewById(R.id.pname); 
      name1.setText(names[i]+":"); 

      ptasks = 10; 
      rindex = generator.nextInt(ptasks); 

      TextView task = (TextView) findViewById(R.id.task); 
      task.setText(tasks[rindex]); 

      Button failButton = (Button) findViewById(R.id.fail_btn); 
      failButton.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View failed) 
       { 
        return; 
       } 
      }); 

      Button notButton = (Button) findViewById(R.id.notbad_btn); 
      notButton.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View notbad) 
       { 
        return; 
       } 
      }); 

      Button champButton = (Button) findViewById(R.id.champ_btn); 
      champButton.setOnClickListener(new View.OnClickListener() 
      { 
       public void onClick(View champp) 
       { 
        return; 
       } 
      }); 

     } 

     counter++; 
    } 
    //...other code here 
} 

public void onCreate(Bundle savedInstanceState) 
{ 
    //...other code here 
    int i = 0; 
    TextView name1 = (TextView) findViewById(R.id.pname); 
    TextView task = (TextView) findViewById(R.id.task);   

    Button failButton = (Button) findViewById(R.id.fail_btn); 
    failButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View failed) 
     { 
      //do what must be done for the current player, calculate score, etc 
      prepareNextPlayer(++i, names, name1, task); 
     } 
    }); 

    Button notButton = (Button) findViewById(R.id.notbad_btn); 
    notButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View notbad) 
     { 
      //do what must be done for the current player, calculate score, etc 
      prepareNextPlayer(++i, names, name1, task); 
     } 
    }); 

    Button champButton = (Button) findViewById(R.id.champ_btn); 
    champButton.setOnClickListener(new View.OnClickListener() 
    { 
     public void onClick(View champp) 
     { 
      //do what must be done for the current player, calculate score, etc 
      prepareNextPlayer(++i, names, name1, task); 
     } 
    }); 
    //...other code here 
} 

private void prepareNextPlayer(int i, ArrayList<String> names, String [] tasks, TextView nameField, TextView taskField) 
{ 
     if(i >= names.size()) 
     { 
      //all players have been processed, what happens now? 
      return; 
     } 

     int rindex = generator.nextInt(10); 
     nameField.setText(names.get(i)+":"); 
     task.setText(tasks[rindex]); 
}