gpt4 book ai didi

java - 如何在 Android ListView 中显示 Firebase 项目?

转载 作者:行者123 更新时间:2023-11-30 00:02:48 25 4
gpt4 key购买 nike

我正在尝试在 Android ListView 中显示一些 Firebase 记录。目前,当我输入一条新记录时,我的代码正在向 ListView 返回一个单独的 0,但是我的 Firebase 数据库正在完美地显示我想要的信息。

我在这个问题上花了很多时间,但似乎不能完全查明问题所在。

如有任何帮助,我们将不胜感激。

下面是我显示/添加新记录的代码:

    TenantList tenantListAdapter;

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

mAuth = FirebaseAuth.getInstance();
FirebaseUser user = mAuth.getCurrentUser();
String uid = user.getUid();

//databaseTenantsList = FirebaseDatabase.getInstance().getReference("tenants").child(uid);

textViewPropertyName = (TextView) findViewById(R.id.textViewPropertyName);
editTextTenantsName = (EditText) findViewById(R.id.editTextTenantsName);
seekBarAge = (SeekBar) findViewById(R.id.seekBarAge);

buttonAddTenant = (Button) findViewById(R.id.buttonAddTenant);

listViewTenants = (ListView) findViewById(R.id.listViewTenants);
tenants = new ArrayList<>();
tenantListAdapter = new TenantList(this, tenants);
listViewTenants.setAdapter(tenantListAdapter);

Intent intent = getIntent();

tenants = new ArrayList<>();

String id = intent.getStringExtra(PropertyActivity.PROPERTY_ID);
String name = intent.getStringExtra(PropertyActivity.PROPERTY_NAME);

textViewPropertyName.setText(name);

databaseTenants = FirebaseDatabase.getInstance().getReference("tenants").child(uid).child(id).child(uid);

//databaseTenants = FirebaseDatabase.getInstance().getReference("tenants").child(id);

buttonAddTenant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
saveTenant();
}
});
}

@Override
protected void onStart() {
super.onStart();

databaseTenants.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tenants.clear();

for(DataSnapshot tenantSnapshot : dataSnapshot.getChildren()){
Tenant tenant = tenantSnapshot.getValue(Tenant.class);

tenants.add(tenant);
}
tenantListAdapter.notifyDatasetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

}

private void saveTenant() {

String tenantName = editTextTenantsName.getText().toString().trim();
int age = seekBarAge.getProgress();

if(!TextUtils.isEmpty(tenantName)){

String id = databaseTenants.push().getKey();

Tenant tenant = new Tenant(id, tenantName, age);

FirebaseUser user = mAuth.getCurrentUser();
String uid = user.getUid();

databaseTenants.child(uid).child(id).setValue(tenant);

Toast.makeText(this, "Tenant saved successfully", Toast.LENGTH_LONG).show();

} else {

Toast.makeText(this, "Tenant name should not be empty", Toast.LENGTH_LONG).show();
}
}
}

租户列表

    public class TenantList extends ArrayAdapter<Tenant> {

private Activity context;
private List<Tenant> tenants;

public TenantList(Activity context, List<Tenant> tenants) {

super(context, R.layout.tenant_list_layout, tenants);
this.context = context;
this.tenants = tenants;
}

@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
LayoutInflater inflater = context.getLayoutInflater();

View listViewItem = inflater.inflate(R.layout.tenant_list_layout, null, true);

TextView textViewTenant = (TextView) listViewItem.findViewById(R.id.textViewTenant);
TextView textViewAge = (TextView) listViewItem.findViewById(R.id.textViewAge);

Tenant tenant = tenants.get(position);
textViewTenant.setText(tenant.getTenantName());
textViewAge.setText(String.valueOf(tenant.getTenantAge()));

return listViewItem;

}
}

要添加更多上下文,这是我的数据结构:

Data Structure

最佳答案

据我所见,在到达 tennat 对象之前还有一层。

databaseTenants = FirebaseDatabase.getInstance().getReference("tenants").child(uid).child(id).child(uid);

此外,将您的适配器创建拉取到 onCreate 方法,以便您可以全局访问它:

添加成员变量:

TenantList tenantListAdapter

并在您的 ListView 之后对其进行初始化,将租户拉到适配器实例化之前以便您可以访问它:

listViewTenants = (ListView) findViewById(R.id.listViewTenants);
tenants = new ArrayList<>();
tenantListAdapter = new TenantList(this, tenants);
listViewTenants.setAdapter(tenantListAdapter);

然后,在您的事件监听器中:

databaseTenants.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
tenants.clear();

for(DataSnapshot tenantSnapshot : dataSnapshot.getChildren()){
for(DataSnapshot lastSnapshot: tenantSnapshot.getChildren()){
Tenant tenant = lastSnapshot.getValue(Tenant.class);
tenants.add(tenant);
}
}
tenantListAdapter.notifyDataSetChanged();
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

关于java - 如何在 Android ListView 中显示 Firebase 项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49599224/

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