2016-09-19 75 views
0

我剛開始學習Android編程。我正在使用按鈕製作一個簡單的Tic Tac Toe遊戲。但本場比賽是工作,我打算我想添加一個「LogActivity」查看獲獎者如何使用ListView for Android App生成遊戲日誌列表?

這裏是我想使 https://www.youtube.com/watch?v=Y7D1FFx7B78

這裏什麼的視頻是我到目前爲止做出視頻 https://www.youtube.com/watch?v=8vwJh3gWeaw

這裏是主要部分從我的代碼爲TicTacToeActivity.java

public void buttonClicked(Button btn){ 
    if (turn){ 
     btn.setText("X"); 
     TextView textView = (TextView) findViewById(R.id.turn_display); 
     textView.setText(R.string.o_turn); 
    } 
    else { 
     btn.setText("O"); 
     TextView textView = (TextView) findViewById(R.id.turn_display); 
     textView.setText(R.string.x_turn); 
    } 
    turnCount++; 
    //need to prevent button click once clicked 
    btn.setClickable(false); 
    turn = !turn; 
    //check winner 
    checkWinner(); 
} 
private void checkWinner(){ 
    //check if already winner 
    boolean winnerExist = false; 
    //horizontal check 
    if (btnTopLeft.getText() == btnTopCenter.getText() && btnTopCenter.getText()== btnTopRight.getText() && !btnTopLeft.isClickable()){ 
     winnerExist = true; 
    } 
    else if (btnMidLeft.getText() == btnMidCenter.getText() && btnMidCenter.getText() == btnMidRight.getText() && !btnMidLeft.isClickable()){ 
     winnerExist = true; 
    } 
    else if (btnBottomLeft.getText()== btnBottomCenter.getText() && btnBottomCenter.getText() == btnBottomRight.getText() && !btnBottomLeft.isClickable()){ 
     winnerExist = true; 
    } 
    //vertical checks 
    else if (btnTopLeft.getText() == btnMidLeft.getText() && btnMidLeft.getText() == btnBottomLeft.getText() && !btnTopLeft.isClickable()){ 
     winnerExist = true; 
    } 
    else if (btnTopCenter.getText() == btnMidCenter.getText() && btnMidCenter.getText() == btnBottomCenter.getText() && !btnTopCenter.isClickable()){ 
     winnerExist = true; 
    } 
    else if (btnTopRight.getText() == btnMidRight.getText() && btnMidRight.getText() == btnBottomRight.getText() && !btnTopRight.isClickable()){ 
     winnerExist = true; 
    } 
    //diagonal check 
    else if (btnTopLeft.getText() == btnMidCenter.getText() && btnMidCenter.getText()== btnBottomRight.getText() && !btnTopLeft.isClickable()){ 
     winnerExist = true; 
    } 
    else if (btnBottomLeft.getText() == btnMidCenter.getText() && btnMidCenter.getText() == btnTopRight.getText() && !btnBottomLeft.isClickable()){ 
     winnerExist = true; 
    } 

    if (winnerExist){ 
     if (!turn) { 
      // X wins toast("X wins"); 
      TextView textView = (TextView) findViewById(R.id.turn_display); 
      textView.setText(R.string.x_win); 
      logCount++; 
      Log.i("log", "X"); 


     } 
     else { 
      TextView textView = (TextView) findViewById(R.id.turn_display); 
      textView.setText(R.string.o_win); 
      logCount++; 
      Log.i("log", "O"); 
     } 
     //call generic method 
     enableDisableButtons(false); 
    } 
    else if (turnCount == 9){ 
     TextView textView = (TextView) findViewById(R.id.turn_display); 
     textView.setText(R.string.game_tie); 
     logCount++; 
     Log.i("log", "Draw"); 
    } 

另一部分來自從TicTacToeActivity爲LogActivity 我現在不想將它存儲到數據庫中。所以我只想列出X Wins或O Wins,或者遊戲是Draw。我試圖讓它使用ListView工作,但我沒有足夠的知識。我用Google搜索,但沒有找到什麼,我找

我評論過我嘗試了代碼,但沒有工作

btnLog.setOnClickListener(new View.OnClickListener(){ 
    @Override 
    public void onClick(View view) { 
     Intent logIntent = new Intent(TicTacToeActivity.this, LogActivity.class); 
     //TextView log_view = (TextView) findViewById(R.id.log_list); 
     //logIntent.putExtra("log_view", R.id.log_list); 
     startActivity(logIntent); 

     //Intent logIntent = new Intent(TicTacToeActivity.this, LogActivity.class); 
     //TextView log_view = (TextView) findViewById(R.id.log_list); 
     //logIntent.putExtra("log_view", R.id.log_list); 


     //addIntent.putExtra("num2display", num2input.getText().toString()); 
     //LogList 
     //startActivity(logIntent); 
     //startActivity(LogIntent); 

     //list = (ListView) findViewById(R.id.list); 

     //add(); 


    } 
}); 

這裏是activity_log.xml的UI

  <?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="edu.uco.rawal.p3rabina.LogActivity"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:id="@+id/message" 
     android:textSize="30sp" 
     android:text="@string/log_button" /> 





    <LinearLayout 
     android:orientation="horizontal" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/log_block" 
     android:background="@color/colorPrimaryDark" 

     android:layout_below="@+id/message" 
     android:layout_centerVertical="true" 
     android:layout_centerHorizontal="true"> 


     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/numColumn" 
      android:text="@string/s_num" 
      android:textColor="@color/colorWhite" 
      android:textSize="26sp" 
      android:layout_weight="1"/> 
     <TextView 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:id="@+id/result" 
      android:text="@string/result" 
      android:textColor="@color/colorWhite" 
      android:textSize="26sp" 
      android:layout_weight="1"/> 


    </LinearLayout> 
    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/log_block"> 
     <ListView 
      android:id="@+id/list_view" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 

      android:background="@android:color/darker_gray" /> 
    </LinearLayout> 



</RelativeLayout> 
+0

你應該閱讀[我如何在Java中比較字符串?](http://stackoverflow.com/questions/513832 /怎麼辦 - 我 - 比較 - 字符串,在Java的)。 –

回答

1

日誌中用於日誌文件(錯誤,調試等),不應該用於在應用程序視圖中顯示信息。

我明白了一個DB是矯枉過正,但你可能想使用某種形式的持久性數據存儲Data Options。在你的情況下,你可能想要將一個列表保存到你的井字遊戲活動的內部存儲中,這可以在你的LogActivity中讀取。

如果你想避免任何存儲到磁盤,來解決你的問題就來存儲遊戲結果的列表,並通過列表中來回你的活動之間作爲額外的意圖有問題的方式。

MainActivity.java

ArrayList<String> myList = new ArrayList<>(); 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Sample results added to your ArrayList 
    myList.add("X"); 
    myList.add("O"); 
    myList.add("Draw"); 

    Button b = (Button) findViewById(R.id.listPasser); 
    b.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      Intent intent = new Intent(MainActivity.this, ListActivity.class); 

      // Add the extra to the intent and use it to call the next activity 
      intent.putExtra("resultsLog", myList); 
      startActivity(intent); 
     } 
    }); 

} 

ListActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_list); 

    // Get your list from the intent you made in MainActivity 
    ArrayList<String> myList = getIntent().getExtras().getStringArrayList("resultsLog"); 

    ListView lv = (ListView) findViewById(R.id.listView); 

    ListAdapter adapter = new ArrayAdapter<>(
      this, 
      android.R.layout.simple_list_item_1, 
      myList); 

    lv.setAdapter(adapter); 

//  Easier way to do things if you want to use TextView instead of ListView 
//  StringBuilder output = new StringBuilder(); 
//  for(String s : myList) { 
//   output.append(myList.indexOf(s) + " " + s + System.getProperty("line.separator")); 
//  } 
// 
//  lv.setText(output.toString()); 
    } 
} 
+0

我試圖使用intent的putExtra和getExtra。但是,當我將其應用於ListView時,我無法使其工作。當我搜索ListView時,我發現有人使用適配器,但我無法正確應用它,因爲我是新手。在這種情況下需要ListView,因爲我需要迭代遊戲獲勝者列表。 – robinleathal

+0

ListView不是必需的。你可以從你的ArrayList中建立一個字符串,然後在TextView中設置文本。但是,如果你堅持使用ListView,我已經編輯了我的答案,添加了一個簡單的例子來啓動和運行。 – jtwaller