我正在尝试在Android ListView中显示一些Firebase记录。目前,当我输入新记录时,我的代码将0
单独返回ListView
,但我的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();
}
}
}
TenantList
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;
}
}
要添加更多上下文,这是我的数据结构:
答案 0 :(得分:1)
从我所看到的情况来看,在你到达tennat对象之前还有一个等级。
databaseTenants = FirebaseDatabase.getInstance().getReference("tenants").child(uid).child(id).child(uid);
此外,将您的适配器创建提取到onCreate方法,以便您可以全局访问它:
添加成员变量:
TenantList tenantListAdapter
并在列表视图之后对其进行初始化,在适配器实例化之前将其拉出,以便您可以访问它:
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) {
}
});