Question about the parameter “Context” in ArrayAdapter constructor












0















First, Here is my code



class MergedView (context : Context,attributeSet: AttributeSet?) : LinearLayout(context,attributeSet) {

var myCalendar : HorizontalCalendar? = null
var scheduleList : ListView?= null

init {

val infService = Context.LAYOUT_INFLATER_SERVICE
val li = context.getSystemService(infService) as LayoutInflater
val v = li.inflate(R.layout.calendar_sequence, this, false)


addView(v)

val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -5)


val endDate = Calendar.getInstance()
endDate.add(Calendar.MONTH, 5)

myCalendar = HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(5)
.build()

myCalendar?.setCalendarListener(object : HorizontalCalendarListener() {
override fun onDateSelected(date: Calendar, position: Int) {

}
})

val texts = arrayOf("None", "None","None")

val adapter =
ArrayAdapter(context,android.R.layout.simple_list_item_1,texts)

scheduleList = findViewById(R.id.testList)

scheduleList?.setAdapter(adapter)

}


}



I am making custom view called merged View which has ListView and HorizontalCalendarView(another custom view)



So, on strictly saying, according to android development document, one of the constructors of ArrayAdapter is

"ArrayAdapter(Context context, int resource, int textViewResourceId, T
objects)". Only one thing I can't understand is Context parameter. According to my understanding, context parameter is a context(Activity) which adapter view is attached. but, then, if adapter View is attatched on custom View (in my case Merged View), then we can say, context is not a thing that adapter view is attached. So I think passing a context to ArrayAdapter constructor is not right(I think passing a custom view sounds more logical).



So here is my question
1. what is the thing I am misunderstanding??
2. what is the correct notion??










share|improve this question























  • The Context is not necessarily what the AdapterView is "attached" to. In the ArrayAdapter, it's just used for access to resources; e.g., an appropriate LayoutInflater.

    – Mike M.
    Nov 25 '18 at 8:45













  • So, in my case, context is not the thing ListView is attached to, instead Merged View is the thing ListView is attached to??

    – 백근우
    Nov 25 '18 at 8:52













  • In terms of the View hierarchy, yes, your ListView is attached to your MergedView (though there may be other ViewGroups in between). The Context is just something ArrayAdapter needs so it knows how to inflate and style its own item Views internally.

    – Mike M.
    Nov 25 '18 at 8:56













  • so... um.. the parameter context must be the activity that view(ListVIew's itemview) is appeared??

    – 백근우
    Nov 25 '18 at 9:11











  • It doesn't have to be the Activity, and it may very well not be, depending on how your MergedView is instantiated. It might be a wrapper around your Activity, or some other Context. Whenever you instantiate an ArrayAdapter inside an Activity, though, you just pass the Activity, because it is a Context that has all of the styling information it needs to make the item Views look like your other Views.

    – Mike M.
    Nov 25 '18 at 9:19


















0















First, Here is my code



class MergedView (context : Context,attributeSet: AttributeSet?) : LinearLayout(context,attributeSet) {

var myCalendar : HorizontalCalendar? = null
var scheduleList : ListView?= null

init {

val infService = Context.LAYOUT_INFLATER_SERVICE
val li = context.getSystemService(infService) as LayoutInflater
val v = li.inflate(R.layout.calendar_sequence, this, false)


addView(v)

val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -5)


val endDate = Calendar.getInstance()
endDate.add(Calendar.MONTH, 5)

myCalendar = HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(5)
.build()

myCalendar?.setCalendarListener(object : HorizontalCalendarListener() {
override fun onDateSelected(date: Calendar, position: Int) {

}
})

val texts = arrayOf("None", "None","None")

val adapter =
ArrayAdapter(context,android.R.layout.simple_list_item_1,texts)

scheduleList = findViewById(R.id.testList)

scheduleList?.setAdapter(adapter)

}


}



I am making custom view called merged View which has ListView and HorizontalCalendarView(another custom view)



So, on strictly saying, according to android development document, one of the constructors of ArrayAdapter is

"ArrayAdapter(Context context, int resource, int textViewResourceId, T
objects)". Only one thing I can't understand is Context parameter. According to my understanding, context parameter is a context(Activity) which adapter view is attached. but, then, if adapter View is attatched on custom View (in my case Merged View), then we can say, context is not a thing that adapter view is attached. So I think passing a context to ArrayAdapter constructor is not right(I think passing a custom view sounds more logical).



So here is my question
1. what is the thing I am misunderstanding??
2. what is the correct notion??










share|improve this question























  • The Context is not necessarily what the AdapterView is "attached" to. In the ArrayAdapter, it's just used for access to resources; e.g., an appropriate LayoutInflater.

    – Mike M.
    Nov 25 '18 at 8:45













  • So, in my case, context is not the thing ListView is attached to, instead Merged View is the thing ListView is attached to??

    – 백근우
    Nov 25 '18 at 8:52













  • In terms of the View hierarchy, yes, your ListView is attached to your MergedView (though there may be other ViewGroups in between). The Context is just something ArrayAdapter needs so it knows how to inflate and style its own item Views internally.

    – Mike M.
    Nov 25 '18 at 8:56













  • so... um.. the parameter context must be the activity that view(ListVIew's itemview) is appeared??

    – 백근우
    Nov 25 '18 at 9:11











  • It doesn't have to be the Activity, and it may very well not be, depending on how your MergedView is instantiated. It might be a wrapper around your Activity, or some other Context. Whenever you instantiate an ArrayAdapter inside an Activity, though, you just pass the Activity, because it is a Context that has all of the styling information it needs to make the item Views look like your other Views.

    – Mike M.
    Nov 25 '18 at 9:19
















0












0








0








First, Here is my code



class MergedView (context : Context,attributeSet: AttributeSet?) : LinearLayout(context,attributeSet) {

var myCalendar : HorizontalCalendar? = null
var scheduleList : ListView?= null

init {

val infService = Context.LAYOUT_INFLATER_SERVICE
val li = context.getSystemService(infService) as LayoutInflater
val v = li.inflate(R.layout.calendar_sequence, this, false)


addView(v)

val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -5)


val endDate = Calendar.getInstance()
endDate.add(Calendar.MONTH, 5)

myCalendar = HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(5)
.build()

myCalendar?.setCalendarListener(object : HorizontalCalendarListener() {
override fun onDateSelected(date: Calendar, position: Int) {

}
})

val texts = arrayOf("None", "None","None")

val adapter =
ArrayAdapter(context,android.R.layout.simple_list_item_1,texts)

scheduleList = findViewById(R.id.testList)

scheduleList?.setAdapter(adapter)

}


}



I am making custom view called merged View which has ListView and HorizontalCalendarView(another custom view)



So, on strictly saying, according to android development document, one of the constructors of ArrayAdapter is

"ArrayAdapter(Context context, int resource, int textViewResourceId, T
objects)". Only one thing I can't understand is Context parameter. According to my understanding, context parameter is a context(Activity) which adapter view is attached. but, then, if adapter View is attatched on custom View (in my case Merged View), then we can say, context is not a thing that adapter view is attached. So I think passing a context to ArrayAdapter constructor is not right(I think passing a custom view sounds more logical).



So here is my question
1. what is the thing I am misunderstanding??
2. what is the correct notion??










share|improve this question














First, Here is my code



class MergedView (context : Context,attributeSet: AttributeSet?) : LinearLayout(context,attributeSet) {

var myCalendar : HorizontalCalendar? = null
var scheduleList : ListView?= null

init {

val infService = Context.LAYOUT_INFLATER_SERVICE
val li = context.getSystemService(infService) as LayoutInflater
val v = li.inflate(R.layout.calendar_sequence, this, false)


addView(v)

val startDate = Calendar.getInstance()
startDate.add(Calendar.MONTH, -5)


val endDate = Calendar.getInstance()
endDate.add(Calendar.MONTH, 5)

myCalendar = HorizontalCalendar.Builder(this, R.id.calendarView)
.range(startDate, endDate)
.datesNumberOnScreen(5)
.build()

myCalendar?.setCalendarListener(object : HorizontalCalendarListener() {
override fun onDateSelected(date: Calendar, position: Int) {

}
})

val texts = arrayOf("None", "None","None")

val adapter =
ArrayAdapter(context,android.R.layout.simple_list_item_1,texts)

scheduleList = findViewById(R.id.testList)

scheduleList?.setAdapter(adapter)

}


}



I am making custom view called merged View which has ListView and HorizontalCalendarView(another custom view)



So, on strictly saying, according to android development document, one of the constructors of ArrayAdapter is

"ArrayAdapter(Context context, int resource, int textViewResourceId, T
objects)". Only one thing I can't understand is Context parameter. According to my understanding, context parameter is a context(Activity) which adapter view is attached. but, then, if adapter View is attatched on custom View (in my case Merged View), then we can say, context is not a thing that adapter view is attached. So I think passing a context to ArrayAdapter constructor is not right(I think passing a custom view sounds more logical).



So here is my question
1. what is the thing I am misunderstanding??
2. what is the correct notion??







android android-arrayadapter






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 8:40









백근우백근우

34




34













  • The Context is not necessarily what the AdapterView is "attached" to. In the ArrayAdapter, it's just used for access to resources; e.g., an appropriate LayoutInflater.

    – Mike M.
    Nov 25 '18 at 8:45













  • So, in my case, context is not the thing ListView is attached to, instead Merged View is the thing ListView is attached to??

    – 백근우
    Nov 25 '18 at 8:52













  • In terms of the View hierarchy, yes, your ListView is attached to your MergedView (though there may be other ViewGroups in between). The Context is just something ArrayAdapter needs so it knows how to inflate and style its own item Views internally.

    – Mike M.
    Nov 25 '18 at 8:56













  • so... um.. the parameter context must be the activity that view(ListVIew's itemview) is appeared??

    – 백근우
    Nov 25 '18 at 9:11











  • It doesn't have to be the Activity, and it may very well not be, depending on how your MergedView is instantiated. It might be a wrapper around your Activity, or some other Context. Whenever you instantiate an ArrayAdapter inside an Activity, though, you just pass the Activity, because it is a Context that has all of the styling information it needs to make the item Views look like your other Views.

    – Mike M.
    Nov 25 '18 at 9:19





















  • The Context is not necessarily what the AdapterView is "attached" to. In the ArrayAdapter, it's just used for access to resources; e.g., an appropriate LayoutInflater.

    – Mike M.
    Nov 25 '18 at 8:45













  • So, in my case, context is not the thing ListView is attached to, instead Merged View is the thing ListView is attached to??

    – 백근우
    Nov 25 '18 at 8:52













  • In terms of the View hierarchy, yes, your ListView is attached to your MergedView (though there may be other ViewGroups in between). The Context is just something ArrayAdapter needs so it knows how to inflate and style its own item Views internally.

    – Mike M.
    Nov 25 '18 at 8:56













  • so... um.. the parameter context must be the activity that view(ListVIew's itemview) is appeared??

    – 백근우
    Nov 25 '18 at 9:11











  • It doesn't have to be the Activity, and it may very well not be, depending on how your MergedView is instantiated. It might be a wrapper around your Activity, or some other Context. Whenever you instantiate an ArrayAdapter inside an Activity, though, you just pass the Activity, because it is a Context that has all of the styling information it needs to make the item Views look like your other Views.

    – Mike M.
    Nov 25 '18 at 9:19



















The Context is not necessarily what the AdapterView is "attached" to. In the ArrayAdapter, it's just used for access to resources; e.g., an appropriate LayoutInflater.

– Mike M.
Nov 25 '18 at 8:45







The Context is not necessarily what the AdapterView is "attached" to. In the ArrayAdapter, it's just used for access to resources; e.g., an appropriate LayoutInflater.

– Mike M.
Nov 25 '18 at 8:45















So, in my case, context is not the thing ListView is attached to, instead Merged View is the thing ListView is attached to??

– 백근우
Nov 25 '18 at 8:52







So, in my case, context is not the thing ListView is attached to, instead Merged View is the thing ListView is attached to??

– 백근우
Nov 25 '18 at 8:52















In terms of the View hierarchy, yes, your ListView is attached to your MergedView (though there may be other ViewGroups in between). The Context is just something ArrayAdapter needs so it knows how to inflate and style its own item Views internally.

– Mike M.
Nov 25 '18 at 8:56







In terms of the View hierarchy, yes, your ListView is attached to your MergedView (though there may be other ViewGroups in between). The Context is just something ArrayAdapter needs so it knows how to inflate and style its own item Views internally.

– Mike M.
Nov 25 '18 at 8:56















so... um.. the parameter context must be the activity that view(ListVIew's itemview) is appeared??

– 백근우
Nov 25 '18 at 9:11





so... um.. the parameter context must be the activity that view(ListVIew's itemview) is appeared??

– 백근우
Nov 25 '18 at 9:11













It doesn't have to be the Activity, and it may very well not be, depending on how your MergedView is instantiated. It might be a wrapper around your Activity, or some other Context. Whenever you instantiate an ArrayAdapter inside an Activity, though, you just pass the Activity, because it is a Context that has all of the styling information it needs to make the item Views look like your other Views.

– Mike M.
Nov 25 '18 at 9:19







It doesn't have to be the Activity, and it may very well not be, depending on how your MergedView is instantiated. It might be a wrapper around your Activity, or some other Context. Whenever you instantiate an ArrayAdapter inside an Activity, though, you just pass the Activity, because it is a Context that has all of the styling information it needs to make the item Views look like your other Views.

– Mike M.
Nov 25 '18 at 9:19














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465904%2fquestion-about-the-parameter-context-in-arrayadapter-constructor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53465904%2fquestion-about-the-parameter-context-in-arrayadapter-constructor%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Costa Masnaga