gpt4 book ai didi

java - Android 自定义 ListView 仅显示 Firebase 数据库中的一项

转载 作者:行者123 更新时间:2023-11-30 00:26:34 25 4
gpt4 key购买 nike

这是一个 Firebase 聊天应用程序,聊天窗口或 ListView 是一个 Activity 的 fragment 这就是 Firebase JSON 树的样子,

Firebase JSON Structure image

我制作了三个类 FirebaseHelper、ChatListAdapter 和 ChatListManager

FirebaseHelper.java

public class FirebaseHelper {
DatabaseReference db;
Boolean saved;
ArrayList<ChatListManager> chatListArr = new ArrayList<>();


public FirebaseHelper(DatabaseReference db) {
this.db = db;
}

public boolean sendChat(String uname, String message) {
if (chatListArr == null) {
saved = false;
} else {
try {
Map<String, Object> keyMap = new HashMap<String, Object>();
String tempKey = db.push().getKey();
db.updateChildren(keyMap);
DatabaseReference chat;
chat = db.child(tempKey);
Map<String, String> chats = new HashMap<String, String>();
chats.put("user", uname);
chats.put("message", message);
chat.setValue(chats);
saved = true;

} catch (DatabaseException e) {
e.printStackTrace();
saved = false;
}
}
return saved;
}

public void receiveChat(DataSnapshot dataSnapshot) {


Iterator i = dataSnapshot.getChildren().iterator();
ChatListManager chatList = new ChatListManager();
while (i.hasNext()) {


chatList.setChatMessage((String) ((DataSnapshot) i.next()).getValue());
chatList.setChatUsername((String) ((DataSnapshot) i.next()).getValue());

chatListArr.clear();
chatListArr.add(chatList);

}
}
public ArrayList<ChatListManager> retrieve ()
{
db.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
receiveChat(dataSnapshot);
}

@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
receiveChat(dataSnapshot);
}

@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}

@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}

@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return chatListArr;
}
}

ChatListAdapeter.java

public class ChatListAdapter extends BaseAdapter {

Context context;
ArrayList<ChatListManager> chatListArr;

public ChatListAdapter(Context c,ArrayList<ChatListManager> chatListArray) {
this.context = c;
this.chatListArr = chatListArray;
}

@Override
public int getCount() {
return chatListArr.size();
}

@Override
public Object getItem(int position) {
return chatListArr.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView= LayoutInflater.from(context).inflate(R.layout.layout_chat_listview_row,parent,false);

}
TextView uname= (TextView) convertView.findViewById(R.id.chatMessageUser);
TextView message= (TextView) convertView.findViewById(R.id.chatMessageText);
final ChatListManager s= (ChatListManager) this.getItem(position);
uname.setText(s.getChatUsername());
message.setText(s.getChatMessage());
return convertView;
}
}

ChatListManager.java

public class ChatListManager  {
private String chatUsername;
private String chatMessage;

public ChatListManager() {

}

public ChatListManager(String chatUsername, String chatMessage){
this.setChatUsername(chatUsername);
this.setChatMessage(chatMessage);

}
public String getChatUsername() {

return chatUsername;
}

public void setChatUsername(String chatUsername) {
this.chatUsername = chatUsername;
}

public String getChatMessage() {
return chatMessage;
}

public void setChatMessage(String chatMessage) {
this.chatMessage = chatMessage;
}
}

ChatFragment.java

public class ChatFragment extends Fragment implements View.OnClickListener {


private EditText chatMessageBox;
private Button chatSendButton;
private DatabaseReference db;
private ListView chatList;
private FirebaseHelper firebaseHelper;
private ChatListAdapter chatListAdapter;
private ChatListManager chatListManager;

public ChatFragment() {

}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View chatView = inflater.inflate(R.layout.fragment_chatroom, container, false);


chatMessageBox = (EditText) chatView.findViewById(R.id.chatMessageBox);
chatSendButton = (Button) chatView.findViewById(R.id.chatSendButton);
chatSendButton.setOnClickListener(this);

db = FirebaseDatabase.getInstance().getReference().child("chat_room");
firebaseHelper = new FirebaseHelper(db);
chatListAdapter = new ChatListAdapter(getActivity().getApplicationContext(), firebaseHelper.retrieve());

chatList = (ListView) chatView.findViewById(R.id.chatList);
chatList.setAdapter(chatListAdapter);





return chatView;
}

@Override
public void onClick(View v) {
if (v == chatSendButton) {

String uname = SharedPrefManager.getInstance(getActivity().getApplicationContext()).getDetails("u_name");
String message = chatMessageBox.getText().toString();
chatListManager = new ChatListManager(uname,message);

firebaseHelper.sendChat(chatListManager.getChatUsername(), chatListManager.getChatMessage());
chatListAdapter.notifyDataSetChanged();

}
}
}

我的问题是,在 ListView 中,它不会从 firebase Db 加载聊天消息,而是显示我发送的消息。就像我附加的 AVD 输出文件一样

我是一名学生,我引用了一些 youtube 视频和 StackOverflow 问题来编写这些代码。

Android AVD output

最佳答案

public void receiveChat(DataSnapshot dataSnapshot) {


Iterator i = dataSnapshot.getChildren().iterator();
ChatListManager chatList = new ChatListManager();
while (i.hasNext()) {


chatList.setChatMessage((String) ((DataSnapshot) i.next()).getValue());
chatList.setChatUsername((String) ((DataSnapshot) i.next()).getValue());

chatListArr.clear();
chatListArr.add(chatList);

}

应该是,

public void receiveChat(DataSnapshot dataSnapshot) {

Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()) {

String message = (String) ((DataSnapshot) i.next()).getValue();
String uname = (String) ((DataSnapshot) i.next()).getValue();
ChatListManager chatList = new ChatListManager(uname,message);

chatListArr.add(chatList);
}
}

经过几个小时的代码我终于找到了 self !!

关于java - Android 自定义 ListView 仅显示 Firebase 数据库中的一项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45263226/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com