droidtermsexample-release.zip


ContentProvier_BasicSetting.zip

ContentProvider_Completed.zip




** droidtermsexample apk파일을 다운로드 및 앱 실행 후, 코드를 작성해야 한다.


0. Result


     




1. Get permission to use the ContentProvider.


AndroidManifest.xml

1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tistory.qlyh8.contentprovider">
 
    <!-- DroidTermsExample 컨텐트 프로바이더로부터 읽기 권한만 요청 -->
    <uses-permission android:name="com.example.udacity.droidtermsexample.TERMS_READ"/>
 
    <application
        ...
    </application>
cs




2. Using AsyncTask, Get the ContentResolver and 

   Identify the data you are manipulating to create a URI.


데이터베이스 작업은 오래 걸리는 작업이기 때문에,

메인 스레드가 아닌 다른 스레드에서 처리하기 위해 AsyncTask 이용한다.


2-1. Create an AsyncTask with the following generic types <Void, Void, Cursor>.

2-2. In the doInBackground method, 

      Write the code to access the DroidTermsExample provider and Return the Cursor object.

2-3. Create an instance variable Cursor mData and

      In the onPostExecute method, store the Cursor object in mData.

2-4. Create and execute the AsyncTask in onCreate.


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package com.tistory.qlyh8.contentprovider;
 
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import com.udacity.example.droidtermsprovider.DroidTermsExampleContract;
 
public class MainActivity extends AppCompatActivity {
 
    private int mCurrentState;  // 현재 상태
    private final int STATE_HIDDEN = 0// 단어 정의가 감줘져 있는 상태
    private final int STATE_SHOWN = 1;  // 단어 정의가 보여진 상태
 
    private Cursor mData;   // 컨텐트 프로바이더의 데이터
 
    private Button mButton;
    private TextView mWordTextView, mDefinitionTextView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        mButton = findViewById(R.id.button_next);
        mWordTextView = findViewById(R.id.text_view_word);
        mDefinitionTextView = findViewById(R.id.text_view_definition);
 
        // AsyncTask 를 생성하고 실행한다.
        new WordFetchTask().execute();
    }
 
    // 현재 단어 정의 보이기 / 다음 단어로 이동하기
    public void onButtonClick(View view) {...}
 
    // 현재 단어 정의 보이기
    public void showDefinition() {...}
 
    // 다음 단어로 이동하기
    public void nextWord() {...}
 
    public class WordFetchTask extends AsyncTask<Void, Void, Cursor>{
 
        @Override
        protected Cursor doInBackground(Void... voids) {
            // 컨텐트 리졸버를 가져온다.
            ContentResolver resolver = getContentResolver();
 
            // Contract 클래스로부터 리졸브의 쿼리 메서드를 호출한다.
            Cursor cursor = resolver.query(DroidTermsExampleContract.CONTENT_URI,
                    nullnullnullnull);
 
            return cursor;
        }
 
        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);
            //MainActivity 를 위해 데이터를 세팅한다.
            mData = cursor;
            // 초기화
            nextWord();
        }
    }
}
 
cs




3. Display the information in the UI.


3-1. Get the column index, in the Cursor, of each piece of data.

3-2. Don't try to do showDefinition() and nextWord() if the cursor hasn't been set yet.

3-3Get the next word.

3-4. Close the cursor.


MainActivity.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package com.tistory.qlyh8.contentprovider;
 
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
 
import com.udacity.example.droidtermsprovider.DroidTermsExampleContract;
 
public class MainActivity extends AppCompatActivity {
 
    private int mCurrentState;  // 현재 상태
    private final int STATE_HIDDEN = 0// 단어 정의가 감줘져 있는 상태
    private final int STATE_SHOWN = 1;  // 단어 정의가 보여진 상태
 
    private Cursor mData;   // 컨텐트 프로바이더의 데이터
    private int mDefCol, mWordCol;  // 커서의 단어 정의 및 단어 열 인덱스
 
    private Button mButton;
    private TextView mWordTextView, mDefinitionTextView;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {...}
 
    // 현재 단어 정의 보이기 / 다음 단어로 이동하기
    public void onButtonClick(View view) {...}
 
    // 현재 단어 정의 보이기
    public void showDefinition() {
        if(mData != null){
            mButton.setText(getString(R.string.next_word));
            mDefinitionTextView.setVisibility(View.VISIBLE);
            mCurrentState = STATE_SHOWN;
        }
    }
 
    // 다음 단어로 이동하기
    public void nextWord() {
        if(mData != null) {
            // 커서의 다음 위치로 이동한다. 마지막 위치면 첫 번째 위치로 이동한다.
            if(!mData.moveToNext())
                mData.moveToFirst();
 
            mButton.setText(getString(R.string.show_definition));
            mDefinitionTextView.setVisibility(View.INVISIBLE);
 
            // 다음 단어를 가져온다.
            mWordTextView.setText(mData.getString(mWordCol));
            mDefinitionTextView.setText(mData.getString(mDefCol));
 
            mCurrentState = STATE_HIDDEN;
        }
    }
 
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // 커서를 닫는다.
        mData.close();
    }
 
    public class WordFetchTask extends AsyncTask<Void, Void, Cursor>{
 
        @Override
        protected Cursor doInBackground(Void... voids) {...}
 
        @Override
        protected void onPostExecute(Cursor cursor) {
            super.onPostExecute(cursor);
            //MainActivity 를 위해 데이터를 세팅한다.
            mData = cursor;
            // 각 데이터의 커서에서 열 인덱스를 가져온다.
            mDefCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_DEFINITION);
            mWordCol = mData.getColumnIndex(DroidTermsExampleContract.COLUMN_WORD);
            // 초기화
            nextWord();
        }
    }
}
 
cs





Android Course of Udacity - Lesson 8

+ Recent posts