当我手动更改Firebase数据库中的值时,我在列表视图中创建了更多项目,而不是在列表视图中更新了值 [英] when I manually change value in firebase database I create more items in listview instead of updating value in listview
问题描述
我不知道我的代码出了什么问题,请帮助我. 只是试图检索数据以显示列表视图.
I have No idea what's going wrong in my code Please Help Me. Just Trying To Retrieve data to list view.
我的MainActivity.java
My MainActivity.java
public class MainActivity extends AppCompatActivity {
ListView listView;
FirebaseDatabase database;
DatabaseReference ref;
ArrayList<String> list;
ArrayAdapter <String> adapter;
User user;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
user = new User();
listView = findViewById(R.id.listView);
database = FirebaseDatabase.getInstance();
ref = database.getReference("match1contest1");
list = new ArrayList<>();
adapter = new ArrayAdapter<String>(this,R.layout.user_info,R.id.userInfo, list);
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds: dataSnapshot.getChildren()){
user = ds.getValue(User.class);
list.add(user.getName().toString()+user.getEmail().toString());
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
我的User.java
My User.java
public class User {
private String name;
private String email;
private String phone;
private String userName;
private String password;
public User() {
}
public User(String name, String email, String phone, String userName, String password) {
this.name = name;
this.email = email;
this.phone = phone;
this.userName = userName;
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
下面是图片
我只想更新值
我不知道为什么会创建更多物品
I don't know why it creates more items
我的火力基地只有3个孩子
I have only 3 children in firebase
我只是在检索数据,数据是从Firebase控制台手动保存的
I am Just retrieving data, the data is saved manually from firebase console
请帮助我...
推荐答案
当您更新数据库中的值时,它将触发您的
When you update a value in your database, it will trigger your
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {...
同样,它将为所有子项(包括更新的子项)获取并再次放置在列表内,这将重复这些项,因为在您的onDataChange
内部,您有一个for循环,它将再次遍历所有子项并将它们再次添加到当前填充的列表中.
Again, and that will fetch for all childrens (included the updated one) and will be placed inside the list again this will duplicate the items since inside your onDataChange
you have a for loop that will loop throught all the childrens again and will add them again to the current populated list.
您需要做的是在再次获取时清除列表
What you need to do, is to clear the list when is fetched again
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot ds: dataSnapshot.getChildren()){
user = ds.getValue(User.class);
list.add(user.getName().toString()+user.getEmail().toString());
}
listView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
另一种方法是使用 updateChildren( )(带有地图),这只会更新您要在数据库中更新的项目
Another way you can do it is to use updateChildren() with a map, this will just update the item you are updating in your database
这篇关于当我手动更改Firebase数据库中的值时,我在列表视图中创建了更多项目,而不是在列表视图中更新了值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!