2015-08-27 40 views
1

我對Android比較陌生,細節經常會導致我的程序無法運行。 TextView(在這種情況下tvQuestion)應該改變其內容,等待,然後再次改變它們,然而第一個改變是沒有看到的,程序等待並且只顯示最後的改變。 注意,所有的內容都是從SQLite數據庫中收集並放入一個自定義對象的問題。 我相信問題出在onClick(View v)方法,但是,任何和所有的幫助表示讚賞。TextView.setText()僅在睡眠後顯示最終更改()

這裏是我的代碼:

package com.infernalpoison.falseorfact.util; 

import android.app.Activity; 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

import com.infernalpoison.falseorfact.R; 

public class Play extends Activity implements View.OnClickListener { 
final private Context playContext = this; 
private int correct = 0; 
private int wrong = 0; 
private TextView tvQuestion; 
private ImageView btnFalse, btnFact; 
private Question questionObj; 
private Thread thrNewQuestion = new Thread() { 
    public void run() { 
     DatabaseConstruction dbc = new DatabaseConstruction(playContext); 
     try { 
      dbc.open(); 
      questionObj = dbc.getRdmQ(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } finally { 
      tvQuestion.setText(questionObj.getQuestion()); 
     } 
    } 
}; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);// Remove title bar 
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
      WindowManager.LayoutParams.FLAG_FULLSCREEN); // Remove notification bar 

    setContentView(R.layout.activity_play); 

    tvQuestion = (TextView) findViewById(R.id.tvQuestion); 
    btnFact = (ImageView) findViewById(R.id.btn_fact); 
    btnFact.setOnClickListener(this); 
    btnFalse = (ImageView) findViewById(R.id.btn_false); 
    btnFalse.setOnClickListener(this); 

    questionObj = new Question(); 

    thrNewQuestion.run(); 

} 

@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
     **case R.id.btn_fact: 
      factSelect(); 
      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      thrNewQuestion.run(); 
      break; 
     case R.id.btn_false: 
      falseSelect(); 
      try { 
       Thread.sleep(2000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      thrNewQuestion.run(); 
      break;** 
    } 
} 

private void factSelect() { 
    if (questionObj.isAnswer() == true) { 
     correct++; 
     tvQuestion.setText("That is Correct! Well Done!"); 
    } else { 
     wrong++; 
     tvQuestion.setText("That is Incorrect. " + questionObj.getFactCorrection()); 
     //TODO ADD MECH TO EXIT 
    } 
} 

private void falseSelect() { 
    if (questionObj.isAnswer() == false) { 
     correct++; 
     tvQuestion.setText("That is Correct! Well Done!"); 
    } else { 
     wrong++; 
     tvQuestion.setText("That is Incorrect. " + questionObj.getFalseCorrection()); 
     //TODO ADD MECH TO EXIT 
    } 
} 
} 

和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" 
tools:context="com.infernalpoison.falseorfact.util.PlayFragment"> 

<ImageView 

    android:id="@+id/btn_false" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentLeft="true" 
    android:adjustViewBounds="true" 
    android:clickable="true" 
    android:scaleType="fitXY" 
    android:src="@drawable/btn_false" /> 

<TextView 
    android:id="@+id/tvQuestion" 
    android:layout_width="200dp" 
    android:layout_height="200dp" 
    android:layout_centerInParent="true" 
    android:text="Empty" /> 

<ImageView 

    android:id="@+id/btn_fact" 
    android:layout_width="wrap_content" 
    android:layout_height="match_parent" 
    android:layout_alignParentRight="true" 
    android:adjustViewBounds="true" 
    android:clickable="true" 
    android:scaleType="centerCrop" 
    android:src="@drawable/btn_fact" /> 

在此先感謝。 PS這是我的第一篇文章,所以如果您需要更多信息請通知我!

+0

使用原始'Thread's在Android是氣餒。改用'AsyncTask'。 [實施例](http://developer.android.com/reference/android/os/AsyncTask.html)。 –

+1

看看線程。它看起來像你讓UiThread睡眠,但這也導致textview更改凍結 –

+1

@khalafnt線程不會在android中泄氣。這取決於線程在做什麼。 http://stackoverflow.com/a/9800870/3956566 –

回答

1

代替:

try { 
      Thread.sleep(2000); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     thrNewQuestion.run(); 

做這樣的事情:

new Handler().postDelayed(new Runnable() { 

     @Override 
     public void run() { 
      thrNewQuestion.run(); 
     } 
    }, 2000);