简体   繁体   English

如何从TextView替换数据,而不是从Firebase数据库添加到TextView?

[英]How do you replace data from a TextView instead of adding to the TextView from a firebase database?

I'm populating a Textview from a firebase database. 我正在从Firebase数据库填充Textview。 When I add an extra child ( names ) to the database, the TextView keeps the previous data and repeats the previous data plus the new child entry. 当我向数据库添加一个额外的子项( names )时,TextView保留先前的数据,并重复先前的数据以及新的子项。 I want to dismiss the previous data and just have the the updated version. 我想关闭以前的数据,而只拥有更新的版本。 For example the TextView populates with 例如,TextView填充为

Mary, John

But when I add an extra child the TextView populates with 但是当我添加一个额外的孩子时,TextView会填充

Mary, John, Mary, John, Adam

I just want the TextView to populate With Mary, John, Adam 我只希望TextView中填充Mary, John, Adam

here is my code 这是我的代码

TextView stringTextView;

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

   stringTextView = (TextView) findViewById(R.id.textView2);

    DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
    DatabaseReference ref = rootRef.child("County").child("Dublin").child("contacts");

    ValueEventListener eventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ArrayList<String> names = new ArrayList<>();
            for (DataSnapshot ds : dataSnapshot.getChildren()) {
                String name = ds.child("name").getValue(String.class);
               // Log.d("TAG", name);
                names.add(name);
                //String number = ds.child("number").getValue(String.class);
                //Log.d("Name", name + " / " + number);
            }
            for (String name : names) {
                //stringTextView = (TextView) findViewById(R.id.textView2);
                stringTextView.setText(stringTextView.getText().toString() + name + " , ");
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
        }
    };

    ref.addValueEventListener(eventListener);
}}
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
    **stringTextview.setText(" ")**

That's what I'd do, change the text to empty every time I capture a new snapshot of data :) 那就是我要做的,每次捕获新的数据快照时,将文本更改为空:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM