Mor*_*ton 1 android firebase picasso firebase-realtime-database
我使用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);
Run Code Online (Sandbox Code Playgroud)
这是我的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() + ")");
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我想使用Picaso设置图像时,我发现我无法获得 contex
我尝试设置全局privat static Context contex;,它显示错误上下文为空的崩溃.
如何在静态环境中显示资源为url的图像?
试试这种方式..
Picasso.with(rightImage.getContext()).load("http://i.imgur.com/DvpvklR.png").into(rightImage);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
63 次 |
| 最近记录: |