如何在 Android 的自定义适配器中使用全局变量?

发布于 2024-10-20 06:05:35 字数 2142 浏览 6 评论 0原文

我创建了一个扩展 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

百善笑为先 2024-10-27 06:05:35

在 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;
}

然后从上下文变量中获取应用程序上下文的句柄:

if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(f.fb_id))
            bCheck.setChecked(true);

Keep a private member variable referencing the context within your Adapter class:

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:

if (((GlobalVars) mContext.getApplicationContext()).isFriendSelected(f.fb_id))
            bCheck.setChecked(true);
紫竹語嫣☆ 2024-10-27 06:05:35

ArrayAdapter 不是上下文,因此您无法从此类获取 Application。但是你可以在Activity中编写ArrayAdapter的实现(它是上下文)。像这样,当你初始化适配器时:

    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;
                }
            };

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:

    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;
                }
            };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文