使用Firebase数据库时如何获取RecyclerView的子值?

时间:2017-03-15 06:01:46

标签: android firebase firebase-realtime-database

我使用dataSnapshot.getValue(ChatItem.class);从FirebaseDatabase中保存namemsg的值。

当我使用firstRoot.addChildEventListener时,我的textRight of recyclerView值为空,所以我尝试在addChildEventListenersecondRoot.addChildEventListener的不同根中使用thirdRoot.addChildEventListener,这会导致崩溃

我不知道另一种方法。有人可以教我如何完成它吗?  提前谢谢。

这是我的RecyclerView的ChatRoom类:

public class ChatRoom extends AppCompatActivity {

    private String roomName, userName;

    private Button sendMessage;
    private EditText typeMessage;
    private String temp_key;

    private DatabaseReference firstRoot;
    private DatabaseReference secondRoot;
    private DatabaseReference thirdRoot;

    private RecyclerView recyclerChat;
    private ArrayList<ChatItem> listData = new ArrayList<>();
    private RecyclerChatAdapter adapter;

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

        initViews();

        roomName = getIntent().getExtras().getString("room_name");
        userName = getIntent().getExtras().getString("userName");

        setTitle("Chat Name:" + roomName);

        firstRoot = FirebaseDatabase.getInstance().getReference().child("ChatGroup");//specify first child

        sendMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                secondRoot = firstRoot.child(roomName);

                Map<String, Object> mapKey = new HashMap<>();
                temp_key = firstRoot.push().getKey();//create key control json tree
                firstRoot.updateChildren(mapKey);


                Map<String, Object> map = new HashMap<>();
                map.put("name", userName);
                map.put("msg", typeMessage.getText().toString());

                thirdRoot = secondRoot.child(temp_key);
                thirdRoot.updateChildren(map);

                typeMessage.setText("");
            }
        });

        firstRoot.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                ChatItem chatItem = dataSnapshot.getValue(ChatItem.class);//i save value to my java bean class

                listData.add(chatItem);
                recyclerChat.scrollToPosition(adapter.getItemCount() - 1);//just let the latest data below
                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) {

            }
        });


        LinearLayoutManager manager = new LinearLayoutManager(this);
        //        GridLayoutManager gridLayoutManager=new GridLayoutManager(this,2);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerChat.setLayoutManager(manager);

        adapter = new RecyclerChatAdapter(this, listData);
        recyclerChat.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    private void initViews() {
        sendMessage = (Button) findViewById(R.id.sendMessage);
        typeMessage = (EditText) findViewById(R.id.typeMessage);
        recyclerChat = (RecyclerView) findViewById(R.id.recyclerChat);
    }
}

我的javabean:

public class ChatItem {
    private String name;
    private String msg;


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

}

关于onBindViewHolder的我的RecyclerViewAdapter:

@Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.textRight.setText(itemList.get(position).getMsg());
    }

这是我的json树: enter image description here

我尝试为我的javabean使用构造函数,直到没有工作:

public ChatItem(){
}

public ChatItem(String name, String msg) {
    this.name = name;
    this.msg = msg;
} 

我也设置了权限:

 <uses-permission android:name="android.permission.INTERNET"/>

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

我的问题是holder.textRight.setText(itemList.get(position).getMsg());为空,我无法为我的recyclerView获取

我使用ChatItem chatItem = dataSnapshot.getValue(ChatItem.class);就像这个json树一样: enter image description here

0 个答案:

没有答案