[英]How can I use my global vars in a custom adapter in Android?
I created a class extending Application to have global variables in my app. 我创建了一个扩展Application的类,以在我的应用程序中具有全局变量。 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 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) {
...
}
}
Keep a private member variable referencing the context within your Adapter class: 在Adapter类中保留一个引用上下文的私有成员变量:
private Context mContext;
private ArrayList<Friend> items;
public FriendAdapter(Context context, int textViewResourceId,
ArrayList<Friend> items) {
super(context, textViewResourceId, items);
this.items = items;
this.mContext = context;
}
Then get a handle to your application context from the context variable: 然后从context变量获取应用程序上下文的句柄:
if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(f.fb_id))
bCheck.setChecked(true);
ArrayAdapter isn't a context, so you can't get Application from this class. ArrayAdapter不是上下文,因此您不能从此类获得Application。 But you can write ArrayAdapter's implementation in Activity(it is context).
但是您可以在Activity(它是上下文)中编写ArrayAdapter的实现。 Something like this, when you initialize adapter:
初始化适配器时,类似以下内容:
ArrayAdapter<String> A = new ArrayAdapter<String>(this, R.layout.item_view,
new String[]{"1", "2"}){
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//get view implementation
return convertView;
}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.