用户登录后,我试图在主菜单屏幕的列表视图上显示Firebase实时数据库中的数据。该应用程序正在运行,但未显示数据。
这是我数据库中的数据
现在输入密码。
MainMenu.java在OnCreate()上调用此函数。
bgs= [];
public lineChartData: Array<any>
ngOnInit(){
this.lineChartData = []
. . .
}
getbgs() {
this.bgsService.getbgss(this.token).subscribe(
data => {
this.bgs = data.map(k => k.bgs);
this.lineChartData = [
{ data: [this.bgs], label: 'Series A' }
]
}
);
}
CustomListAdapter.java
public void makeItem ()
{
lv = findViewById(R.id.listView);
db = FirebaseDatabase.getInstance().getReference();
helper = new FirebaseHelper(db);
adapter= new AdapterItem(this,helper.retrive());
lv.setAdapter(adapter);
}
AdapterItem.java
public class CustomListAdapter{
private String ItemName;
private String Quantity;
private String SerialNo;
private String SupplierName;
private String SupplierEmail;
private String SupplierPhone;
public CustomListAdapter(){
}
public CustomListAdapter (String ItemName,String Quantity,String SerialNo,String SupplierName,String SupplierEmail,String SupplierPhone)
{
this.ItemName = ItemName;
this.Quantity = Quantity;
this.SerialNo = SerialNo;
this.SupplierName = SupplierName;
this.SupplierEmail = SupplierEmail;
this.SupplierPhone = SupplierPhone;
}
public void setItemName (String ItemName)
{
this.ItemName = ItemName;
}
public String getItemName ()
{
return ItemName;
}
public void setQuantity (String Quantity)
{
this.Quantity = Quantity;
}
public String getQuantity ()
{
return Quantity;
}
public void setSerialNo (String SerialNo)
{
this.SerialNo = SerialNo;
}
public String getSerialNo ()
{
return SerialNo;
}
public void setSupplierName (String SupplierName)
{
this.SupplierName = SupplierName;
}
public String getSupplierName()
{
return SupplierName;
}
public void setSupplierEmail (String SupplierEmail)
{
this.SupplierEmail = SupplierEmail;
}
public String getSupplierEmail() {
return SupplierEmail;
}
public void setSupplierPhone (String SupplierPhone)
{
this.SupplierPhone = SupplierPhone;
}
public String getSupplierPhone() {
return SupplierPhone;
}
}
content_main_menu.xml
public class AdapterItem extends BaseAdapter {
Context c;
ArrayList<CustomListAdapter> customListAdapters;
public AdapterItem(Context c, ArrayList<CustomListAdapter> customListAdapters) {
this.c = c;
this.customListAdapters = customListAdapters;
}
@Override
public int getCount() {
return customListAdapters.size();
}
@Override
public Object getItem(int position) {
return customListAdapters.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
convertView=LayoutInflater.from(c).inflate(R.layout.content_main_menu_list,parent,false);
}
TextView ItemName = convertView.findViewById(R.id.name);
TextView SerialNo = convertView.findViewById(R.id.serialNo);
TextView SupplierName = convertView.findViewById(R.id.supplierName);
TextView amount = convertView.findViewById(R.id.amount);
final CustomListAdapter CLA = (CustomListAdapter) this.getItem(position);
ItemName.setText(CLA.getItemName());
SerialNo.setText(CLA.getSerialNo());
SupplierName.setText(CLA.getSupplierName());
amount.setText(CLA.getQuantity());
convertView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(c,CLA.getItemName(),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
content_main_menu_list.xml是我为要显示的每个数据集创建的自定义布局。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/app_bar_main_menu">
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp" />
答案 0 :(得分:1)
代码中的问题在于,您的CustomListAdapter
类中有一个名为ItemName
的字段,但是您使用的是名为getItemName()
的吸气剂,自Firebase以来这是不正确的正在数据库中查找名为 itemName
而不是ItemName
的字段。看到小写的i
与大写的I
?
有两种方法可以解决此问题。第一个是通过根据Java Naming Conventions重命名字段来更改模型类。因此,您的模型类应如下所示:
public class CustomListAdapter {
private String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
public CustomListAdapter() {}
public CustomListAdapter(String itemName, String quantity, String serialNo, String supplierName, String supplierEmail, String supplierPhone) {
this.itemName = itemName;
this.quantity = quantity;
this.serialNo = serialNo;
this.supplierName = supplierName;
this.supplierEmail = supplierEmail;
this.supplierPhone = supplierPhone;
}
public String getItemName() { return itemName; }
public String getQuantity() { return quantity; }
public String getSerialNo() { return serialNo; }
public String getSupplierName() { return supplierName; }
public String getSupplierEmail() { return supplierEmail; }
public String getSupplierPhone() { return supplierPhone; }
}
在此示例中,有private
个字段和公共获取器。还有一个更简单的解决方案,可以直接在公共字段上设置值,如下所示:
public class CustomListAdapter {
public String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
}
现在只需删除当前数据,然后使用正确的名称再次添加即可。仅当您处于测试阶段时,此解决方案才有效。
还有第二种方法,即使用annotations
。因此,如果您更喜欢使用私有字段和公共getter,则应仅在getter之前使用PropertyName批注。因此,您的CustomListAdapter
类应如下所示:
public class CustomListAdapter {
private String ItemName;
private String Quantity;
private String SerialNo;
private String SupplierName;
private String SupplierEmail;
private String SupplierPhone;
public CustomListAdapter() {}
public CustomListAdapter(String itemName, String quantity, String serialNo, String supplierName, String supplierEmail, String supplierPhone) {
ItemName = itemName;
Quantity = quantity;
SerialNo = serialNo;
SupplierName = supplierName;
SupplierEmail = supplierEmail;
SupplierPhone = supplierPhone;
}
@PropertyName("ItemName")
public String getItemName() { return ItemName; }
@PropertyName("Quantity")
public String getQuantity() { return Quantity; }
@PropertyName("SerialNo")
public String getSerialNo() { return SerialNo; }
@PropertyName("SupplierName")
public String getSupplierName() { return SupplierName; }
@PropertyName("SupplierEmail")
public String getSupplierEmail() { return SupplierEmail; }
@PropertyName("SupplierPhone")
public String getSupplierPhone() { return SupplierPhone; }
}