我正在尝试从Firebase
数据库中检索数据。顶级节点是消息,子节点是作者和消息。登录后,聊天消息将从数据库中提取。 ChatActivity
呼叫ChatListAdapter
使用以下代码:-
mAdapter = new ChatListAdapter(this, mDatabaseReference);
mChatListView.setAdapter(mAdapter);
这里mChatListView
是RecyclerView
。适配器代码如下:-
public class ChatListAdapter extends RecyclerView.Adapter<ChatListAdapter.ViewHolder> {
private Activity mActivity;
private DatabaseReference mDatabaseReference;
private ArrayList<DataSnapshot> mSnapshotList;
private ChildEventListener mListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mSnapshotList.add(dataSnapshot);
notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
public class ViewHolder extends RecyclerView.ViewHolder {
TextView authorName, body;
public ViewHolder(View view) {
super(view);
authorName = (TextView) itemView.findViewById(R.id.author);
body = (TextView) itemView.findViewById(R.id.message);
}
}
public ChatListAdapter(Activity activity, DatabaseReference ref) {
this.mActivity = activity;
this.mDatabaseReference = ref.child("messages");
mDatabaseReference.addChildEventListener(mListener);
mSnapshotList = new ArrayList<>();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.chat_row, parent, false);
return new ViewHolder(itemView);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
InstantMessage message = getItem(position);
String author = message.getAuthor();
holder.authorName.setText(author);
String msg = message.getMessage();
holder.body.setText(msg);
}
public InstantMessage getItem(int i) {
DataSnapshot snapshot = mSnapshotList.get(i);
return snapshot.getValue(InstantMessage.class);
}
@Override
public int getItemCount() {
return mSnapshotList.size();
}
}
ChatActivity如下:-
public class ChatActivity extends AppCompatActivity {
private RecyclerView mChatListView;
private DatabaseReference mDatabaseReference;
private ChatListAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_chat);
mDatabaseReference = FirebaseDatabase.getInstance().getReference();
mChatListView = (RecyclerView) findViewById(R.id.chat_list_view);
}
@Override
public void onStart(){
super.onStart();
mAdapter = new ChatListAdapter(this, mDatabaseReference);
mChatListView.setAdapter(mAdapter);
}
@Override
public void onStop() {
super.onStop();
mAdapter.cleanUp();
}
}
答案 0 :(得分:1)
通过Map在数组列表中添加数据,示例推详
从Firebase获取数据的以下方法:
public void getUsers(){
final ArrayList arrayList=new ArrayList();
DatabaseReference q1=FirebaseDatabase.getInstance().getReference();
q1.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
collectiouser((Map<String,Object>)dataSnapshot.getValue());
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
toolbar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i=new Intent(ChatPage.this,FriendsProfile.class);
i.putExtra("receiver",receiver);
startActivity(i);
finish();
}
});
}
将数据添加到列表:
private void collectiouser(Map<String, Object> value) {
ArrayList<String> user=new ArrayList<>();
for(Map.Entry<String,Object> entry : value.entrySet()){
Map singleuser=(Map)entry.getValue();
String u=entry.getKey();
String s=singleuser.toString();
user.add(u); // Add Data into List
}
}
答案 1 :(得分:1)
有两种方法可以解决此问题。 This 是一种推荐的方法,您可以使用此方法从RecyclerView
和 {{3 }} 是如何使用FirebaseRecyclerAdapter
。
但是请注意,为了使用这些答案,您需要添加Firebase-UI依赖项。请参阅this的最新版本。
答案 2 :(得分:0)
SharedPreferences