回答編集履歴

1

具体的な呼び出し方を明記しました

2017/09/13 01:07

投稿

hiramekun
hiramekun

スコア428

test CHANGED
@@ -11,3 +11,55 @@
11
11
  こちらが`getApplicationContext()`の[ドキュメント](https://developer.android.com/reference/android/content/Context.html#getApplicationContext())ですが、以下のように`Activity`や`Fragment`のライフサイクルに関わらず、アプリ自体のプロセスに関わるものにだけ使ってねと書いてあります。
12
12
 
13
13
  > This generally should only be used if you need a Context whose lifecycle is separate from the current context, that is tied to the lifetime of the process rather than the current component.
14
+
15
+
16
+
17
+ ## Adapter側
18
+
19
+ ```java
20
+
21
+ class MyAdapter extends RecyclerView.Adapter {
22
+
23
+ private Context context;
24
+
25
+
26
+
27
+ public MyAdapter(Context context) {
28
+
29
+ this.context = context;
30
+
31
+ }
32
+
33
+
34
+
35
+ @Override
36
+
37
+ public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
38
+
39
+ // contextを使う
40
+
41
+ }
42
+
43
+ }
44
+
45
+ ```
46
+
47
+ ## 呼び出し側
48
+
49
+ ```java
50
+
51
+ class MyActivity extends AppCompatActivity {
52
+
53
+ @Override
54
+
55
+ protected void onCreate(@Nullable Bundle savedInstanceState) {
56
+
57
+ super.onCreate(savedInstanceState);
58
+
59
+ MyAdapter adapter = new MyAdapter(this);
60
+
61
+ }
62
+
63
+ }
64
+
65
+ ```