udacity / ud851-Exercises

Apache License 2.0
2.04k stars 6.81k forks source link

T08.02-Solution-AddAsyncTaskToRetrieveCursor #446

Closed DrHaitham closed 5 years ago

DrHaitham commented 5 years ago

/*

package com.udacity.example.quizexample;

import android.content.ContentResolver; import android.database.Cursor; import android.os.AsyncTask; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button;

import com.udacity.example.droidtermsprovider.DroidTermsExampleContract;

/**

public class MainActivity extends AppCompatActivity {

// The current state of the app
private int mCurrentState;

//  (3) Create an instance variable storing a Cursor called mData
private Button mButton;
private Cursor mCursor;

// This state is when the word definition is hidden and clicking the button will therefore
// show the definition
private final int STATE_HIDDEN = 0;

// This state is when the word definition is shown and clicking the button will therefore
// advance the app to the next word
private final int STATE_SHOWN = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Get the views
    mButton = (Button) findViewById(R.id.button_next);

    //  (5) Create and execute your AsyncTask here
    new ConnectToContentProvider().execute();
}

/**
 * This is called from the layout when the button is clicked and switches between the
 * two app states.
 * @param view The view that was clicked
 */
public void onButtonClick(View view) {

    // Either show the definition of the current word, or if the definition is currently
    // showing, move to the next word.
    switch (mCurrentState) {
        case STATE_HIDDEN:
            showDefinition();
            break;
        case STATE_SHOWN:
            nextWord();
            break;
    }
}

public void nextWord() {

    // Change button text
    mButton.setText(getString(R.string.show_definition));

    mCurrentState = STATE_HIDDEN;

}

public void showDefinition() {

    // Change button text
    mButton.setText(getString(R.string.next_word));

    mCurrentState = STATE_SHOWN;

}

private class ConnectToContentProvider extends AsyncTask<Void,Void, Cursor>{

    @Override
    protected Cursor doInBackground(Void... voids) {
        ContentResolver resolver = getContentResolver();
        return resolver.query(DroidTermsExampleContract.CONTENT_URI,null,null,null,null);
    }

    @Override
    protected void onPostExecute(Cursor cursor) {
        super.onPostExecute(cursor);
        mCursor = cursor;
    }
}

//  (1) Create AsyncTask with the following generic types <Void, Void, Cursor>
//  (2) In the doInBackground method, write the code to access the DroidTermsExample
// provider and return the Cursor object
//  (4) In the onPostExecute method, store the Cursor object in mData

}

johnshea commented 5 years ago

We do not accept pull requests for solutions to the exercises.