我有一个简单的聊天应用程序,我希望在背板上运行ChatActivity,并在从数据库中检索数据时收到通知。但我不明白背景会如何运作。
在最终的CustomAdapter适配器= new CustomAdapter(this,mesajList,firebaseUser); .....部分,消息被接收并显示在屏幕上。我希望这在后台工作
public class SohbetActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private static String PREFS = "PREFS";
private static int SPLASH_TIME_OUT = 400;
TextView tvBaslik;
EditText et_mesaj;
FloatingActionButton buttonGonder;
ListView lv_chatyap;
FirebaseDatabase database;
FloatingActionButton imageButton;
private AdView mAdView2;
DatabaseReference dbRef;
FirebaseStorage firebaseStorage;
StorageReference storageReference;
public static final int RC_SELECT_IMAGE=1;
final FirebaseUser firebaseUser= FirebaseAuth.getInstance().getCurrentUser();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sohbet);
tvBaslik= (TextView) findViewById(R.id.textViewBaslik);
et_mesaj= (EditText) findViewById(R.id.editTextMesaj);
buttonGonder= (FloatingActionButton) findViewById(R.id.buttonMesajGonder);
lv_chatyap= (ListView) findViewById(R.id.listViewChatYap);
lv_chatyap.setDivider(null);
lv_chatyap.setDivider(null);
et_mesaj.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(s.toString().trim().length()>0){
buttonGonder.setEnabled(true);}
else{
buttonGonder.setEnabled(false);}
}
@Override
public void afterTextChanged(Editable s) {}
});
String oda= getIntent().getStringExtra("odaKey");
tvBaslik.setText(oda);
final ArrayList<Mesaj> mesajList=new ArrayList<Mesaj>();
database=FirebaseDatabase.getInstance();
dbRef=database.getReference("chats/"+oda);
firebaseStorage=FirebaseStorage.getInstance();
storageReference=firebaseStorage.getReference("chatImages/"+oda);
buttonGonder.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onClick(View v) {
String gonderen=firebaseUser.getEmail();
String mesaj=et_mesaj.getText().toString();
String zaman = new SimpleDateFormat("dd/MM HH:mm").format(Calendar.getInstance().getTime());
dbRef.push().setValue(new Mesaj(gonderen,mesaj,zaman,null));
et_mesaj.setText("");
}
});
final CustomAdapter adapter=new CustomAdapter(this,mesajList,firebaseUser);
dbRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
mesajList.clear();
for(DataSnapshot ds:dataSnapshot.getChildren()){
mesajList.add(ds.getValue(Mesaj.class));
}
lv_chatyap.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
`
答案 0 :(得分:0)
您需要了解一些事项。
这是流程。