私はAndroidプログラミングの学習を始めました。私はボタンを使ってシンプルなTic Tac Toeゲームを作っています。ゲームは、私が意図したとおりに機能していますが、私はここでAndroid AppのListViewを使用してゲームログのリストを生成する方法は?
は、私がここに https://www.youtube.com/watch?v=Y7D1FFx7B78
を作りたいと思っています何のビデオは、私がこれまでに作られたものの映像です受賞者を表示するには、「LogActivity」を追加したいですここ https://www.youtube.com/watch?v=8vwJh3gWeaw
TicTacToeActivity.java
LogActivityためTicTacToeActivityからのための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");
}
別の部分のための私のコードからの主要な部分であります私は今データベースに保存したくありません。だから私はX WinsまたはO Winsのいずれかをリストにしたい、またはゲームはDrawです。私はそれをListViewを使って動作させようとしましたが、十分な知識がありません。私はグーグルが、私は私が試したコードをコメントしている
を探していますが、働いていなかったものを見つけることができませんでした
ここ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();
}
});
はUI
ためactivity_log.xmlは <?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>
あなたは[私はJavaで文字列を比較するにはどうすればよい?]読んでください(http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)を参照してください。 –