如何从firebase检索所有数据并在listview android上显示?

时间:2017-11-03 10:05:56

标签: android listview firebase firebase-realtime-database

我正在制作一个Android应用程序,这是从客户那里获得的订单,但我遇到了一个问题。我正在尝试从Firebase检索数据并在列表视图中显示。我可以从Firebase获取数据,但是当它显示在列表视图中时,它只会多次显示一个数据。我希望每条记录显示在一行上。任何人都可以看到我错在哪里? Database Image

ListView Image

OrderHistory

public class OrderHistory
{
    String ammount,photoId,trxId,name,copy,photoSize,date;
    public OrderHistory(String name,String photoId,String trxId,String copy,String photoSize,String ammount,String date)
    {
        this.name = name;
        this.ammount = ammount;
        this.photoId = photoId;
        this.copy = copy;
        this.photoSize = photoSize;
        this.trxId = trxId;
        this.date = date;
    }


    public String getAmmount() {
        return ammount;
    }

    public void setAmmount(String ammount) {
        this.ammount = ammount;
    }

    public String getPhotoId() {
        return photoId;
    }

    public void setPhotoId(String photoId) {
        this.photoId = photoId;
    }

    public String getTrxId() {
        return trxId;
    }

    public void setTrxId(String trxId) {
        this.trxId = trxId;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCopy() {
        return copy;
    }

    public void setCopy(String copy) {
        this.copy = copy;
    }

    public String getPhotoSize() {
        return photoSize;
    }

    public void setPhotoSize(String photoSize) {
        this.photoSize = photoSize;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }
}

OrderHistoryAdapter

public class OrderHistoryAdapter extends BaseAdapter {

    private List<OrderHistory> orderHistories;
    Context context;

    public OrderHistoryAdapter(Context context, List<OrderHistory> myOrderInformations) {
        this.context = context;
        this.orderHistories = myOrderInformations;
    }

    @Override
    public int getCount() {
        return orderHistories.size();
    }

    @Override
    public Object getItem(int position) {
        return orderHistories.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.show_my_order_history, parent, false);
        final TextView txtName, txtdate, txtPhotoId, trxId,txtAmount,txtPhotoSize,txtCopy;
        txtName = (TextView)view.findViewById(R.id.txtName);
        txtdate = (TextView)view.findViewById(R.id.txtDate);
        txtPhotoId = (TextView)view.findViewById(R.id.txtPhotoId);
        trxId = (TextView)view.findViewById(R.id.txtTrx);
        txtAmount = (TextView)view.findViewById(R.id.txtAmount);
        txtPhotoSize = (TextView)view.findViewById(R.id.txtSize);
        txtCopy = (TextView)view.findViewById(R.id.txtCopy);
        txtName.setText(orderHistories.get(position).getName());
        txtdate.setText(orderHistories.get(position).getDate());
        txtPhotoId.setText(orderHistories.get(position).getPhotoId());
        trxId.setText(orderHistories.get(position).getTrxId());
        txtAmount.setText(orderHistories.get(position).getAmmount());
        txtCopy.setText(orderHistories.get(position).getCopy());
        txtPhotoSize.setText(orderHistories.get(position).getPhotoSize());
        return view;
    }
}

OrderHistoryList

public class OrderHistoryList extends AppCompatActivity
{
    private DatabaseReference databaseReference;
    private List<OrderHistory> orderHistories;
    private static String phoneNumber;
    private ListView listView;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.show_my_order);
        Firebase.setAndroidContext(this);
        listView = (ListView)findViewById(R.id.listView);
        getAllOrderFromFirebase();
    }

    private void getAllOrderFromFirebase()
    {
        orderHistories = new ArrayList<>();
        databaseReference = FirebaseDatabase.getInstance().getReference("order");
        String phone = getIntent().getExtras().getString("phone");
        databaseReference.orderByChild("phone").equalTo(phone).addChildEventListener(new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                String amount, photoId, trxId, name, copy, photoSize, date;
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    name = snapshot.child("name").getValue(String.class);
                    photoId = snapshot.child("photoId").getValue(String.class);
                    amount = snapshot.child("totalAmount").getValue(String.class);
                    trxId = snapshot.child("trxId").getValue(String.class);
                    photoSize = snapshot.child("photoSize").getValue(String.class);
                    date = snapshot.child("date").getValue(String.class);
                    copy = snapshot.child("totalCopy").getValue(String.class);
                    orderHistories.add(new OrderHistory(name, photoId, trxId, copy, photoSize, amount, date));
                }
                OrderHistoryAdapter adapter;
                adapter = new OrderHistoryAdapter(OrderHistoryList.this, orderHistories);
                adapter.notifyDataSetChanged();
                listView.setAdapter(adapter);
            }
            @Override
            public void onChildChanged(DataSnapshot dataSnapshot, String s) {

            }

            @Override
            public void onChildRemoved(DataSnapshot dataSnapshot) {

            }

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

            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

我猜你的问题是你引用数据的方式,而不是这个

void
find_entry1(u_int32_t key, hashnode_t **entry)
{
    u_int32_t index = get_hash_index(key, DB_HASH_LEN);
    u_int32_t offset = get_hash_offset(key, DB_HASH_LEN);
    u_int32_t start_index = index;

    do {
        printf("index %d offset %d ", index, offset);
        *entry = db_start + index;
        index = (index + offset) % DB_HASH_LEN;
    } while ((start_index != index) && ((*entry)->key != key));

    printf("\n");
}

使用此

 databaseReference = FirebaseDatabase.getInstance().getReference("order");

并且您没有使用查询对象来查询数据库引用

所以现在你不必像你做的那样直接从databaseReference查询

相反,你这样做:

databaseReference = FirebaseDatabase.getInstance().getReference().child("order");

一旦你有一个适合你的查询,你现在就添加一个子监听器并继续你的其余代码:

Query query=databaseReference.orderByChild("phone").equalTo(phone);