gpt4 book ai didi

java - Firebase Android ListView 未显示

转载 作者:行者123 更新时间:2023-12-02 10:24:36 25 4
gpt4 key购买 nike

用户登录后,我试图在主菜单屏幕的 ListView 上显示 Firebase 实时数据库中的数据。应用程序正在运行,但不显示数据。

这是我数据库中的数据

enter image description here

现在是代码。

MainMenu.java 该函数在 OnCreate() 上调用。

 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);

}

自定义列表适配器.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;
}
}

AdapterItem.java

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.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" />

content_main_menu_list.xml 是我为要显示的每个数据集创建的自定义布局。

 <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
xmlns:app="http://schemas.android.com/apk/res-auto">


<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<TextView
android:id="@+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />

<TextView
android:id="@+id/serialNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<TextView
android:id="@+id/supplierName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="TextView"
app:layout_constraintTop_toBottomOf="@+id/serialNo" />

<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="23dp"
android:layout_marginBottom="8dp"

android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#0171B0"
app:layout_constraintBottom_toTopOf="@+id/serialNo" />
</android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

最佳答案

代码中的问题在于,您的 CustomListAdapter 类中有一个名为 ItemName 的字段,但您使用的是名为 getItemName() 的 getter ,这是不正确的,因为 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)getter。还有一个更简单的解决方案,直接在公共(public)字段上设置值,如下所示:

public class CustomListAdapter {
public String itemName, quantity, serialNo, supplierName, supplierEmail, supplierPhone;
}

现在只需删除当前数据并使用正确的名称再次添加即可。该解决方案仅在您处于测试阶段时才有效。

还有第二种方法,即使用注释。因此,如果您更喜欢使用私有(private)字段和公共(public) getter,则应该使用 PropertyName注释仅在 getter 前面。因此,您的 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; }
}

关于java - Firebase Android ListView 未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54069574/

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