単語を表示するためのソリューション:(randomWordの一部を非表示および表示するための)スパン可能な文字列の代わりに、indexOfおよびuserinputとともに、ユーザーからの推測の後に正しい文字を入力できるStringBuilderを見つけました。このような:
public void guess()
{
char guess = userInput.getText().charAt(0);
StringBuilder builder = new StringBuilder(hiddenWord);
String j = ""+guess;
int index = randomedWord.indexOf(guess);
if (randomedWord.contains(j))
{
while (index >= 0)
{
builder.setCharAt(index*2, guess);
index = randomedWord.indexOf(guess, index + 1);
hiddenWord = builder.toString().trim();
wordHolder.setText(hiddenWord);
if (!(hiddenWord.toString().contains("_".toString())) )
{
winner();
}
}
}
else
{
showImages();
}
}
RANDOMWORDの解決策(生成):Jamesの回答を見てください。カーソル自体をDBに返しましたが、必要な列の文字列値ではありません。これは正しい方法です。
db.open();
wordHolder.setText(db.randomize());
db.close();
public String randomize() {
Cursor cursor;
cursor = this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
if(cursor.moveToFirst()) {
return cursor.getString(cursor.getColumnIndexOrThrow( "word" ));
}
return "";
}
フルコード:
// MainActivity.java
package com.emanuelolsson.simplehangman;
import database.DBAdapter;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class HangMan extends Activity {
// DECLARE NEEDED VARIABLES
private Button newGameButton, enterLetterButton;
private EditText userInput;
private TextView wordHolder;
private ImageView imageOne, imageTwo, imageThree, imageFour, imageFive, imageSix, imageSeven, imageEight, winner, hanged;
private String hiddenWord, randomedWord;
private int count = 0;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hang_man);
DBAdapter db = new DBAdapter(this);
// ASSIGN OBJECTS
newGameButton = (Button) findViewById(R.id.newGame);
enterLetterButton = (Button) findViewById(R.id.enter);
wordHolder = (TextView) findViewById(R.id.wordHolder);
userInput = (EditText) findViewById(R.id.userInput);
imageOne = (ImageView) findViewById(R.id.imageView1);
imageTwo = (ImageView) findViewById(R.id.imageView2);
imageThree = (ImageView) findViewById(R.id.imageView3);
imageFour = (ImageView) findViewById(R.id.imageView4);
imageFive = (ImageView) findViewById(R.id.imageView5);
imageSix = (ImageView) findViewById(R.id.imageView6);
imageSeven = (ImageView) findViewById(R.id.imageView7);
imageEight = (ImageView) findViewById(R.id.imageView8);
winner = (ImageView) findViewById(R.id.winner);
hanged = (ImageView) findViewById(R.id.hanged);
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.INVISIBLE);
hanged.setVisibility(View.INVISIBLE);
wordHolder.setVisibility(View.VISIBLE);
// ADD LISTENERS
newGameButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
newGame();
}
});
enterLetterButton.setOnClickListener(new OnClickListener(){
public void onClick(View arg0) {
if (!(userInput.getText().toString().isEmpty()) )
{
guess();
} else if (userInput.getText().toString().isEmpty())
{
Toast toast = Toast.makeText(getApplicationContext(), "You need to insert a letter", Toast.LENGTH_SHORT);
toast.show();
}
}
});
db.open();
db.fill();
randomedWord = db.randomize();
invWord();
wordHolder.setText(hiddenWord);
db.clear();
db.close();
}
public void newGame() {
DBAdapter db = new DBAdapter(this);
count = 0;
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.INVISIBLE);
hanged.setVisibility(View.INVISIBLE);
db.open();
db.fill();
randomedWord = db.randomize();
invWord();
wordHolder.setText(hiddenWord);
db.clear();
db.close();
}
public void invWord()
{
hiddenWord = randomedWord;
hiddenWord = hiddenWord.replaceAll(".", "_" +" ");
}
public void guess()
{
char guess = userInput.getText().charAt(0);
StringBuilder builder = new StringBuilder(hiddenWord);
String j = ""+guess;
int index = randomedWord.indexOf(guess);
if (randomedWord.contains(j))
{
while (index >= 0)
{
builder.setCharAt(index*2, guess);
index = randomedWord.indexOf(guess, index + 1);
hiddenWord = builder.toString().trim();
wordHolder.setText(hiddenWord);
if (!(hiddenWord.toString().contains("_".toString())) )
{
winner();
}
}
}
else
{
showImages();
}
}
private void winner()
{
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.INVISIBLE);
winner.setVisibility(View.VISIBLE);
}
private void showImages() {
count ++;
if (count == 1)
{
imageOne.setVisibility(View.VISIBLE);
}
else if (count == 2)
{
imageTwo.setVisibility(View.VISIBLE);
}
else if (count == 3)
{
imageThree.setVisibility(View.VISIBLE);
}
else if (count == 4)
{
imageFour.setVisibility(View.VISIBLE);
}
else if (count == 5)
{
imageFive.setVisibility(View.VISIBLE);
}
else if (count == 6)
{
imageSix.setVisibility(View.VISIBLE);
}
else if (count == 7)
{
imageSeven.setVisibility(View.VISIBLE);
}
else if (count == 8)
{
gameEnd();
}
}
private void gameEnd()
{
imageOne.setVisibility(View.INVISIBLE);
imageTwo.setVisibility(View.INVISIBLE);
imageThree.setVisibility(View.INVISIBLE);
imageFour.setVisibility(View.INVISIBLE);
imageFive.setVisibility(View.INVISIBLE);
imageSix.setVisibility(View.INVISIBLE);
imageSeven.setVisibility(View.INVISIBLE);
imageEight.setVisibility(View.VISIBLE);
hanged.setVisibility(View.VISIBLE);
wordHolder.setText(randomedWord);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_hang_man, menu);
return true;
}
}
// XML
<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" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picOne"
android:src="@drawable/one" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picTwo"
android:src="@drawable/two" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picThree"
android:src="@drawable/three" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picFour"
android:src="@drawable/four" />
<ImageView
android:id="@+id/imageView5"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picFive"
android:src="@drawable/five" />
<ImageView
android:id="@+id/imageView6"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picSix"
android:src="@drawable/six" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picSeven"
android:src="@drawable/seven" />
<ImageView
android:id="@+id/imageView8"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="43dp"
android:contentDescription="@string/picEight"
android:src="@drawable/eight" />
<ImageView
android:id="@+id/winner"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:contentDescription="@string/winner"
android:src="@drawable/winner" />
<ImageView
android:id="@+id/hanged"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignTop="@+id/imageView1"
android:layout_centerHorizontal="true"
android:contentDescription="@string/hanged"
android:src="@drawable/hanged" />
<TextView
android:id="@+id/wordHolder"
android:layout_width="175dp"
android:layout_height="40dp"
android:layout_centerInParent="true"
android:layout_alignTop="@+id/imageView1"
android:layout_marginTop="134dp"
android:layout_marginLeft="50dp"
android:ems="10"
android:textSize="30dp" />
<Button
android:id="@+id/newGame"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/wordHolder"
android:layout_centerVertical="true"
android:text="@string/newGame" />
<Button
android:id="@+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/newGame"
android:layout_alignBottom="@+id/newGame"
android:layout_toLeftOf="@+id/newGame"
android:text="@string/enter" />
<EditText
android:id="@+id/userInput"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignBaseline="@+id/enter"
android:layout_alignBottom="@+id/enter"
android:layout_toLeftOf="@+id/enter"
android:ems="10"
android:inputType="text"
android:textSize="20dp" >
<requestFocus />
</EditText>
<ImageView
android:id="@+id/imageView9"
android:layout_width="250dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:contentDescription="@+id/header"
android:src="@drawable/header" />
</RelativeLayout>