map.containsKey(key)返回true,但map.get(key)不返回任何内容

时间:2017-06-27 02:05:48

标签: java android firebase firebase-realtime-database

我正在努力使用Firebase来使用简单的消息传递系统。但这一件事让我想要拔出自己的头发。我有一张地图用于存储<String,Message>对,其中Message是我写的一个类。我不能把我的信息从地图上拿出来。即使map.get(key)返回map.containsKey(key),我也已将问题与true无法返回消息隔离开来。可能导致这种情况的原因以及如何解决?

这是Message类:

public class Message {
    private long timeStamp;
    private String message;
    private String to;
    private String from;

    public Message(String message, String to, String from)
    {
        timeStamp = new Date().getTime();
        this.message = message;
        this.to = to;
        this.from = from;
    }

    public Message(){

    }

    public String getMessageText() {
        return this.message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getTo() {
        return this.to; 
    }

    public void setTo(String to) {
        this.to = to;
    }

    public String getFrom() {
        return this.from;
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public long getTimeStamp() {
        return this.timeStamp;
    }

    public void setTimeStamp(long timeStamp) { 
        this.timeStamp = timeStamp; 
    }
}

这是我使用的代码,显示map.containsKey(key)返回true

public class LoadMessages extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load_messages);

    // Load previous messages

    final LinearLayout messagesLayout = (LinearLayout) findViewById(R.id.messagesLayout);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference = database.getReference(getIntent().getStringExtra("user") + "/Messages");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {

            Map<String, Message> map = new TreeMap<String, Message>();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Message m = snapshot.getValue(Message.class);
                map.put(String.valueOf(m.getTimeStamp()), m);

                TextView TV = new TextView(LoadMessages.this);
                Boolean ck = map.containsKey(String.valueOf(m.getTimeStamp()));
                if (ck) {
                    TV.setText("Contains key");
                } else {
                    TV.setText("Doesn't contain key.");
                }
                messagesLayout.addView(TV);
            }

            // code to iterate through map entries and display messages

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    // code Add new message onClick of fab

}
}

此版本的代码在TextView

中没有显示任何内容
public class LoadMessages extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_load_messages);

    // Load previous messages

    final LinearLayout messagesLayout = (LinearLayout) findViewById(R.id.messagesLayout);

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    DatabaseReference databaseReference = database.getReference(getIntent().getStringExtra("user") + "/Messages");
    databaseReference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(final DataSnapshot dataSnapshot) {

            Map<String, Message> map = new TreeMap<String, Message>();

            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Message m = snapshot.getValue(Message.class);
                map.put(String.valueOf(m.getTimeStamp()), m);

                TextView TV = new TextView(LoadMessages.this);
                Message test = map.get(String.valueOf(m.getTimeStamp()));
                TV.setText(test.getMessageText());
                messagesLayout.addView(TV);
            }

            // code to iterate through map entries and display messages

        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

    // code Add new message onClick of fab

}
}

编辑:这是我的数据库结构: enter image description here

2 个答案:

答案 0 :(得分:0)

我认为问题是snapshot.getValue(Message.class)返回null。 TreeMap可以将null存储为值,因此当它包含您的键时,该值实际上为null。

答案 1 :(得分:0)

好的,我明白了!事实证明,消息中的消息字符串被更改为数据库中的messageText,因此当我尝试将其提取到Message对象时,它将无法识别它。解决方法是将Message类中的消息String更改为messageText。