活动显示已注册用户的列表。触摸任何用户后,它会与该用户聊天,但问题是它在每个用户中显示相同的消息。 MainActivity.java
public class MainActivity extends BaseActivity {
ListView usersList;
private DatabaseReference mDatabaseUser;
ArrayList<String> list=new ArrayList<>();
private DatabaseReference root = FirebaseDatabase.getInstance().getReference().getRoot();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDatabaseUser = FirebaseDatabase.getInstance().getReference("users");
usersList = (ListView)findViewById(R.id.usersList);
final ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,list);
usersList.setAdapter(adapter);
usersList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String s = usersList.getItemAtPosition(i).toString();
Intent intent = new Intent(getApplicationContext(),Chat_Room.class);
intent.putExtra("user_name",s);
startActivity(intent);
}
});
mDatabaseUser.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
/*list.add((String) dataSnapshot.getValue(String.class));*/
User user = dataSnapshot.getValue(User.class);
// Check for null
if (user == null) {
// Log.e(TAG, "User data is null!");
return;
}
list.add(user.username);
adapter.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) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.action_logout:
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(this, SignActivity.class));
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Chat_Room.java
public class Chat_Room extends AppCompatActivity {
private static final String TAG = MainActivity.class.getName();
private EditText metText;
private Button mbtSent;
private DatabaseReference mFirebaseRef;
private List<Chat> mChats;
private RecyclerView mRecyclerView;
private ChatAdapter mAdapter;
private String mId;
@SuppressLint("HardwareIds")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat__room);
String user_name = getIntent().getStringExtra("user_name");
setTitle(user_name);
metText = (EditText) findViewById(R.id.etText);
mbtSent = (Button) findViewById(R.id.btSent);
mRecyclerView = (RecyclerView) findViewById(R.id.rvChat);
mChats = new ArrayList<>();
mId = Settings.Secure.getString(this.getContentResolver(), Settings.Secure.ANDROID_ID);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
//mRecyclerView.setItemAnimator(new SlideInOutLeftItemAnimator(mRecyclerView));
mAdapter = new ChatAdapter(mChats, mId);
mRecyclerView.setAdapter(mAdapter);
/**
* Firebase - Inicialize
*/
FirebaseDatabase database = FirebaseDatabase.getInstance();
mFirebaseRef = database.getReference("message");
mbtSent.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message = metText.getText().toString();
if (!message.isEmpty()) {
/**
* Firebase - Send message
*/
mFirebaseRef.push().setValue(new Chat(message, mId));
}
metText.setText("");
}
});
mFirebaseRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
if (dataSnapshot != null && dataSnapshot.getValue() != null) {
try {
Chat model = dataSnapshot.getValue(Chat.class);
mChats.add(model);
mRecyclerView.scrollToPosition(mChats.size() - 1);
mAdapter.notifyItemInserted(mChats.size() - 1);
} catch (Exception ex) {
Log.e(TAG, ex.getMessage());
}
}
}
@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) {
Log.d(TAG, databaseError.getMessage());
}
});
}
}
Chat.java
public class Chat {
public String message;
public String id;
public Chat() {
}
public Chat(String message, String id) {
this.message = message;
this.id = id;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
ChatAdapter.java
class ChatAdapter extends RecyclerView.Adapter<ChatAdapter.ViewHolder> {
private static final int CHAT_END = 1;
private static final int CHAT_START = 2;
private List<Chat> mDataSet;
private String mId;
ChatAdapter(List<Chat> dataSet, String id) {
mDataSet = dataSet;
mId = id;
}
@Override
public ChatAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v;
if (viewType == CHAT_END) {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_chat_end, parent, false);
} else {
v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_chat_start, parent, false);
}
return new ViewHolder(v);
}
@Override
public int getItemViewType(int position) {
if (mDataSet.get(position).getId().equals(mId)) {
return CHAT_END;
}
return CHAT_START;
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
Chat chat = mDataSet.get(position);
holder.mTextView.setText(chat.getMessage());
}
@Override
public int getItemCount() {
return mDataSet.size();
}
/**
* Inner Class for a recycler view
*/
class ViewHolder extends RecyclerView.ViewHolder {
TextView mTextView;
ViewHolder(View v) {
super(v);
mTextView = (TextView) itemView.findViewById(R.id.tvMessage);
}
}
}
如何改进这一点,以便在触摸每个用户时,它显示该用户的聊天消息与所有用户消息不同。