我在获取新邮件时遇到了一些麻烦。
所以我的算法就是这样的。我收到聊天中的最后一条消息(在Firebase中),如果最后一条消息UserId不等于我的,则将布尔值(newMessage)设置为true以使其成为新消息。
当我点击它时,变量设置为false。但是,当我重新运行应用程序时,布尔值不会保持不变,它只会自行重置。
我应该怎么做才能解决这个问题?
以下是代码:
MessengerMessagesFragment.java
public class MessengerMessagesFragment extends Fragment{
private static final String TAG = "MessengerMessagesFragme";
private List<User> mUserList;
private static final int SIGN_IN_REQUEST_CODE = 111;
private ListView mListView;
private ArrayList<String> mUsersInM;
private MessagesListAdapter mAdapter;
User user;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_messenger_messages, container, false);
mListView = (ListView) view.findViewById(R.id.lvMessages);
mUserList = new ArrayList<>();
mUsersInM = new ArrayList<>();
if (FirebaseAuth.getInstance().getCurrentUser() == null) {
// Start sign in/sign up activity
startActivityForResult(AuthUI.getInstance()
.createSignInIntentBuilder()
.build(), SIGN_IN_REQUEST_CODE);
} else {
// User is already signed in, show list of messages
getUsersInM();
}
return view;
}
private void updateUserslist(){
Log.d(TAG, "updateUsersList: updating users list");
mAdapter = new MessagesListAdapter(getActivity(), R.layout.layout_message_listitem, mUserList);
mListView.setAdapter(mAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e(TAG, "onItemClick: selected user: " + mUserList.get(position).toString());
mAdapter.setNewMessage(false);
Log.e(TAG, "onItemClick: Set new message bool " + mAdapter.isNewMessage());
Intent intent = new Intent(getActivity(), ChatActivity.class);
intent.putExtra(getString(R.string.intent_user), mUserList.get(position));
startActivity(intent);
}
});
}
private void getMessages(){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
for(int i = 0; i < mUsersInM.size(); i++){
final int count = i;
Query query = reference
.child(getString(R.string.dbname_users))
.orderByChild(getString(R.string.field_user_id))
.equalTo(mUsersInM.get(i));
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot : dataSnapshot.getChildren()){
mUserList.add(singleSnapshot.getValue(User.class));
}
if(count >= mUsersInM.size() - 1){
updateUserslist();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
private void getUsersInM(){
DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = reference
.child(getString(R.string.dbname_messages))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
mUsersInM.add(singleSnapshot.getKey().toString());
Log.d(TAG, "onDataChange: Message user id: " + mUsersInM.toString() );
}
getMessages();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
MessagesListAdapter.java
public class MessagesListAdapter extends ArrayAdapter<User> {
private static final String TAG = "MessageListAdapter";
private LayoutInflater mInflater;
private List<User> mUsers = null;
private int layoutResource;
private Context mContext;
private int messagesCount = 0;
DatabaseReference myRef;
public boolean newMessage;
public boolean isNewMessage() {
return newMessage;
}
public void setNewMessage(boolean newMessage) {
this.newMessage = newMessage;
}
//private int msgC;
private String userID;
public MessagesListAdapter(@NonNull Context context, @LayoutRes int resource, @NonNull List<User> objects) {
super(context, resource, objects);
mContext = context;
myRef = FirebaseDatabase.getInstance().getReference();
//getMessagesCount();
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layoutResource = resource;
this.mUsers = objects;
}
private static class ViewHolder{
TextView username, message, timeStamp;
ImageView profileImage, statusOnline, statusOffline;
RelativeLayout messageBackground;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
final ViewHolder holder;
if(convertView == null){
convertView = mInflater.inflate(layoutResource, parent, false);
holder = new ViewHolder();
holder.username = (TextView) convertView.findViewById(R.id.f_username);
holder.message = (TextView) convertView.findViewById(R.id.m_message);
holder.profileImage = (ImageView) convertView.findViewById(R.id.f_profile_image);
holder.timeStamp = (TextView) convertView.findViewById(R.id.m_timestamp);
holder.statusOnline = (ImageView) convertView.findViewById(R.id.iv_Online);
holder.statusOffline = (ImageView) convertView.findViewById(R.id.iv_Offline);
holder.messageBackground = (RelativeLayout) convertView.findViewById(R.id.RelLayoutMessage);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.username.setText(getItem(position).getUsername());
//holder.email.setText(getItem(position).getEmail());
//DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
Query query = myRef.child(mContext.getString(R.string.dbname_user_account_settings))
.orderByChild(mContext.getString(R.string.field_user_id))
.equalTo(getItem(position).getUser_id());
query.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot singleSnapshot: dataSnapshot.getChildren()){
//Log.d(TAG, "onDataChange: found user: " + singleSnapshot.getValue(UserAccountSettings.class).toString());
ImageLoader imageLoader = ImageLoader.getInstance();
imageLoader.displayImage(singleSnapshot.getValue(UserAccountSettings.class).getProfile_photo(),
holder.profileImage);
userID = singleSnapshot.getValue(User.class).getUser_id();
boolean is_Online = singleSnapshot.getValue(User.class).isOnline();
if(is_Online == true){
holder.statusOnline.setVisibility(View.VISIBLE);
holder.statusOffline.setVisibility(View.GONE);
}else if(is_Online == false){
holder.statusOnline.setVisibility(View.GONE);
holder.statusOffline.setVisibility(View.VISIBLE);
}
}
setMessageItemSettings(holder, userID);
getNewMessage(holder, userID);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return convertView;
}
private void getNewMessage(final ViewHolder holder, final String user_id){
Query lastMessageQuery = myRef.child(mContext.getString(R.string.dbname_messages))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(user_id).orderByKey().limitToLast(1);
lastMessageQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot data : dataSnapshot.getChildren()) {
String messageUid = data.child(mContext.getString(R.string.field_messageUserId)).getValue().toString();
Log.e(TAG, "onDataChange: *************** MESSAGE FROM USER *************** : " + messageUid + " ====== " + newMessage);
if(messageUid.equals(user_id)){
newMessage = true;
Log.e(TAG, "onDataChange: *************** NEW MESSAGE FROM USER *************** : " + messageUid + " ====== " + newMessage);
if(newMessage){
holder.messageBackground.setBackgroundColor(Color.parseColor("#b7b7b7"));
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void setMessageItemSettings(final ViewHolder holder, final String user_id){
Log.e(TAG, "getView: USER ID************************************" + userID);
Log.e(TAG, "getView: USER ID************************************" + messagesCount);
//msgC = messagesCount;
//DatabaseReference databaseReference2 = FirebaseDatabase.getInstance().getReference();
Query lastMessageQuery = myRef.child(mContext.getString(R.string.dbname_messages))
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.child(user_id).orderByKey().limitToLast(1);
lastMessageQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "onDataChange: THINGS--------------+++ ---" + dataSnapshot.toString());
Log.e(TAG, "getView: USER ID************************************" + userID);
Log.e(TAG, "getView: USER ID************************************" + messagesCount);
//String lastItem = dataSnapshot.getValue().toString();
for (DataSnapshot data : dataSnapshot.getChildren()) {
try{
String mMessage = data.child(mContext.getString(R.string.field_messageText)).getValue().toString();
holder.message.setText(mMessage);
String mTime = data.child(mContext.getString(R.string.field_messageTime)).getValue().toString();
holder.timeStamp.setText(DateFormat.format("dd-MM-yyyy (HH:mm:ss)", Long.valueOf(mTime)));
}catch(NullPointerException e){
Log.e(TAG, "onDataChange: *************** GETTING MESSAGES FROM USER ARE NULL *************** : " + e.getMessage());
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
//Handle possible errors.
}
});
//Log.d(TAG, "getView: USER ID****************" + userID);
}
答案 0 :(得分:0)
您是否在某处保存newMessage = true;
声明?
答案 1 :(得分:0)
我已修复它在firebase上的user_id节点2(WI8yTLA ...)中放置new_message变量并解决了我的问题。