使用带有db数据的String,不能使用TextView的TextText

时间:2012-09-23 18:22:58

标签: java string textview settext

所以这就是问题所在。我有一个TextView和一个String randomWord。当应用程序启动时,我打开我的数据库并调用方法db.open()打开它,然后调用db.randomize()调用randomize()方法并查询db for random entry并将其作为String返回。此后我只想要MyTextView.setText(randomWord);但当应用程序运行时,没有显示任何内容我试图在setText中进行硬编码("无论")并显示出来。

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

这是我尝试做的事情:

 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"和"字" "创建表tblnames(_id整数主键自动增量,"         +"单词text not null);"

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

另外:我尝试了setText(generatedWord)以及setText("" + generatedWord); 另外:跑步时没有错误。

2 个答案:

答案 0 :(得分: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 "";
}

答案 1 :(得分:-1)

显示WORD的解决方案:我找到了StringBuilder,而不是一个spannable字符串(隐藏和显示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();
        }
    }

解决方案为RANDOMWORD(生成它):看看James的回答,我已经将光标本身返回到我的数据库而不是我想要的列的字符串值,这是正确的方法

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>