Permalink
Browse files

Done T0803-FinishQuizExample

  • Loading branch information...
GinaHsu committed Oct 11, 2017
1 parent b3dfde6 commit cb2fecefb2b817e774fad4b01513c583d1e2fe20
@@ -23,6 +23,7 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.udacity.example.droidtermsprovider.DroidTermsExampleContract;
@@ -38,6 +39,10 @@
// The current state of the app
private int mCurrentState;
// The index of the definition and word column in the cursor
private int mDefCol, mWordCol;
private TextView mWordTextView, mDefinitionTextView;
private Button mButton;
// This state is when the word definition is hidden and clicking the button will therefore
@@ -56,6 +61,8 @@ protected void onCreate(Bundle savedInstanceState) {
// Get the views
// TODO (1) You'll probably want more than just the Button
mWordTextView = (TextView) findViewById(R.id.text_view_word);
mDefinitionTextView = (TextView) findViewById(R.id.text_view_definition);
mButton = (Button) findViewById(R.id.button_next);
//Run the database operation to get the cursor off of the main thread
@@ -84,30 +91,47 @@ public void onButtonClick(View view) {
public void nextWord() {
// Change button text
mButton.setText(getString(R.string.show_definition));
// TODO (3) Go to the next word in the Cursor, show the next word and hide the definition
// Note that you shouldn't try to do this if the cursor hasn't been set yet.
// If you reach the end of the list of words, you should start at the beginning again.
mCurrentState = STATE_HIDDEN;
if(mData != null){
// If you reach the end of the list of words, you should start at the beginning again.
if(!mData.moveToNext()){
mData.moveToFirst();
}
//Hide the definition TextView
mDefinitionTextView.setVisibility(View.INVISIBLE);
//Change button text
mButton.setText(getString(R.string.show_definition));
//Get the next word
mWordTextView.setText(mData.getString(mWordCol));
mDefinitionTextView.setText(mData.getString(mDefCol));
mCurrentState = STATE_HIDDEN;
}
}
// TODO (4) Show the definition
public void showDefinition() {
if(mData != null){
mDefinitionTextView.setVisibility(View.VISIBLE);
// Change button text
mButton.setText(getString(R.string.next_word));
// TODO (4) Show the definition
mCurrentState = STATE_SHOWN;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
// TODO (5) Remember to close your cursor!
mData.close();
}
// Use an async task to do the data fetch off of the main thread.
@@ -138,6 +162,13 @@ protected void onPostExecute(Cursor cursor) {
// TODO (2) Initialize anything that you need the cursor for, such as setting up
// the screen with the first word and setting any other instance variables
//Get the column index of each piece of data in the Cursor
mDefCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_DEFINITION);
mWordCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_WORD);
//Set the initial state
nextWord();
}
}
@@ -6,14 +6,7 @@
android:minHeight="?android:attr/listPreferredItemHeight"
android:orientation="vertical"
tools:context="com.udacity.example.quizexample.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:id="@+id/button_next"
android:text="@string/show_definition"
android:onClick="onButtonClick"/>
<TextView
android:id="@+id/text_view_word"
android:layout_width="match_parent"
@@ -25,6 +18,15 @@
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="@+id/button_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="20dp"
android:onClick="onButtonClick"
android:text="@string/show_definition" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"

0 comments on commit cb2fece

Please sign in to comment.