我试图在我的应用程序中构建一个简单的聊天活动,其中所有需求都在一个地方,适配器,属性类,视图类和主要活动所有都要收集,此时活动将一个字符串行发送到firebase完全没有问题,但数据提取的代码不起作用,我使用 ChildEeventListiner 来接收一个列表视图中的单个字符串行,((((我猜DataSnapShot没有接收任何数据))))) PS:我在同一个应用程序中使用相同的代码,但是保存更复杂的数据并且工作正常,我不确定这里缺少什么,希望有人能弄清楚这里有什么问题,问我是否有错你需要的任何其他数据。
public class MainActivity extends Activity {
//---------------------attributes Class ==> 1 string input-------------------
public class classChat {
String messageInPut;
public classChat(String messageInPut) {
this.messageInPut = messageInPut;
}
public String getMessageInPut() {
return messageInPut;
}
}
//---------------------------------------------------------------------------
FirebaseUser user;
DatabaseReference databaseChatView;
String msg;
classChat chats;
public static chatAdapter adapter;
ArrayList<classChat> bidChatdata;
ListView chatinstnc;
ImageView sendButton;
EditText messageArea;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//user = FirebaseAuth.getInstance().getCurrentUser();
databaseChatView = FirebaseDatabase.getInstance().getReference("Chat");
chatinstnc = (ListView) findViewById(R.id.chatinstnc);
sendButton = (ImageView)findViewById(R.id.sendButton);
messageArea = (EditText)findViewById(R.id.messageArea);
bidChatdata = new ArrayList<>();
//------------------sending to firebase------------------------------------
sendButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String messageId = databaseChatView.push().getKey();
String msg = messageArea.getText().toString().trim();
databaseChatView.child(messageId).setValue(msg);
Toast.makeText(MainActivity.this, "chat: "+ chats, Toast.LENGTH_SHORT).show();
}
});
//--------------------List Item Click-------------------------------------
/* chatinstnc.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long messageId) {classChat dataModel = bidChatdata.get(position);}
});*/
//-------------Receiving Data From Firebase------------------------------
ChildEventListener childEventListener = new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
bidChatdata.clear();
for (DataSnapshot chatSnapshot : dataSnapshot.getChildren()) {
chats = chatSnapshot.getValue(classChat.class);
bidChatdata.add(chats);
}
adapter = new chatAdapter(MainActivity.this, bidChatdata);
chatinstnc.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
bidChatdata.clear();
for (DataSnapshot chatSnapshot : dataSnapshot.getChildren()) {
chats = chatSnapshot.getValue(classChat.class);
bidChatdata.add(chats);
}
adapter = new chatAdapter(MainActivity.this, bidChatdata);
chatinstnc.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
};
databaseChatView.addChildEventListener(childEventListener);
}
//---------------adapter class--------------------------------------------
public class chatAdapter extends ArrayAdapter<classChat>{
ArrayList<classChat> chat;
Context Context;
public class ViewHolder {
TextView msg1;
}
public chatAdapter(Context context, ArrayList<classChat> chat) {
super(context, R.layout.list_view_items, chat);
this.chat = chat;
this.Context = context;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final classChat chatdata = getItem(position);
ViewHolder viewHolder;
final View result;
if (convertView == null) {
viewHolder = new ViewHolder();
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(R.layout.list_view_items, parent, false);
viewHolder.msg1 = (TextView)convertView.findViewById(R.id.msg1);
result = convertView;
convertView.setTag(viewHolder);
}else{
viewHolder = (ViewHolder) convertView.getTag();
result = convertView;
}
viewHolder.msg1.setText(chatdata.getMessageInPut());
return convertView;
}
}
}