在适配器中获取 android 上下文
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12137680/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting the android context in an adapter
提问by Gautam
In many of the code samples that I find on the internet the context
is obtained in the constructor of an adapter.
在我在互联网上找到的许多代码示例中,它们context
是在适配器的构造函数中获得的。
This context is used to get an inflater
to inflate the views in getView
method.
此上下文用于获取一个inflater
以膨胀getView
方法中的视图。
My Question is why bother getting the context in the constructor when it can easily be obtained like so
我的问题是为什么要在构造函数中获取上下文,因为它可以像这样轻松获得
LayoutInflater inflater;
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(inflater == null){
Context context = parent.getContext();
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
...
...
return convertView;
}
Also is there any reason not to use the above method because it till now I have not faced any problem in using it .
还有什么理由不使用上述方法,因为到目前为止我在使用它时没有遇到任何问题。
采纳答案by Ridcully
Obtaining the Context in the constructor has (at least) three advantages:
在构造函数中获取 Context 具有(至少)三个优点:
- You only do it once, not every time,
getView()
is called. - You can use it for other purposes too, when needed.
- It also works, when
parent
isnull
.
- 你只做一次,而不是每次都
getView()
被调用。 - 需要时,您也可以将其用于其他目的。
- 它也有效,什么时候
parent
是null
。
However, if you don't have any problems with your solution, you might as well stick to it.
但是,如果您的解决方案没有任何问题,您不妨坚持下去。
回答by wyx
Here is an example:
下面是一个例子:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
Holder holder;
if (view == null) {
view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_job, parent, false);
holder = new Holder(view, this);
view.setTag(holder);
} else {
holder = (Holder) view.getTag();
}
holder.parse(getItem(position), position);
return view;
}
public class Holder {
@Bind(R.id.type)
TextView type;
@Bind(R.id.date_time)
TextView dateTime;
@Bind(R.id.grade)
TextView grade;
public Holder(View view) {
ButterKnife.bind(this, view);
}
public void parse(final GetGradeHistoryResponse.GradeHistory item) {
if (item.grade < 0) {
grade.setTextColor(App.getInstance()
.getResources().getColor(R.color.withdraw_status));
grade.setText(String.valueOf(item.grade));
} else {
grade.setTextColor(App.getInstance()
.getResources().getColor(R.color.primary));
grade.setText("+" + String.valueOf(item.grade));
}
type.setText(item.type);
dateTime.setText(item.datetime);
}
}
You can get context by view.getContext() in the Holder
您可以通过以下方式获取上下文 view.getContext() in the Holder
回答by Dmitry Zaytsev
What if someone will create a class that uses BaseAdapter
to store Views somewhere (and, maybe, it will attach them to parent later)? In this case parent
may be null
.
如果有人会创建一个用于BaseAdapter
在某处存储视图的类(并且,它可能稍后会将它们附加到父级)怎么办?在这种情况下parent
可能会null
。
It's not such a big problem, decide for yourself what is better.
这不是什么大问题,自己决定什么更好。
For example:
例如:
public class MockWithAdapter{
private BaseAdapter mAdapter;
public MockWithAdapter(BaseAdapter adapter){
mAdapter = adapter;
}
public List<View> mock(){
int size = mAdapter.getCount();
List<View> views = new ArrayList(size);
for(int i=0; i<size; i++)
views.add(mAdapter.getView(i, null, null));
return views;
}
}
And then you can do with this views whatever you want:
然后你可以随心所欲地使用这个视图:
MockWithAdapter m = new MockWithAdapter(adapter);
ListView lv = new ListView(context);
for(View v : m.mock)
lv.addView(v);
回答by Exel Staderlin
Just simple like this!!
就这么简单!!
class RecentlyAdapter(var data: List<String>) : Adapter<RecentlyAdapter.HomeViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
val inflater = LayoutInflater.from(parent.context)
val view = inflater.inflate(R.layout.card_recently_played, parent, false)
return HomeViewHolder(view)
}
override fun getItemCount(): Int {
return data.size
}
override fun onBindViewHolder(holder: HomeViewHolder, position: Int) {
}
class HomeViewHolder(itemView: View): ViewHolder(itemView) {
init {
itemView.setOnClickListener {
val intent = Intent(itemView.context, MusicPlayerActivity::class.java)
var context = itemView.context
context.startActivity(intent)
}
}
}
}
To get context use itemView.context
要获取上下文使用 itemView.context
回答by Prakash Reddy
Yes but if you need any activity reference like for example dialog alert you cannot use context reference , therefore constructor should receive activity reference from calling Activity/Fragment
是的,但是如果您需要任何活动引用,例如对话框警报,您不能使用上下文引用,因此构造函数应该通过调用 Activity/Fragment 接收活动引用