如何在 Android 的自定义适配器中使用全局变量?
我创建了一个扩展 Application 的类,以便在我的应用程序中包含全局变量。我使用它们,
((GlobalVars)getApplicationContext())
但是当我尝试在自定义适配器中访问它时,这不会编译:
The method getApplicationContext() is undefined for the type FriendAdapter
为什么?如何访问适配器中的全局变量?
感谢
GlobalVars
public class GlobalVars extends Application {
private ArrayList<String> selectFriendList = new ArrayList<String>();
public void addSelectedFriend(String fb_id) {
this.selectFriendList.add(fb_id);
}
public void removeSelectedFriend(String fb_id) {
this.selectFriendList.remove(fb_id);
}
public boolean isFriendSelected(String fb_id) {
return this.selectFriendList.contains(fb_id);
}
}
FriendAdapter
public class FriendAdapter extends ArrayAdapter<Friend> implements OnClickListener {
private ArrayList<Friend> items;
public FriendAdapter(Context context, int textViewResourceId,
ArrayList<Friend> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contact_row, null);
}
Friend f = items.get(position);
if (f != null) {
String name = f.name;
TextView tt = (TextView) v.findViewById(R.id.contact_name);
tt.setText(name);
CheckBox bCheck = (CheckBox) v.findViewById(R.id.checkbox);
bCheck.setTag(f.fb_id);
if (((GlobalVars)getApplicationContext()).isFriendSelected(f.fb_id))
bCheck.setChecked(true);
bCheck.setOnClickListener(this);
}
return v;
}
@Override
public void onClick(View v) {
...
}
}
I created a class extending Application to have global variables in my app. I use them using
((GlobalVars)getApplicationContext())
but this does not compile when I'm trying to access it in my custom adapter:
The method getApplicationContext() is undefined for the type FriendAdapter
Why? How can I access my global vars in my adapter?
Thanks
GlobalVars
public class GlobalVars extends Application {
private ArrayList<String> selectFriendList = new ArrayList<String>();
public void addSelectedFriend(String fb_id) {
this.selectFriendList.add(fb_id);
}
public void removeSelectedFriend(String fb_id) {
this.selectFriendList.remove(fb_id);
}
public boolean isFriendSelected(String fb_id) {
return this.selectFriendList.contains(fb_id);
}
}
FriendAdapter
public class FriendAdapter extends ArrayAdapter<Friend> implements OnClickListener {
private ArrayList<Friend> items;
public FriendAdapter(Context context, int textViewResourceId,
ArrayList<Friend> items) {
super(context, textViewResourceId, items);
this.items = items;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.contact_row, null);
}
Friend f = items.get(position);
if (f != null) {
String name = f.name;
TextView tt = (TextView) v.findViewById(R.id.contact_name);
tt.setText(name);
CheckBox bCheck = (CheckBox) v.findViewById(R.id.checkbox);
bCheck.setTag(f.fb_id);
if (((GlobalVars)getApplicationContext()).isFriendSelected(f.fb_id))
bCheck.setChecked(true);
bCheck.setOnClickListener(this);
}
return v;
}
@Override
public void onClick(View v) {
...
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 Adapter 类中保留一个引用上下文的私有成员变量:
然后从上下文变量中获取应用程序上下文的句柄:
Keep a private member variable referencing the context within your Adapter class:
Then get a handle to your application context from the context variable:
ArrayAdapter 不是上下文,因此您无法从此类获取 Application。但是你可以在Activity中编写ArrayAdapter的实现(它是上下文)。像这样,当你初始化适配器时:
ArrayAdapter isn't a context, so you can't get Application from this class. But you can write ArrayAdapter's implementation in Activity(it is context). Something like this, when you initialize adapter: