数据不会从firebase检索,但不会显示错误

4ngedf3f  于 2021-09-13  发布在  Java
关注(0)|答案(0)|浏览(228)

我想将我的数据从firebase检索到listview android studio。logcat上没有错误,但数据不会显示在listview上。以下是模型中的代码:

public class Transaksi {

    private String namaPemesan;
    private String tglMasuk;
    private String tglSelesai;
    private String namaPenerima;

    public List<Transaksi> getTransaksiList() {
        return transaksiList;
    }

    public void setTransaksiList(List<Transaksi> transaksiList) {
        this.transaksiList = transaksiList;
    }

    private List<Transaksi> transaksiList;

    public String getNamaPemesan() {
        return namaPemesan;
    }

    public void setNamaPemesan(String namaPemesan) {
        this.namaPemesan = namaPemesan;
    }

    public String getTglMasuk() {
        return tglMasuk;
    }

    public void setTglMasuk(String tglMasuk) {
        this.tglMasuk = tglMasuk;
    }

    public String getTglSelesai() {
        return tglSelesai;
    }

    public void setTglSelesai(String tglSelesai) {
        this.tglSelesai = tglSelesai;
    }

    public String getNamaPenerima() {
        return namaPenerima;
    }

    public void setNamaPenerima(String namaPenerima) {
        this.namaPenerima = namaPenerima;
    }

    public Transaksi(String namaPemesan, String tglMasuk, String tglSelesai, String namaPenerima) {
        this.namaPemesan = namaPemesan;
        this.tglMasuk = tglMasuk;
        this.tglSelesai = tglSelesai;
        this.namaPenerima = namaPenerima;
    }
}

该适配器:

public class TransaksiAdapter extends ArrayAdapter<Transaksi> {

    public TransaksiAdapter(Context context, int layout, ArrayList<Transaksi> transaksiArrayList){
        super (context, R.layout.list_transaksi, transaksiArrayList);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View row =  convertView;
        if (row == null) {
            row = LayoutInflater.from(getContext()).inflate(R.layout.list_transaksi, parent, false);

            Transaksi currentTransaksi = getItem(position);
            TextView txtTglMasuk = (TextView) row.findViewById(R.id.detail_tgl_masuk);
            txtTglMasuk.setText(currentTransaksi.getTglMasuk());

            TextView txtTglKeluar = (TextView) row.findViewById(R.id.detail_tgl_keluar);
            txtTglKeluar.setText(currentTransaksi.getTglSelesai());

            TextView txtPemesan = (TextView) row.findViewById(R.id.detail_pemesan);
            txtPemesan.setText(currentTransaksi.getNamaPemesan());

            TextView txtPenerima = (TextView) row.findViewById(R.id.detail_penerima);
            txtPenerima.setText(currentTransaksi.getNamaPenerima());
        }
        return row;
    }
}

这就是活动:

public class ListTransaksiActivity extends AppCompatActivity {

    private DatabaseReference databaseReference;
    private ListView listViewTransaksi;

    ArrayList<Transaksi> trksArrayList = new ArrayList<>();
    TransaksiAdapter transaksiAdapter = null;

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

        transaksiAdapter = new TransaksiAdapter(this, R.layout.list_transaksi, trksArrayList);

        databaseReference = FirebaseDatabase.getInstance("https://rewanglaundry-ec6e9-default-rtdb.asia-southeast1.firebasedatabase.app/").getReference("Laundry");
        listViewTransaksi = findViewById(R.id.listView_Transaksi);

        listViewTransaksi.setAdapter(transaksiAdapter);

        databaseReference.addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                Transaksi value = dataSnapshot.getValue(Transaksi.class);
                trksArrayList.add(value);
                transaksiAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
                transaksiAdapter.notifyDataSetChanged();
            }

            @Override
            public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

            }

            @Override
            public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            }

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

            }
        });

    }
}

以下是json:

{
  "Laundry" : {
    "1626021441819" : {
      "keranjang" : [ {
        "harga" : "10000",
        "id" : 13,
        "namaJenis" : "KILOAN GOSOK",
        "quantity" : "2"
      }, {
        "harga" : "9000",
        "id" : 14,
        "namaJenis" : "KILOAN CUCI",
        "quantity" : "2"
      }, {
        "harga" : "5000",
        "id" : 15,
        "namaJenis" : "KILOAN GOSOK",
        "quantity" : "1"
      } ],
      "namaPemesan" : "Jumi",
      "namaPenerima" : "Ten",
      "tglMasuk" : "11-07-2021",
      "tglSelesai" : "14-07-2021",
      "totalHarga" : "24000"
    },
    "1626264517918" : {
      "keranjang" : [ {
        "harga" : "8000",
        "id" : 16,
        "namaJenis" : "KAOS",
        "quantity" : "1"
      }, {
        "harga" : "9000",
        "id" : 17,
        "namaJenis" : "KILOAN CUCI",
        "quantity" : "2"
      } ],
      "namaPemesan" : "Toni",
      "namaPenerima" : "Tuki",
      "tglMasuk" : "07-07-2021",
      "tglSelesai" : "10-07-2021",
      "totalHarga" : "17000"
    }
  }
}

我使用的是数据库location region asia,所以我将链接放在getinstance上。但它仍然不会显示来自firebase的数据。感谢您的帮助。谢谢你

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题