0

我有一个 TextView 和一个字符串 randomWord。当应用程序启动时,我打开我的数据库并调用方法 db.open() 打开它,然后调用 db.randomize() 方法调用 randomize() 方法并查询数据库以获取随机条目并将其作为字符串返回。此后我只想 MyTextView.setText (randomWord); 但是当应用程序运行时,什么都没有显示。我试图在 setText (“whatever”)中进行硬编码,并显示出来。

所以我想我在随机化或其他方面做错了,因为我没有从数据库中得到一个字。

这就是我尝试做的事情:

 db.open();
        db.randomize(generatedWord);
        //text = new SpannableString(generatedWord);
        //text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0);  
        wordHolder.setText("" + generatedWord);
        db.close();

这是我的随机方法:

  public Cursor randomize(String word) {
            @SuppressWarnings("unused")
            Cursor cursor;
            return cursor =  this.db.query("tblnames ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
        }

我的数据库有两列:“_id”和“word”

"create table tblnames (_id integer primary key autoincrement, "
     + "words text not null);"

是否必须这样做,我没有在 randomize() 中的某处指定 _id 和单词?

另外:我尝试了 setText (generatedWord) 和 setText (" +generatedWord);另外:运行时没有错误。

4

2 回答 2

1

你没有用你的randomize()方法返回一个字符串。

这应该会给你想要的效果

db.open();
//text = new SpannableString(generatedWord);
//text.setSpan(new ForegroundColorSpan(Color.WHITE), 0, text.length(), 0);  
wordHolder.setText(db.randomize());
db.close();

在您的数据库中,您需要从游标返回所需列的字符串值,而不是游标本身。 http://developer.android.com/reference/android/database/Cursor.html#getString(int )

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 "";
}
于 2012-09-23T19:35:34.347 回答
-1

显示单词的解决方案:我找到了StringBuilder,而不是可扩展的字符串(隐藏和显示randomWord的一部分),连同indexOf和userinput,我能够在用户猜测后输入正确的字母。像这样:

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();
        }
    }

随机字的解决方案(生成它):看看詹姆斯的回答,我已经在我的数据库上返回了游标本身,而不是我想要的列的字符串值,这是正确的方法

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>
于 2012-10-24T21:19:37.440 回答