【问题标题】:how to retrieve all data from firebase and display on listview android?如何从firebase检索所有数据并在listview android上显示?
【发布时间】:2017-11-03 10:05:55
【问题描述】:

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

ListView Image

订单历史

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

订单历史列表

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

            }
        });
    }
}

【问题讨论】:

标签: android listview firebase firebase-realtime-database


【解决方案1】:

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

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

使用这个

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

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

所以现在您不会像以前那样直接从 databaseReference 查询

你可以这样做:

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

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

query.addChildEventListener(new ChildEventListener() {

 //the rest of your code goes here(on child added/changed/......)

)};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-25
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多