gpt4 book ai didi

java - 无法从 Firebase 检索信息到自定义 ListView Android

转载 作者:行者123 更新时间:2023-11-29 23:12:39 25 4
gpt4 key购买 nike

我是 Android Studio 新手。我正在尝试开发库存管理应用程序,但无法在来自 Firebase 数据库的自定义 ListView 中显示信息。我在这里找到了类似的问题和解决方案,但对我不起作用。下面是 AccountHome(MainActivity) 的代码。当我多次关闭并重新启动应用程序时,此代码有时会起作用。希望得到一些可行的解决方案。谢谢Screenshot of Firebase Database

public class AccountHom extends AppCompatActivity {

ListView listview;
FirebaseDatabase database;
DatabaseReference myRef;
ArrayList<String> dept ;
ArrayList<Long> total;
AccountSetBudgetHelper accountSetBudgetHelper;

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

accountSetBudgetHelper = new AccountSetBudgetHelper();
listview = findViewById(R.id.set_budget_listview);
database = FirebaseDatabase.getInstance();
myRef = database.getReference("budget");
dept = new ArrayList<>();
total = new ArrayList<Long>();

AccountSetBudgetAdaptr accountSetBudgetAdapter = new AccountSetBudgetAdaptr(AccountHom.this ,dept,total);

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
accountSetBudgetHelper = ds.getValue(AccountSetBudgetHelper.class);
dept.add(accountSetBudgetHelper.getName());
total.add(accountSetBudgetHelper.getTotal());
} }
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
listview.setAdapter(accountSetBudgetAdapter);
}
}
class AccountSetBudgetAdaptr extends ArrayAdapter {
private final Activity context;
private final ArrayList<String> dept;
private final ArrayList<Long> total;

AccountSetBudgetAdaptr(Activity context, ArrayList<String> dept, ArrayList<Long> total) {
super(context, R.layout.single_row_listview_budget,dept);
this.context = context;
this.dept = dept;
this.total = total;
}
@NonNull
@Override
public View getView(int position, View view, @NonNull ViewGroup parent) {

Toast.makeText(getContext(), "this is a boy", Toast.LENGTH_SHORT).show();
View rowView = view;
if(rowView==null){
LayoutInflater inflater = context.getLayoutInflater();
rowView = inflater.inflate(R.layout.single_row_listview_budget, parent, false);
}
TextView mdept = rowView.findViewById(R.id.textView_setbudget_dept);
TextView mtotal = rowView.findViewById(R.id.textView_setbudget_total);
mdept.setText(dept.get(position));
mtotal.setText(String.valueOf(total.get(position)));
return rowView;
}
}
class AccountSetBudgetHelpr {
private String name;
private Long total;
public AccountSetBudgetHelpr() {
}
public AccountSetBudgetHelpr(String name, Long total) {
this.name = name;
this.total = total;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
}[enter image description here][1]

最佳答案

您正在向两个列表添加数据,depttotal 但您没有将新数据通知适配器。因此,要解决此问题,请在 for 循环结束后立即添加以下代码行:

myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
accountSetBudgetHelper = ds.getValue(AccountSetBudgetHelper.class);
dept.add(accountSetBudgetHelper.getName());
total.add(accountSetBudgetHelper.getTotal());
}
accountSetBudgetAdapter.notifyDataSetChanged(); //Notify the adapter
}

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

关于java - 无法从 Firebase 检索信息到自定义 ListView Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55814269/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com