android - 如果我无法获得上下文,我怎么能像 picasso 一样使用?

标签 android firebase firebase-realtime-database picasso

我使用 firbase-ui 显示一个 recyclerView ,它扩展了 Activity ,就像这样:

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<ChatItem, ExpenseHolder>
                (ChatItem.class, R.layout.chat_room_list, ExpenseHolder.class, secondRoot) {
            @Override
            protected void populateViewHolder(ExpenseHolder viewHolder, ChatItem chatItem, int position) {
                viewHolder.setValues(chatItem);
            }
        };
        recyclerChat.scrollToPosition(adapter.getItemCount() - 1);//just let the latest data below
        //recyclerChat.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));//增加橫線
        recyclerChat.setAdapter(adapter);

        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerChat.setLayoutManager(manager);

这是我的 ExpenseHolder ,我发现它必须是静态的,否则会导致崩溃

    public static class ExpenseHolder extends RecyclerView.ViewHolder {
            private final TextView textRightMsg;
            private final TextView textLeftMsg;
            private final TextView rightTime;
            private final ImageView rightImage;

            public ExpenseHolder(View itemView) {
                super(itemView);

                textRightMsg = (TextView) itemView.findViewById(R.id.textRight);
                textLeftMsg = (TextView) itemView.findViewById(R.id.textLeft);
                rightTime = (TextView) itemView.findViewById(R.id.rightTime);
                rightImage = (ImageView) itemView.findViewById(R.id.rightImage);
            }

            public void setValues(ChatItem chatItem) {
                String myName = chatItem.getName();
                if (myName.equals(userName)) {
                    textRightMsg.setText(chatItem.getMsg() + "(" + chatItem.getName() + ")");
                    rightTime.setText(chatItem.getTime());
// I don't know how to get context in this class                    
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(rightImage);
                } else {
                    textLeftMsg.setText(chatItem.getMsg() + "(" + chatItem.getName() + ")");
                }
            }
        }

当我想使用 Picaso 设置图像时,我发现我无法获取 contex

我尝试设置全局 privat static Context contex; ,它显示 error context is null 。

这是我要显示的资源 enter image description here

如何在静态环境中显示资源为 url 的图像?

最佳答案

试试这个方法..

Picasso.with(rightImage.getContext()).load("http://i.imgur.com/DvpvklR.png").into(rightImage);

关于android - 如果我无法获得上下文,我怎么能像 picasso 一样使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42826767/

相关文章:

android - Gradle找不到kotlin编译器版本

firebase - Firebase 推送通知传送缓慢

java - 如何从 firebase 数据库中洗牌值以及如何显示 recyclerView?

ios - Firebase 观察者两次读取数据

javascript - Firebase DB snapshot.val() 返回 null

java - 纹理未绘制在 Box2d Body 上

android - 更改 Eclipse 的默认输出 bin 文件夹

android - 是否可以通过编程方式启用 "Show layout bounds"开发人员选项?

java - 使用 FirebaseRecyclerAdapter 的 RecyclerView 不显示项目 - FirebaseUI (3.1.3)

android - Android 客户端上的 Firestore 集合组 PERMISSION_DENIED