简体   繁体   English

如何通过比较android中的值来显示列表视图

[英]How to display a listview by comparing values in android

I have an app with a listview. 我有一个具有列表视图的应用程序。 Currently it shows all data that I input to the database. 当前,它显示了我输入数据库的所有数据。 I want to display data from firebase database by comparing current time and the time that I input to the database. 我想通过比较当前时间和我输入到数据库的时间来显示Firebase数据库中的数据。

here is the code 这是代码

 lv = findViewById(R.id.listView);
        Query query = FirebaseDatabase.getInstance().getReference().child("Details");
        FirebaseListOptions<Member> options = new FirebaseListOptions.Builder<Member>()
                .setLayout(R.layout.list_view_item)
                .setQuery(query,Member.class)
                .build();
        adapter = new FirebaseListAdapter(options) {
            @Override
            protected void populateView(@NonNull View v, @NonNull Object model, int position) {
                check();

                TextView loc = v.findViewById(R.id.locs);
                TextView des = v.findViewById(R.id.dests);
                TextView bus = v.findViewById(R.id.busn);
                TextView tim = v.findViewById(R.id.time);

                Member db = (Member) model;
                loc.setText(db.getLocation().toString());
                des.setText(db.getDestination().toString());
                bus.setText(db.getBusname().toString());
                tim.setText(db.getTime().toString());
            }
        };
        lv.setAdapter(adapter);

}

 @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }


@Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
}

Any error please guide me, I am new in Android. 如有任何错误,请指导我,我是Android新手。 Thanks in Advance :) 提前致谢 :)

You're looking for Firebase Database queries , which allow you to filter data on the Firebase servers. 您正在寻找Firebase数据库查询 ,该查询可让您过滤Firebase服务器上的数据。 Say that you have a field that holds a timestamp for each node under details (the number of milliseconds since January 1, 1970). 假设您有一个字段,其中包含每个节点在详细信息下的时间戳(自1970年1月1日以来的毫秒数)。 You can get every child node from between one and two weeks ago with: 您可以从一到两周前获得每个子节点,方法是:

Query query = FirebaseDatabase.getInstance().getReference().child("Details");

long oneWeekInMillis = 7 * 24 * 60 * 60 * 1000;
long oneWeekAgo = System.currentTimestampMillis() - oneWeekInMillis;
long twoWeeksAgo = oneWeekAgo - oneWeekInMillis;

query = query.orderByChild(timestamp).startAt(twoWeeksAgo).endAt(oneWeekAgo);

And you can just pass this query into the adapter, like you're already doing. 就像您已经在做的一样,您可以将此查询传递给适配器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM