I want to use it in my array adapter. When I put this
in a sub-activity to create an adapter it does not work.
相关问题
- How can I create this custom Bottom Navigation on
- Bottom Navigation View gets Shrink Down
- How to make that the snackbar action button be sho
- Listening to outgoing sms not working android
- How to create Circular view on android wear?
相关文章
- android开发 怎么把图片放入drawable的文件夹下
- android上如何获取/storage/emulated/下的文件列表
- androidStudio有个箭头不认识
- SQLite不能创建表
- Windows - Android SDK manager not listing any plat
- Animate Recycler View grid when number of columns
- Why is the app closing suddenly without showing an
- Android OverlayItem.setMarker(): Change the marker
You can get context a few ways:
By the Activity, using
Your_Activity_Name.this
By the application, using
getApplicationContext()
By the View, using
Your_View.getContext()
The only one I would not recommend is using
getBaseContext()
. If you need something universal, have a public static variable in your main activity and assign the application context to it when your app starts. This way you can always callYour_Activity.your_context_variable
Here we take variable context :
Make the constructor of adapter take another input like:
This is a click listener in which we used context:
getApplicationContext()
can get the context valueYou can use
getBaseContext()
but this isnot a local context
.Besides the correct previous answers, you may want to think about refactoring your code if you've come to the point where you need to access "your" context from a sub activity. When you create a sub activity (ie: startActivityForResult) you are truly waiting for a result, not for an action in the caller activity. Then, when the sub activity has finished (and you have the result of its calculations), you can access your context in a proper way. It just doesn't seem fine that the subactivity is aware of its creator, not to mention interact with it.